10

It seems like Three.js does not have a good way to dispose a THREE.Scene and all of the objects within that scene.

Currently I am doing the following:

  $.each(scene.__objects, function(idx, obj) {                               
    scene.remove(obj);                                                                                     
    if (obj.geometry) {                                                                                    
      obj.geometry.dispose();                                                                              
    }                                                                                                      
    if (obj.material) {                                                                                    
      if (obj.material instanceof THREE.MeshFaceMaterial) {                 
        $.each(obj.material.materials, function(idx, obj) {                 
          obj.dispose();                                                                                   
        });                                                                                                
      } else {                                                                                             
        obj.material.dispose();                                                                            
      }                                                                                                    
    }                                                                                                      
    if (obj.dispose) {                                                                                     
      obj.dispose();                                                                                       
    }                                                                                                      
  });             

Looking at the Chrome Heap profiler, there are still many objects that do not get cleaned up (Textures, Shader Materials, Vectors, etc...).

zfedoran
  • 2,986
  • 4
  • 22
  • 25

1 Answers1

4

I agree with arriu that there should be a cleaner and generic way to dispose of memory in three.js, probably starting from the scene node and traversing all the way down. I also think that his generic function above should be extended in order to handle more types of memory allocation.

  • Looking at the example webgl_test_memory.html it does something very specific to the example and frees up memory right after its allocation.
  • Looking at webgl_test_memory2.html this example also does something very specific by adding meshes to an array and then going through and disposing the elements of the array. This method can not handle many memory allocations that have been made within the function calls.

I am not saying that the two examples do not free up memory. I think that the scene node should have a method to free all the memory below it.

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
gaitat
  • 12,449
  • 4
  • 52
  • 76
  • updated info on dispose() at http://stackoverflow.com/a/33199591/1980846 – gaitat Dec 16 '16 at 22:17
  • It can't really have that method. What if you're using the same object in two scenes? What if you're switching a scene but want to keep the objects? What if you delete a mesh but want to keep the material? Or maybe you want to keep the geometry, but dispose of the mesh (maybe you're changing it's material, or for whatever reason). In simpler, sample-like project this is usually not needed at all, but in large, production-ready projects this is something i encounter every single day. You just can not do it that freely, the developer MUST explicitly say what gets freed and what doesn't. – tfrascaroli Mar 06 '17 at 07:20
  • [Here's a good explanation from MrDoob](https://github.com/mrdoob/three.js/issues/5175) – tfrascaroli Mar 06 '17 at 07:34