Javascript Garbage Collection. Creating Objects and Vars?

I am currently creating a game in Javascript. After some testing, I began to notice an occasional lag that could only be caused by the fact that GK started playing. II decided to run a profile on it. The result shows that the GC is actually the culprit:

GC Profile

I read that creating new objects causes a lot of GC. I am wondering if something like this:

var x = []; 

Creates any kind of garbage since primitive types in Java do not. Since there are no real types in Javascript, I'm not sure. Also, which one is best for creating the least amount of garbage:

Option 1:

 function do() { var x = []; ... } 

Option 2:

 var x = []; function do() { x = []; ... } 

Option 3:

 function do() { x = []; ... } 

Or option 4:

 function do() { var x = []; ... delete x; } 

Option 5:

 var x = []; function do() { x.length = 0; ... } 

The do function in my case is called 30 Times a Second. And it performs several operations on the array.

I'm interested because I just made all of my variables global to try to prevent them from collecting GC, but GC hasn't changed much.

Could you also give some general examples of things that create a lot of garbage and some alternatives.

Thanks.

+5
source share
1 answer

Can you also show the timeline memory? If you have problems with the GC, this should be clearly obvious, as you will see a sawtooth waveform. Whenever the graph crashes, that GC kicks, blocking the thread to empty our garbage and that the main reason for freezing memory

An example of a sawtooth wave graph (a blue graph is a memory): enter image description here

Generally speaking, which instance of the object you are using does not matter much since the memory [] effect is minimal, you are interested in the contents of the arrays, but to view your options:

Option 1: This is generally good, with one consideration: Closing. You should try to avoid closing as much as possible, as they are usually the main reason for GC.

Option 2: Avoid references to things outside of your scope, this does not help memory, and it makes your application a little slower, since it has to go up the closing chain to find a match, There is no use for this.

Option 3: never do this, you always want to define x somewhere else you will deliberately leak into the global scope and therefore potentially never be GCed

Option 4: This is really interesting. usually delete x does nothing, since delete only affects the properties of an object. If you did not know, delete actually returns a boolean value indicating whether the object was deleted or not, you can run this example in the Chrome console:

 function tmp () { var a = 1; delete a; // false console.log('a=', a) // 1 b = 2; delete b; // true !!! console.log('b=', b) // Exception } tmp(); 

What?! well, when you say b = 2 (without var ), this is the same as writing window.b = 2 , so when you delete b , you basically do delete window.b that satisfy "only the property property declaration " However, DO NOT DO IT!

Option 5:. This actually saves you a tiny tiny bit of memory, since it does not have GC x , HOWEVER: it needs to GC all the contents of x which is usually much larger in size, so x it will not make any difference

This is a fantastic article if you want to know more about memory profiling and shared memory memory errors: http://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/

+1
source

Source: https://habr.com/ru/post/1234429/


All Articles