Reputation: 6749
This is a question to get a general idea how to avoid this situation. I have potentially millions of things, each with a few floats. I have to iterate through all of them, and inside the loop I have to do some calculations.
It is something like this:
for (var i = 0; i < 10000000; i++) {
var num1 = thing[i].x * something;
var num2 = thing[i].y * somethingElse;
buffer.push(num1);
buffer.push(num2);
var num3 = thing[i].z * something;
var num4 = thing[i].w * somethingElse;
buffer2.push(num3);
buffer2.push(num4);
}
I find that my memory usage explodes into the hundreds of megabytes and the browser crashes. As you can see, there is nothing hard about what I need to do, but I just need to do it on A LOT of things. I have found that the browser correctly garbage collects the temporary floats and my memory usage drops quickly, but that is assuming the browser doesn't crash first.
Any ideas how I could get the browser to garbage collect periodically (or some similar solution) so I don't end up with a huge gob of memory that crashes the browser?
Upvotes: 2
Views: 115
Reputation: 2950
You could try doing it in portions inside a function scope, have not tried this particular case, but pretty sure variables are garbage collected once they are out of scope:
function doPortion(nFrom, nTo) {
for (var i = nFrom; i < nTo; i++) {
var num1 = thing[i].x * something;
var num2 = thing[i].y * somethingElse;
buffer.push(num1);
buffer.push(num2);
var num3 = thing[i].z * something;
var num4 = thing[i].w * somethingElse;
buffer2.push(num3);
buffer2.push(num4);
}
}
var nStep = 1000;
for (var n=0; n<10000000-nStep; n += nStep) {
doPortion(n, n+nStep);
}
Upvotes: 1
Reputation: 1524
If you have logic witch is repeatable, do it dynamically... where you create needed value when it's really needed:
kind of: http://jsfiddle.net/acrashik/qdxLv/2/
var something = 10, somethingElse = 20;
function buffer(i){
var num1 = thing(i).x * something;
var num2 = thing(i).y * somethingElse;
return [num1, num2];
}
function thing(i){
return {x: i*10,y:i*1.5};
}
//get needed value
buffer(20);
alert(buffer(20))
Upvotes: 2
Reputation: 1524
reduce this case by twice memeroy consumption, just link second buffer to first;
var buffer = [];
var buffer2 = buffer;
for (var i = 0; i < 10000000; i++) {
var num1 = thing[i].x * something;
var num2 = thing[i].y * somethingElse;
buffer.push(num1);
buffer.push(num2);
}
updating first buffer you are updating second as well
Upvotes: 0