1

Is object literal free memory when same object name recreate? what is effect on memory when I recreate same object name from object literal. e.g.

var objLit = {};
for(var i=0; i<5; i++){
    objLit['room'] = {};
    objLit['room'].name = "A",
    objLit['room'].class = 10
}

console.log(objLit['room']);

output is currect but what about 4 previews object literal which made by for loop. my question:

is all 4 object auto delete? is all 4 object reference auto delete?

Sandeep
  • 1,461
  • 13
  • 26

1 Answers1

0

I gave this a try in Chrome:

var objLit = {};

for(var i = 0; i < 500000000; i++){
    objLit['room'] = {};
    objLit['room'].name = "A",
    objLit['room'].class = 10
}

console.log(objLit['room']);

If the memory wasn't released, you'd expect the memory usage for that tab to keep growing while the loop was running.

This wasn't the case. Chrome does not consume more memory if the loop is 500000000 iterations long, than when it's 50 iterations long.

In other words, garbage collection gets rid of the previous value.
(In Chrome, but I'm sure other browsers behave the same way for an example like this.)

Cerbrus
  • 70,800
  • 18
  • 132
  • 147