1

I have some problem freeing memory in threejs when i remove a mesh using scene.remove(mesh) the mesh is removed but seems that memory used from js is not released.

I'm using webglrenderer with buffergeometry for the mesh and windows.

Salvatore
  • 177
  • 9

2 Answers2

6

This can be dark side of the js memory usage.

First try to setup primitive value to your objects.

mesh.geometry.dispose();
mesh.geometry = null; // or undefined .

// `delete` also cool but not support for old browsers 
// The delete keyword deletes both the value of the property 
// and the property itself.
delete mesh.geometry 

Another way (try some hack) :

 mesh.geometry = VerySmallmesh.geometry  //see for three.js how to do this if this is not correct mesh.geometry = null; 
// try to override memory stack 

You must be sure that this object is only instance of him self (how to say). Be sure you dont have a clone if you have than you will need to destroy him also.

Update : I want to say one more , use slice method for clearing arrays from object in forEach or for loop.

Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
  • uhm ok i will try to call dispose and see if something change, but if i have some object3D with some children and every children has another children i need to call dispose for every mesh? and there is a method for do this in every children easily? – Salvatore Oct 07 '16 at 15:06
  • Read it to the end this page (most important on buttom page): https://github.com/mrdoob/three.js/issues/5175 and than see example in this https://threejs.org/examples/#webgl_test_memory – Nikola Lukic Oct 08 '16 at 11:39
  • seems that works thank you but takes time for free memory, there is a quickly way for free memory? i ask this because i need to use threejs in a smartphone with 1 gb of ram – Salvatore Oct 09 '16 at 08:25
  • On mobile you will need big revision , i suggest to you to optimize every aspect of your project. Make mesh little more low poly , be careful about materials , textures even console.log can make troubles ... You say nice : 'takes time for free memory' ... – Nikola Lukic Oct 09 '16 at 12:15
  • yeah... i already know this... thank you for your time :) i will accept this answer on desktop application works fine, and for mobile no problem i think that i can manage the mesh in react components life cycle – Salvatore Oct 09 '16 at 12:26
0

I have also faced the same issue while working with my project.I suspect that you are only removing the mesh from the scene, not from the memory.Try doing the below to free the memory.It's working for me.

if (mesh) {
    scene.remove(mesh);
    mesh.geometry.dispose();
    mesh.material.dispose();                                    
    mesh = [];
}

Also, go through this StackOverflow discussion for further clarification. freeing memory in three.js

Community
  • 1
  • 1
vairav
  • 414
  • 3
  • 11