Out of memory exception when adding and removing collision components

Hi Team!

I’ve been looking to add collision to a mesh entity that can change its scale which as I understand is not synced? So I thought as a quick workaround I’d remove the collision component on scale change and re add it. This seems to work but after a minute or so I get an out of memory exception from ammosjs suggesting that remove collision component does not free up the reference on ammo js.

here the remove and add code

gizoRoot.value.findComponents('collision').forEach((c) => {
addCollision(translateX, { type: 'mesh' });
addCollision(translateY, { type: 'mesh' });
addCollision(translateZ, { type: 'mesh' });

I know this is hacky but is there a way to ensure it gets cleaned up?

The error

p.s. whats the better way to update collision after scaling?

Hi @Adriaaaaan,

From what I remember the collision component system will remove any cached trimesh shape after the entity is removed/disabled.

Try checking in the console the following list to see if the list is indeed being cleared or shapes are still referenced leading to that memory crash:


Tried logging it out and the list isn’t growing (if thats what you’d seen before)

I see, then the memory leak is coming from somewhere else, not from the trimesh cache.

Scaling mesh colliders isn’t supported, but what you are doing (enabling/disabling collision comp) should work.

Try submitting a bug request on the engine repo about this, and possible post a sample project that reproduces the issue.

@yaustar @LeXXik may know more on this.

yeah just had a look at the code, I can see theres an internal event for “onInsert” but nothing regarding to onRemove so basically it appears that adding collision just leaks memory

1 Like

Not sure what is causing OOM here. A small repro project would help. What are those addCollision(x, { type: "mesh" })?

If you disable an entity or remove the collision component, then the rigidbody and its collision mesh is destroyed in the physics world. onInsert is related to inserting compound children to a parent. The safest option is to disable the entity, do your changes, then enable it. A repro project would be nice.

“addCollision” is just a wrapper function that adds the collision component. i’ll see if I can repo on a simple project

1 Like

I tried this

and it has the same issue