2

I am writing code for JS. And I need to know how works memory in JS when I remove big Object.

var a = new Object();
a.b = new Object();
a.b.c = new Object();
a.b.c.d = new Object(); 

a.b = undefined; // Is it delete a.b.c and a.b.c.d or not?
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
askeet
  • 689
  • 1
  • 11
  • 24

2 Answers2

4

If there are no pointers to an object it will be garbage collected. Since the only pointer to a.b.c was in a.b, a.b.c will be garbage collected. Same situation with a.b.c.d.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
drzhbe
  • 785
  • 1
  • 6
  • 15
4

JavaScript is automatically garbage collected; the object's memory will be reclaimed only if the Garbage Collectior decides to run and the object is eligible for that.

The delete operator or nullify your object ( a.b = undefined; )has nothing to do with directly freeing memory (it only does indirectly via breaking references). See the memory management page for more details).

Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30