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.