Release mesh vertices data from JavaScript heap (once copied in GPU)

I’m creating meshes with Float32Array vertices data.
Many of those vertices data are no longer used once copied by Babylon.js to the GPU via native WebGL bufferData.

Looks like Babylon.js keeps reference to my Float32Array vertice data sources. And this keeps the JavaScript garbage collector from releasing memory.

Is there a way to explicitly remove reference to the vertice data kinds of my choice? I don’t need tangent, normals, etc… anymore. But maybe want to keep position (in order to keep the Babylon.js click event working).

I want to reduce JavaScript heap memory pressure as low as possible (mainly for Safari).

Note that I’m never copying, cloning meshes and I’m not making use of Mesh.getVerticesData.

Thank you.

3 Likes

Cc @sebavan

1 Like

Let me add @ryantrem to the thread as he did something really similar recently for some meshes but I can not remember the function name ?

2 Likes

The only thing I can think of that I did recently that was similar to this was about releasing all the geometry data from main memory after it is loaded into graphics memory. In my case, given a rootNode for a loaded glb, I had something like this:

(rootNode.getChildMeshes(false, (mesh): mesh is BABYLON.Mesh => mesh instanceof BABYLON.Mesh)).forEach(mesh => mesh.geometry.clearCachedData());

But this would also break picking, which it sounds like @yvele is trying to maintain, hence the idea of only clearing the data for certain vertex attributes (e.g. everything except positions, and maybe normals if they are used to determine the normal of the raycast hit).

For that though, I think you could just call mesh.geometry.removeVerticesData instead to remove only the attributes you’re sure you don’t need (for things like picking). @sebavan do you think this would work?

@yvele - since it seems like you are concerned about the footprint in main memory, can you share a little more detail about the meshes you are creating? What vertex attributes do they have?

2 Likes

So I can do:

That looks OK for me :+1:

But, I know that on Apple devices GPU memory and RAM are shared… does this._vertexBuffers[kind].dispose(); also dispose memory in GPU? (I don’t think so but just to make sure…).

What does VertexBuffer.dispose really do?

since it seems like you are concerned about the footprint in main memory, can you share a little more detail about the meshes you are creating? What vertex attributes do they have?

I have indices, positions, normals, tangents, uvs and custom attributes as well.

1 Like

This releases the webgl memory associated with it, and you will not be able to draw it anymore :slight_smile:

But I need only the memory to be released from JavaScript heap… not from GPU :thinking:
I still need the render loop to draw the geometry.

What I need is only remove TypedArray JavaScript references but keep the vertex data in the GPU… does that make sense?

this is exactly what the clearCachedData does.

1 Like

One issue might also be mesh.getTotalVertices() calls. Think this is is called by inspector, so might no be a could idea during development. Save for deployment.

1 Like

Ok cool thank you! So the render loop will still work.

Oh ok. I’m not using the inspector :wink: I’m only using Spector.js but I think Spector.js is 3D engine anemic anyway (and then doesn’t rely on Babylon.js internal structure)

@sebavan @JCPalmer I have a related question… Does clearing the cached vertice data prevent Babylon.js from recovering from WebGL context lost? :thinking:

As the documentation says: Babylon.js “keeps track of resources creation” does that mean that Babylon.js recovers from geometry cached data (the one that I’m clearing)? :thinking:

Yup this will need to be handled on your side in this case :slight_smile:

1 Like