Jump to content

Javascript GC or pooling


Recommended Posts

I'm not an expert in javascript GC so I'm still wondering the best way to approach object creation/destruction in javascript.

For example if you look at this PIXI demo: https://pixijs.io/examples/#/interaction/dragging.js

If you drag objects around for let's say 1 minute, you'll be creating around 10k Point objects.

But if you implement Point pooling (or other solution), you end up using only one Point.

Now coming from other techs I tend to think 10k object in one minute is very, very bad even though a Point is fairly small it does add up. 

So what are the javascript experts think about this? Is the javascript GC so good that creating 10k Point objects beats easily recycling one (with array.push > array.length - 1)

Link to comment
Share on other sites

24 minutes ago, botmaster said:

 If you drag objects around for let's say 1 minute, you'll be creating around 10k Point objects.

where did you see this ?

the GC correctly do job here and not create more point than necessary ?


If you got 10 000 point in memory after 1 min, something wrong, the 9 point your can see here came from native element in pixijs.

Pooling it better if you want keep ref and prevent GC, pixi-sound and greensock have good example of pool system,

In greensock, it scan every ~2 sec the pool, and than release for GC.

It good for managing no syncro stuff like tween.

Link to comment
Share on other sites

ho ok sorry for the misunderstand.
Well from my logic , a pool it good, because it avoid GC to remove and manage too many stuff in same frame.
It allow you to decant the emptying of memory, so it will in some case avoid `spike lag` and than a little freeze.
Yes i think pooling it a good idea to manage 10k... 20k points with GC.

Release 20k objets to GC!  you will probably have some spike lag at this frame. `(little freeze)`

it's better to have a pool with ref id, and remove these references through a time range if you have a lot of stuff.

Link to comment
Share on other sites

GC in javascript is frequently undergoing changes, but for the last few years (if this hasn't just changed...) one of the main problems is that it isn't occurring "in the background when the program is idle" like most people assume.

In truth the biggest GC hit occurs while creating a new object -- i know that sounds unbelievably bad but unless it has recently changed that's what is happening. The reason GC is invoked while creating a new object is that the trigger for GC is based off of trying to allocate new memory but discovering too much memory is already used. So right in the middle of important game code it'll periodically perform a round of garbage collection, and then finish creating that new object. This is how one can end up with relatively low CPU usage but still GC-related hitches... the GC is happening at basically the worst time every time.

So that detail of GC may change in the future, but if experiencing GC problems here are the two tricks I use most frequently...

As @ivan.popelyshev noted a local variable can prevent the GC. We can take a loop that used to create a new Point() (or several) as part of its internal logic. If that object is declared outside of the loop and has its values changed within the loop, then it'll only be created once in total.

The second is pooling... but it is important to really understand pooling's benefit else we simply re-invent the GC lag spikes. If a pool dynamically grows and shrinks much then the GC issue will be very similar. Also pools in the past were used to speed up object creation which I'm just going to say is rarely a benefit nowadays. The best pools for mitigating GC are pools that never (or very rarely) free anything at all. For the things that actually benefit from pooling, such as projectiles in bullet-hell game, or particles, this is somewhat reasonable feature. Just create 10,000 particles (or w/e) and recycle them as needed.

Link to comment
Share on other sites

It's definitely the kind of information I was looking for. Depending on objects, situation, and technology a pool can save memory/cpu/battery or can make things worse, that's the part I don't know about javascript. I guess for the dragging example I can let it run from a fully charged device and see how long it last, then I can run the pooled one in same condition and see if that makes any difference.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...