1

There is already some subject around three.js and memory usage, but i don't know if they're too old or if I'm still missing something : their solutions doesn't work for me with three.js r90.


LOADING all Meshs

here is how i'm loading objects :

      var loader = new THREE.ObjectLoader();
      var objects = [];

      function loadMesh(path) {

        loader.load(path, function (obj) {

           obj.traverse(function (child) {

              if (child instanceof THREE.Mesh) {
                 child.material.skinning = true;
              }
           });
           scene.add(obj);
           objects.push(obj);
        });
     }
     // ...
     loadMesh('path/to/obj01.json');
     loadMesh('path/to/obj02.json'); 
     loadMesh('...');
     ///...

After all meshs loaded, memory increase of around 1Go.


REMOVING all meshs (and free memory)

1 - first try

     function clearAll() {

        var objLen = objects.length;
        for (var i = 0; i < objLen; i++) {
           scene.remove(objects[i]); 
           objects[i] = undefined;
           objects.slice(i, 1);
        } 
     }

After calling this, all meshs are well removed from scene but memory don't decrease after the GC cycle. (I'm using the "about:memory" page in firefox)

2- try with dispose()

     function clearAll() {

        for (var i = 0; i < objects.length; i++) {
           scene.remove(objects[i]);
           objects[i].traverse(function (child) {

              if (child instanceof THREE.Mesh) {
                 child.geometry.dispose();
                 child.material.dispose(); 
              }
           });
           objects[i] = undefined;
           objects.slice(i, 1);
        }
        renderer.dispose(); 
     }

still no memory releasing after GC cycle.

3 - Trying to unset the renderer.

In the first try I notice that memory is release when i'm calling clearAll() if I never render the scene withrenderer.render(scene, camera). So i tried to unset it :

     function clearAll() {

        var objLen = objects.length;
        for (var i = 0; i < objLen; i++) {
           scene.remove(objects[i]); 
           objects[i] = undefined;
           objects.slice(i, 1);
        } 
        renderer = undefined;
        initRenderer(); // recreate and configure renderer.
     }

This way the GC free properly memory. This make me think that there is still some references to meshs in the renderer? Why they're not unset when i'm calling dispose on mesh and renderer?


EDIT

4 - try with the Gaitat's solution from here :

Three.js Collada - What's the proper way to dispose() and release memory (garbage collection)?

   function clearAll() {

    for (var i = 0; i < objects.length; i++) {
       scene.remove(objects[i]);
       disposeHierarchy (objects[i], disposeNode);
       delete(objects[i]);   
  }

  function disposeNode(){...}
  function disposeHierarchy(){...}

and no more memory release.

Cawet
  • 254
  • 2
  • 12
  • this should help: https://stackoverflow.com/a/33199591/1980846 – gaitat Feb 19 '18 at 13:57
  • Thank @gaitat but I already try your function, it's not working for me. the diference is that it's not a collada file that I'm loading but a json file generate with the blender module json exporter. – Cawet Feb 19 '18 at 18:25
  • it should not matter that it is not a collada object. The code takes care of the internal three.js generated structures. – gaitat Feb 19 '18 at 19:11
  • It's what I gess, but I tried it and still no positive result, so there is something somewhere I'm doing wrong or something somewhere that is not correct (see my edit). Am I loading the stuff correctly? – Cawet Feb 19 '18 at 20:18
  • how are you testing the memory used by your scene? Do you have a fiddle? – gaitat Feb 19 '18 at 23:38
  • Thank you very mush for your help ! i Can't use jsFiddle because it can't load external resources (for json) but i uploaded something here : – Cawet Feb 20 '18 at 10:23
  • http://cluse-app.cawet.net/static/threeApp/memory-test.html. (use the load and clear button at the bottom right) I'm testing memory by using the system monitor and I launch the GC with the about:memory page in firefox, right after push the "clear" button – Cawet Feb 20 '18 at 10:29

0 Answers0