# Problems deleting assets from memory

**URL:** <https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463>\
**Category:** Questions\
**Tags:** memory-leak, geometry\
**Created:** [June 23, 2020, 12:40am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463 "2020-06-23T00:40:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ismael\_Sanz\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ismael_sanz_gonzalez/32/11830_2.png) [@Ismael\_Sanz\_Gonzalez](https://discourse.threejs.org/u/Ismael_Sanz_Gonzalez)\
**Post date:** [June 23, 2020, 12:40am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463/1 "2020-06-23T00:40:34Z")

</div>

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](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/b/bc0c320b2e79abf60913151f4dbc645fdc5c4939.png)

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [June 23, 2020, 12:58am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463/2 "2020-06-23T00:58:12Z")

</div>

> [@Texture.dispose(); called but scene still rendered](https://discourse.threejs.org/t/texture-dispose-called-but-scene-still-rendered/15539/2):
>
> Yes. If you immediately call dispose() right after creating a material, geometry, texture etc. nothing will happen. The entities have to be used for rendering at least once so the respective WebGL entities have been created (buffers, shader programs, texture objects). In other words, dispose() controls allocated memory on WebGL/GPU side. If you don’t allow the engine to allocate this memory first, freeing it will obviously have no effect.

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

---

<div class="post-metadata">

**Author:** ![Ismael\_Sanz\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ismael_sanz_gonzalez/32/11830_2.png) [@Ismael\_Sanz\_Gonzalez](https://discourse.threejs.org/u/Ismael_Sanz_Gonzalez)\
**Post date:** [June 23, 2020, 1:30am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463/3 "2020-06-23T01:30:51Z")

</div>

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 );

```

}

---

<div class="post-metadata">

**Author:** ![Ismael\_Sanz\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ismael_sanz_gonzalez/32/11830_2.png) [@Ismael\_Sanz\_Gonzalez](https://discourse.threejs.org/u/Ismael_Sanz_Gonzalez)\
**Post date:** [June 23, 2020, 1:32am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463/4 "2020-06-23T01:32:15Z")

</div>

the process is something like: delete\_elements - render - add\_elements ?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [June 23, 2020, 9:32am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463/5 "2020-06-23T09:32:14Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Ismael\_Sanz\_Gonzalez](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ismael_sanz_gonzalez/32/11830_2.png) [@Ismael\_Sanz\_Gonzalez](https://discourse.threejs.org/u/Ismael_Sanz_Gonzalez)\
**Post date:** [June 23, 2020, 9:43am UTC](https://discourse.threejs.org/t/problems-deleting-assets-from-memory/16463/6 "2020-06-23T09:43:03Z")

</div>

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()

```

}
