Problems deleting assets from memory

Hi all,

I have a script that generates shapes dynamically and then deletes them all to recreate them again with different parameters.

I can manage to eliminate them and recreate them, but it looks like all the geometry is still in memory…

any help please?

function dispose(children){

  for(var i = 0; i < children.length; i++){
    var child = children[i]
    var id = child.id
    child.geometry.dispose();
    scene.remove(scene.getObjectById( id, true ));

    var new_pts_geo = new Float32Array();
    children[i].geometry.attributes.position.array = new_pts_geo;
    children[i].geometry.attributes.position.needsUpdate = true;

  }

  renderer.dispose()
  scene.dispose()
  renderer.info.reset()
  renderer.update = true
  console.log(renderer.info)
}

image

Are you disposing the assets immediately, without letting them render at least once beforehand?

I have this other function animate(), but this function happens after I delete the elements and add them again…

function animate() {

requestAnimationFrame( animate );


controls.update();
renderer.render( scene, camera );

}

the process is something like: delete_elements - render - add_elements ?

What you are doing in your code is actually not valid. When a geometry is disposed, you have to create a new geometry and not just assign new buffer data. Besides, doing:

children[i].geometry.attributes.position.array = new_pts_geo;

is invalid, too. BufferAttributes are fixed sized and the array property of a buffer attribute should be considered as read-only.

yep yep I fixed it. I had a group and I was iterating through all the children to take them out of the scene. But it looks like the group was still referencing the mesh inside it… so instead of grouping them I just add them individually and then use this:

function dispose(children){

for(var i = 0; i < children.length; i++){

var child = children[i]
var id = child.id

child = scene.getObjectById( id, true )

child.geometry.dispose();
child.material.dispose();
scene.remove(child)
scene.dispose()

}