Box2d using a lot of b2Vec2s

Has anyone profiled a Box2D game in Flex/Flash Builder? I have, just this evening, and I didn't really like the results. I originally profiled my game because it was having significant slowdown whenever I started adding, I guess, "too many" physics objects on the screen. I have "chains" (made up of ~6 revolute joints each) attached to floating spheres in a buoyancy half-plane. I watched the profile of my game with these chains, and box2d is using 30% of the total memory to hold vectors (b2Vec2). That can't be right, can it?

After 120 seconds of play, there were 495,345 "new b2Vec2" calls, with 11,000 currently in use (after garbage collection was run). I've checked all my objects, and they're all cleaned up, removed from the game, removed from memory, when they're no longer in use. I've double checked that everything that gets Added to Box2D is Removed, and everything that's Created is Destroyed. That's a lot of b2Vec2s for the engine to be holding on to. I'd like to think I'm doing something wrong. Eleven-thousand vectors for ~130 total objects seems disproportionately high to me.

For more numbers, there are, in the same memory snapshot (descending from most number of alive instances after b2Vec2):
1408 b2Mat22
1145 b2Vec3
1067 b2DynamicTreePair
695 b2ContactID
695 Features
688 b2ManifoldPoint
559 b2AABB
(more available, if it's helpful)
The core of box2d is inlined to avoid allocations. But the core is pretty small - I haven't done joints. So there are still quite a few allocations. In use count does seem high, but possible.

Alas, I do not have any good profiling tools available, so things are not quite as tight as could be.
Also the java port of box2d show many vec2 allocation, so probably is the original code to be vec2 hungry
The original C++ code uses vec2s all over the place. The difference is that they are a simple struct that is created on the stack and not worried about. However in a managed language like AS3 everything is created in managed memory and put up for garbage collection when the reference count goes to zero or it becomes unreachable from the root. As BorisTheBrave has said, there are many places in the code that could use a closer scrutiny towards language specific optimizations( pools instead of new allocations for instance. ) That will come with time as Boris gets time to work on it. However he isn't the only one capable of doing work on the port and anyone with time and skill should contribute.

With that said, b2Vec2 is used as a general purpose vector that could hold things like position, velocity, acceleration, forces, vertices for polygons or any number of things. If you have a simple box it could very well have dozens of b2Vec2s to describe its properties.
I mostly wanted to make aware the fact that so many b2Vec2s are being allocated, but more importantly, that so many do not seem to be getting cleaned up. It appears there's a leak, as the longer I leave my app running, the more (thousands more) b2Vec2s become lodged into memory. Something is maintaining a reference to them.

Yes, it's possible that I may have a leak somewhere in my simple main menu demo, but I've searched my code (with the help of others, just in case it's a mistake blind to the maker) enough to make myself sure that there isn't. Still, no one's perfect. 🙂
There's a lot of permanent pooling so you may see a large amount of vectors even after you have deleted everything on the world. But that's not a memory leak, as the amount cannot grow unbounded. I'm pretty confident there are no leaks.

So, 11000 vectors for 130 objects. That is basically 100 vectors per object. Suppose we are considering a random pile of boxes. Each box shape is 4 vectors + 10 per body + 2 per fixture. The broadphase probably consumes at least 2 per shape as well. (in v2.0, every polygon was 8 vectors, so that's improved).
Then we have b2Contacts. For a pile, we can expect at least 4 contacts per box (remember, contacts are made based on loose contact), divide by two as contact is between two shapes. Each contact has two manifolds (old and new), each of which has 3 vectors.

Thus conservative estimates per object come to 18 + 4/2*2*3 = 30. So I don't think the quantity you are seeing is that unreasonable. There's a lot of work internally to avoid allocations duplicating calcuations, and that means keeping large amounts of objects active.

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