I encountered a memory leak issue while loading multiple obj mesh files with mtl file using three.js. Here are the steps I followed:
Start by visiting this page: . Once the page is loaded, click on 'load' and you will see yellow meshes appearing on the screen as you move your mouse;
Next, if you are using Windows, open your task manager to check how much memory the browser tab is consuming, take note of it.
Lastly, click on 'reload', then re-click on 'load' at the top-left corner of the page or 'clear', and monitor the memory usage in the task manager again. You'll notice that the memory usage keeps increasing and doesn't seem to decrease.
I have included some release code at the end of the index.js file, here is the snippet:
function reload() {
var indexes = [];
for (var index in scene.children) {
if(scene.children[index].name.indexOf('test') !== -1){
indexes.push(scene.children[index]);
}
}
for(var index in indexes){
scene.remove(indexes[index]);
}
load();
};
I made changes to the two main functions as follows:
function load() {
clear();
var paths = [];
for(var i=1;i<=4;i++){
paths.push({obj: i + '/model_1v.obj', mtl: i + '/model_1v.mtl'});
}
var onProgress = function(xhr) {
if (xhr.lengthComputable) {
var percentComplete = xhr.loaded / xhr.total * 100;
}
};
var onError = function(xhr) {};
THREE.Loader.Handlers.add(/\.dds$/i, new THREE.DDSLoader());
for(var i=0;i<paths.length;i++){
var path = paths[i];
var loader = new THREE.OBJMTLLoader();
loader.load(path.obj, path.mtl, function(model) {
model.name='test-' + i;
scene.add(model);
}, onProgress, onError);
}
}
function clear() {
THREE.Cache.clear();
var models = [];
for (var i in scene.children) {
if(scene.children[i].name.indexOf('test') !== -1){
models.push(scene.children[i]);
}
}
for(var i in models){
scene.remove(models[i]);
models[i].traverse(
function(obj){
if (obj instanceof THREE.Mesh) {
obj.geometry.dispose();
obj.material.dispose();
}
}, true
);
}
models.length = 0;
};
Despite these changes, the issue persists. If you're interested in helping out or exploring further, you can clone the code from this repository: https://github.com/idazhenhan/idazhenhan.github.io.git , and run the code on your local server to get a clearer picture of the problem.
I am seeking assistance in resolving this issue. Can anyone help me with this?