2

I have a three.js interactive program that is loading multiple models. After the initial run, the program resets, removing all the models and clearing out all of the variables, but the memory usage does not decrease.

The .json models are taking up a lot of memory, which is interfering on many levels. We are trying to reduce the size of the models, but that is only going to go so far if the memory can't be reallocated.

From the research I've done, .deallocate() has been deprecated. I am loading using THREE.ObjectLoader(), so I'm not sure how the .dispose() would work in that instance. I tried:

scene.remove(basketContents[type][i]);
basketContents[type][i].geometry.dispose();
basketContents[type][i].material.dispose();
basketContents[type][i].texture.dispose();

But that gives me errors that .geometry.dispose(), etc. does not exist.

How can I remove the object from memory so that the memory can be used for other objects?

Mary7678
  • 413
  • 6
  • 12
  • JS automatically deallocates objects when you are finished with them; there is no way to manually deallocate them. Possible duplicate of http://stackoverflow.com/questions/22696253/deallocate-object-three-js – user886 Apr 14 '17 at 16:20
  • http://stackoverflow.com/questions/33152132/three-js-collada-whats-the-proper-way-to-dispose-and-release-memory-garbag – gaitat Apr 14 '17 at 16:28
  • Its more than just dereferencing js objects. Geometrija and textures take up space on the gpu that would just stay there without calling a specific method on the js counterpart. – pailhead Apr 14 '17 at 23:41
  • Thanks, @gaitat, I've looked at that post and don't seem to be able to use .dispose() because I used ObjectLoader to add the objects - I could just be using it incorrectly. See code above for how I'm trying (and failing) to use it. – Mary7678 Apr 17 '17 at 16:27
  • Thanks, @Andrew. I've read that post also. It does not explain how I can free up the memory. Whatever garbage collection is happening doesn't seem to be reallocating, so I'm guessing I'm doing something wrong in my code. – Mary7678 Apr 17 '17 at 16:28
  • Any ideas on a method I could call, @pailhead? – Mary7678 Apr 17 '17 at 16:29
  • 1
    @Mary7678 as the post says call: `disposeHierarchy (basketContents[type][i], disposeNode);` – gaitat Apr 17 '17 at 16:54
  • Ah, I see what your saying @gaitat! I was trying to not use the entire function since I don't have all those different pieces, but I copied that in as you suggested, and it does seem to be helping some . . . still not enough, but maybe there's more that I can do on the initial load . . . Thank you so much!!! – Mary7678 Apr 17 '17 at 17:24
  • Thanks, @gaitat - didn't know I could upvote comments, too! – Mary7678 Apr 17 '17 at 18:46
  • @gaitat - Ah, gotcha. Done! – Mary7678 Apr 17 '17 at 19:18
  • @Mary7678 not sure, every resource should have a `.dispose()` method but i'm not sure if i understand why you can't use it. There are some quirks though, so you definitely need to implement this kind of management yourself. As a last resort you can try nullifying the attributes of a geometry for example. Even if some reference is stuck somewhere, at least it should de-allocate the data. – pailhead Apr 17 '17 at 21:31
  • @pailhead, I would think it would have a `.dispose()` method, too - the browser's just giving me errors, so I think my code is wrong. If someone knows the correct way to use `.dispose()` on an object created with the ObjectLoader, I'm eager to learn! – Mary7678 Apr 18 '17 at 18:27
  • I didn't read through this too carefully, but possible duplicate of [http://stackoverflow.com/questions/37762961/three-js-proper-removing-object-from-scene-still-reserved-in-heap/](http://stackoverflow.com/questions/37762961/three-js-proper-removing-object-from-scene-still-reserved-in-heap/) – micnil Apr 19 '17 at 21:33
  • Thanks, @micnil. That's kind of what I'm doing now (see gaitat's link), but the overall memory usage still increases. It drops a bit but overall climbs, like in the graph shown in your answer. It'd be great if I could clean things out further. – Mary7678 Apr 20 '17 at 15:43

0 Answers0