Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a a question that's been bothering me for some time.

I am using the three.js webgl library to render a large scene with many textures and meshes.

This question is not necessarily bound to webgl, but more javascript arrays and memory management.

I am basically doing this:

var modelArray = [];

var model = function(geometry,db_data){
  var tex   = THREE.ImageUtils.loadTexture('texture.jpg');
  var mat   = new THREE.MeshPhongMaterial({map:tex})
  this.mesh = new THREE.Mesh(geometry,mat);
  this.db   = db_data;
  scene.add(this.mesh);
};

function loadModels(model_array){
  for(i=0;i<geometry.length;i++){
    modelArray.push(new model(model_array[i]['geometry'],model_array[i]['db_info']));
  }
}

loadModels();

Am I being inefficient here? Am I essentially doubling up the amount of memory being used since I have the mesh loaded to the scene and an array. Or does the model (specifically the model.mesh) object in the array simply point to a singular memory block?

Should I just create an array of mesh ids and reference the scene objects, or is it ok to add the mesh to the scene and an array?

Thanks in advance and I hope I was clear enough.

share|improve this question
1  
Are you refactoring for the sake of it or have you encountered a performance or memory issue? – alex Jul 26 at 3:49
My memory usage gets exponentially higher for every model. Not only gpu memory but also the browser tabs memory which just seems way to high. That's why. I am still curious to see if assigning objects to an array is a serious memory hit, or does it simply create a reference to the (in this case mesh) object which is very large. – Hobbes Jul 26 at 4:00
I have experienced off-topic memory issues related to allocated memory persistence through refreshes which I thought was impossible. – Hobbes Jul 26 at 4:01
Anything is possible when the GPU is involved. :) – alex Jul 26 at 4:05
This is not good news lol =[ – Hobbes Jul 26 at 4:27
show 2 more commentsadd comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

The main thing that jumps out at me is this:

var tex   = THREE.ImageUtils.loadTexture('texture.jpg');
var mat   = new THREE.MeshPhongMaterial({map:tex})

If you are loading the same texture every time you create a new model, that could create a lot of overhead (and it can also be pretty slow). I would load the texture(s) and corresponding material(s) you need outside of your loop once.

Your modelArray is a list of plain model objects, each of which has a pointer to the corresponding mesh object (and db object). The scene has a pointer to the same mesh object so you are not exploding your memory use by cloning meshes.

It's possible that your memory use is just because your mesh geometries take up a lot of memory. Try loading your models one by one while watching memory usage; perhaps you have one that is unexpectedly detailed.

share|improve this answer
I do cache materials, geometries and textures ( though for textures i was really hoping i could reuse a texture at variable repeat settings but cannot =[ ). I'm glad to hear that a pointer is created in this situations =] I guess that's what I'll do, load the objects one by one and see whats causing this. Thanks for the input. – Hobbes Jul 26 at 17:57
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.