Dynamically create and destroy Three.js scene without memory leak

Suppose we had a page with two buttons, create and destroy. When you click the create button, the Three.js script in this memory test below is dynamically added to the page and launched. Clicking on the kill should delete the scene, split all buffers and free all memory, etc. http://mrdoob.github.com/three.js/examples/webgl_test_memory.html

Does anyone know how to do this without creating a scene and without changing the URL?

thanks

+4
source share
3 answers

JavaScript is a garbage collected language. If you no longer have references to any object (for example, to an old scene), then the memory will eventually be fixed if there is no error in it. However, the page you are linked to is working fine.

+2
source

I myself was concerned about this issue for a long time, I raised error messages using chrome and Three.js, but could not find a solution.

For some reason, the memory is not freed even after a long time, and it seems that something continues to point to the memory block, and therefore the garbage collector never frees it.

However, here is what helped

  • Create an array that will contain all the elements added to the scene.
  • When you add an additional element to the scene, add it to this array.
  • In kill mode, run scene.remove ('item name') to remove them from the scene.
  • Now iterate over the array and manually make all the elements undefined.

After that, I was able to get more than 600 MB of RAM from the Three.js scene.

update

The answer to this question is Mr. Doob and WestLangley Memory leak with three .js and many shapes , I still have to check it with my code.

In webGLRenderer after deleting the grid with

scene.remove( mesh ) ,

you can free memory with

renderer.deallocateObject( mesh );

You can free the texture with

renderer.deallocateTexture( texture );

+2
source

I think you need to use the dispose () method in lateral geometry, material and texture.

 geometry.dispose(); material.dispose(); texture.dispose(); 

https://github.com/mrdoob/three.js/blob/master/examples/webgl_test_memory.html

+1
source

All Articles