Here is the code which test in IE8:
var stack = [];
function test() {
var i = 0;
while(i++ < 100000) {
stack[i] = {a: "some string"};
delete stack[i];
};
}
test();
This script only uses a little memory in IE. Windows task manager shows 29704K, But the next:
var stack = [];
function test() {
var i = 0;
while(i++ < 100000) {
stack[i] = {a: "some string"};
};
i = 0;
while(i++ < 100000) {
delete stack[i];
}
}
test();
It uses 54860K memory. I wander why or how to release the memory other way.
Appreciated for your help.
If you are just looking at the total browser memory use at the system level, then you may be looking at the maximum browser memory usage, not how much memory is actually in use. Just because you free something up in the browser does not mean that the browser returns that memory to the OS. That memory may be available for future reuse in the browser, but not returned yet to the OS.
Your second example will have a higher peak memory use than the first example because you fully populate the array before you then remove elements. Thus, it would not be surprising if it shows a greater memory use at the system level because of the higher peak usage. That doesn’t mean that the higher amount of memory is still in use, just that the browser had to request more memory from the system in order to deal with the higher peak usage and the browser did not necessarily return the now unused memory back to the OS. That memory will be in the browser’s pool of free memory that can be used for future memory requests so it’s not a memory leak.