How to free cpu-side memory for geometry/texture already on GPU

Is there a way to free CPU side memory once the mesh data is uploaded to GPU?

I tried to remove references to the geometry and texture Array Buffers so that they get garbage collected once they’ve been uploaded to GPU

something like this:

mesh.onAfterRender = () => {
            if(mesh.geometry && mesh.geometry.attributes){
                if (mesh.geometry.attributes.position) {
                    mesh.geometry.attributes.position.array = undefined;
                    if (mesh.geometry.attributes.position.data) {
                        mesh.geometry.attributes.position.data.array = undefined;
                    }
                }
            }
            ...
            if (mesh.material && mesh.material.map) {
                mesh.material.map.mipmaps = undefined;
                if (mesh.material.map.source) {
                    mesh.material.map.source.data = undefined;
                }
            }
        }

I do actually see the memory decrease slightly But the ArrayBuffers are still in memory so something else must hold a reference to them.

You don’t want to do that. You can lose the WebGL context at any time and then you need that data to restore the context.

https://www.khronos.org/webgl/wiki/HandlingContextLost

Thanks for the answer. I’d be satisfied if it weren’t for iOS.

I don’t know (yet) if and how three.js manages the amount of memory for a tab but iOS doesn’t allow growing memory allocated to a tab, also for a rare potential crash risk.

So if I delete CPU side buffers the context can’t be recreated but it would allow me to load a bunch more data before my iOS tab crashes.

I can mitigate this with ktx and LODs but I was hoping to push the limits

1 Like

This is the way to do it. Did you try to force GC before profiling?

Yeah with chrome’s profiler, there’s a force GC button. I still see the array buffers in the dump after that.

It’s a bit obscure but I see a reference to the backing array buffers for geometry in the “proto” object but not sure what that means or if these references are transient to the debugger or something