Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leaks when destroying objects. Remaining __cache__ references? #284

Open
Maksims opened this issue Nov 18, 2019 · 6 comments
Open

Comments

@Maksims
Copy link

Maksims commented Nov 18, 2019

We've been using ammo.js in https://github.com/playcanvas/engine and recently I were going through some memory leaks related to ammo.js. Everything has to be manually cleared, but there are certain references left in ammo.js __cache__ objects.

Here are few examples:

  1. creating btRigidBody, then get use getMotionState on it. Then Ammo.destroy(body), but motionState will remain in Ammo.btMotionState.__cache__. Calling Ammo.destroy(state) on it, will throw an error, as I would assume it is actually destroyed already with body, but reference still in present.

  2. Creating btCollisionShape, of different types, like btBvhTriangleMeshShape. Calling Ammo.destroy(shape) removes shape, but leaves btBvhTriangleMeshShape in Ammo.btBvhTriangleMeshShape.__cache__.

  3. btDefaultMotionState, when related btMotionState is destroyed, it does not remove related Ammo.btDefaultMotionState__cache__.

  4. Calling manifold.getBody0 or manifold.getBody1, will return btCollisionObject, but then references to it, never removed as well, when that object is eventually destroyed by Ammo.destroy(rigidBody).

How do we properly remove those related __cache__ references, when their actual objects are destroyed internally? Doing: delete Ammo.btBvhTriangleMeshShape.__cache__[shape.ptr]; seems very hacky.

@kripken
Copy link
Owner

kripken commented Nov 18, 2019

Thanks, this does sound like a bug. I opened emscripten-core/emscripten#9854 in emscripten, with an idea for how to avoid it.

@lm165678
Copy link

Hi, can someone help me? When I use ammo to create a large number of btbvhtrianglemeshshapes, if the number of meshes is too large, I will report oom. But I don't know how.

I have 10473 solid objects, 877308 vertices and 292436 triangles. I hope to solve this problem without reducing the number of grids, because this is a BIM project.

@lm165678
Copy link

When creating the btbvhtrianglemeshshape of the 7000th entity, it will report oom. When you directly treat the whole scene as a btbvhtrianglemeshshape, it will directly oom. So I divided it into more than 10000 btbvhtrianglemeshshapes.

@Maksims
Copy link
Author

Maksims commented Apr 12, 2020

Sorry for offtopic, but 10k physical objects, is definitely an overkill for a web browser. With such numbers of objects and triangles, you will want to move towards native platforms, and think of optimising an approach. As even for native, it will be a struggle.

@lm165678
Copy link

Sorry for offtopic, but 10k physical objects, is definitely an overkill for a web browser. With such numbers of objects and triangles, you will want to move towards native platforms, and think of optimising an approach. As even for native, it will be a struggle.

Thank you for your reply. It seems that I can only use game like solutions to solve physical simulation on BIM..

@lm165678
Copy link

Sorry for offtopic, but 10k physical objects, is definitely an overkill for a web browser. With such numbers of objects and triangles, you will want to move towards native platforms, and think of optimising an approach. As even for native, it will be a struggle.

Hi, I solved my problem and made sure the frame rate of both the renderer and the physical engine is 60. For non wasm versions. That is, the ammo.js file. Before importing ammo.js, add the statement "var Module = {TOTAL_MEMORY: 512 * 1024 * 1024};" to allocate the total memory. In the official case, it seems that modifying the total memory has no effect, and the memory still leaks. So I modified the ammojs file and added the code: "d.TOTAL_MEMORY = Module ? Module.TOTAL_MEMORY : null;" and then I succeeded. I hope I can help you. In addition, for bvhtrimesh, if too many physical engines are created, the frame rate is relatively low. But if you combine the mesh into one, even millions of vertices and triangles, the physics engine on my laptop is still 60 frames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants