Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Garbage Collection pauses

I am trying to create a simple game engine in JavaScript with WebGl, but I am having some undesired problems with JavaScript in general, which I hope can be avoided. Besides the overall lack of performance with JavaScript, I am having some strange pauses when rendering with WebGl, which happen periodically, once every second or so. I assume this has to happen with GC in JavaScript. Is there anyway to minimize these "stutters"? Are there any common practices I should know, a way to force, at least part of the garbage collection, to happen at a time that I can control?

I know these are simple questions, but I am fairly new to JavaScript, and searching on the internet did not give me to many useful information.

like image 719
Colin Dumitru Avatar asked Jan 28 '11 10:01

Colin Dumitru


People also ask

What is pause time in garbage collection?

Metronome garbage collector (GC) pause time can be fine-tuned for each Java™ process. By default, the Metronome GC pauses for 3 milliseconds in each individual pause, which is known as a quantum.

How do I stop long GC pauses?

If your application's object creation rate is very high, then to keep up with it, the garbage collection rate will also be very high. A high garbage collection rate will increase the GC pause time as well. Thus, optimizing the application to create fewer objects is THE EFFECTIVE strategy to reduce long GC pauses.

How do I fix long garbage collection time?

To reduce GC times, the best thing you can do is use off heap memory. If you can move as much of your large data as possible you can reduce your full GC time to as low as 10 milli-second even with 100s of MB of off heap memory.

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.


1 Answers

Re-use objects as often as possible. If you are creating dozens of objects (like vectors and matrices) for each rendered frame then you will definitely receive GC-related stuttering. So when you are using a scene-graph based approach for rendering your game you might want to cache objects in the scene graph nodes for example. Or you can use the Object Pool Pattern. In other languages like Java this technique is deprecated because the object creation and GC is so fast today that a object pool doesn't help anymore. But in JavaScript it might still help.

I had GC-stutter-problems in a JavaScript game I wrote last year and I solved it by rewriting my 2D vector engine so absolutely no new objects are created during frame rendering. Objects are only created once when the scene is build-up or new scene nodes are added to it. But displaying and animating the scene creates absolutely no new objects.

You may want to take a look at my 2D engine:

http://www.ailis.de/~k/hg/javascript/twodee/file/tip/src/main/javascript/twodee

You'll notice that I cache temporarily needed vectors and matrices in static fields and that I used mutable vector and matrix classes instead of immutable so existing vectors/matrices are modified instead of creating new result vectors/matrices when doing math with them.

like image 96
kayahr Avatar answered Nov 06 '22 13:11

kayahr