Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you want force garbage collection on a variable in Javascript, is it better to set it as null or undefined?

I am developing an Android game using JS framework. I want to make sure that some of the objects are garbage collected once I am done using them. How do i force it? Should I use null or undefined?

like image 703
Deepan Chakravarthy Avatar asked Jul 04 '13 22:07

Deepan Chakravarthy


People also ask

How do I force garbage collection in JavaScript?

As of 2019, it is not possible to explicitly or programmatically trigger garbage collection in JavaScript.

What is the easiest way to immediately force Java garbage collection?

Before an object is garbage collected, the Java runtime system gives the object a chance to clean up after itself. This step is known as finalization and is achieved through a call to the object's finalize method. You can force object finalization to occur by calling System 's runFinalization method. System.

Does garbage collection affect performance?

The most common performance problem associated with Java™ relates to the garbage collection mechanism. If the size of the Java heap is too large, the heap must reside outside main memory. This causes increased paging activity, which affects Java performance.


2 Answers

If you want force garbage collection on a variable in Javascript, is it better to set it as null or undefined?

Doesn't matter, you can even set it to another object you created: as long as the object originally referenced by the variable is no longer referenced anywhere, it can be garbage collected. Think of your object as "something being referenced by variables".

If no variables reference it, it can be garbage collected, and "putting another object into a variable" will make the variable no longer reference the previous object.


I want to make sure that some of the objects are garbage collected once I am done using them. How do i force it?

You can force major garbage collection to be triggered in a Node-based runtime (e.g. Electron or NW.js), by using the --expose-gc flag and running:

global.gc();

If your app/game is browser-based, in Chrome only you can use the --allow-natives-syntax switch (your users would need to pass this to Chrome themselves), and call:

%CollectGarbage();

Note: dead simple, but you might as well just do a window.location.reload() as a fallback, if you can easily continue from where you left off (e.g. reload the game into another level, if you will).

Remember, however, these calls won't screw around, and can block for whole seconds if there is a lot of garbage to collect. This can be ideal if you have determined pauses in your game, such as loading screens between levels, but tricky otherwise.

You might also want to consider using an object pool, with or without using manual garbage collection, to simply reduce garbage and improve performance.

like image 51
John Weisz Avatar answered Sep 28 '22 08:09

John Weisz


Best advice is to define them in scope that makes them eligible for garbage collection. This means don't use global variables that never become eligible for collection. Instead declare them as locals.

like image 36
TGH Avatar answered Sep 28 '22 08:09

TGH