Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Clear Variables from Memory

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?
like image 471
askeet Avatar asked May 30 '26 21:05

askeet


2 Answers

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.

like image 89
drzhbe Avatar answered Jun 02 '26 10:06

drzhbe


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).

like image 36
Vladu Ionut Avatar answered Jun 02 '26 11:06

Vladu Ionut