Memory Usage

Hi

In my project I'm using this Box2D JS port:
https://github.com/illandril/box2dweb-closure
https://raw.githubusercontent.com/illan ... ompiled.js
(The below issue is present with Box2DWeb-Closure was present with Box2DWeb as well.)

When I open http://tobireif.com/boomchat/ in Firefox on Mac OS, FF's memory usage is at eg 239-262 MB. When I click many times on the various smiley/emoticon buttons on the page, in fast succession (so that the screen stays over-filled with various balls for a while) (as stress test), memory usage goes up to eg 403 MB, after a while. And although I then do nothing for minutes, it still doesn't go back to under 400 MB.

(When I click more it can go to eg 530 MB.)

Chrome's profiler points to Box2D.

Any idea?

If the issue can't resolved, I'll have to stop using Box2D (no offense! but I need to ensure that memory usage goes down when all balls are gone), and instead use some super-simple projectile trajectory calculation (without collision detection). But I'd miss getting collision handling for free ...

Tobi
I think you should ask the author of that port about this. I don't see any similar problem with the regular box2dweb, but he has made many changes to improve efficiency.
After spending a long time looking at it, I think the problem could be that instead of releasing contacts normally, he is putting them into an array (of 'contact nodes') so they can be reused. I guess this is faster than doing 'new' every time you need a contact.
This in itself is ok, but the problem (I think) is that when the contact nodes are put into the list, the reference to they contact they hold is not being nulled out.
You can see what I mean in the FreeNode function here: https://github.com/illandril/box2dweb-c ... istNode.js
I believe that in that function there should also be "node.contact = null;"
Otherwise, every contact that is created will hold a reference to the two fixtures (and hence their bodies as well) that will not be freed until another contact is made to reuse it.
Thanks!
I've already talked with tobireif over email, but I'll repeat what I've said to him here just in case somebody else comes along my port and notices the same problem.

box2dweb-closure uses object pools for a lot of things, one of which is b2Vec2. So, if you create a few hundred of your bodies, and they all collide with each-other, box2d needs to use a few thousand b2Vec2 objects at the same time. Then, instead of deleting those b2Vec2 objects, they get stored in an b2Vec2 pool, so the next time it needs a few thousand b2Vec2 objects it doesn't need to make more.
This will cause memory usage to stay high after something causes it to get high, but overall it improves performance if spikes like this happen multiple times during a single use (because of reduced garbage collection load).

The b2ContactListNode pool is (or was, I just fixed that) holding on to more than it needs to (good catch), but I think that would be a fairly small amount of extra memory compared to what the b2Vec2 pool uses (I haven't taken the time to test things after making the fix).


The object pools are designed to help mostly for situations where the activity is semi-regular, and isn't well suited for the "rare huge spike, followed by long almost nothing" type situations like Tobi was testing, because it just holds onto the memory needed for the next huge spike during the downtime. The pool helps a lot once that next spike comes, and in the time immediately after the spike when garbage collection would run, but in between it just holds on to lots of memory.
I recommended that Tobi limit how many smiley balls can appear at any time (because once the screen is full, adding more doesn't really change much). He did some more testing with slower clicks (which limited the number of simultaneous balls) over a long period of time, and found that memory stayed at reasonable levels, as expected.

Original thread: https://box2d.org/forum/viewtopic.php?t=9866