sublime
sublime

Reputation: 4171

javascript memory management - deleting a complex object

I have a big object which holds lot of data. It is structured in this way:

myObject = {

   "key1": Array1[256] , // this is array of objects
   "key1": Array2[2048],
    .
    .
    .
}

I want to now reset this object ( set it to {} ). Before I do this, I want to de-allocate all the memory. What is the right way of doing it?

If I say delete myObject; would it delete everything? Or do I actually have to delete an array of each key. or even further, Do I have to delete each object in the array of each key as well?

It would be great if you can point me to some good reference material on this topic.

Thanks.

Upvotes: 1

Views: 135

Answers (1)

Ry-
Ry-

Reputation: 225164

If you do delete myObject;, it will do absolutely nothing, because delete removes properties of an object. JavaScript has garbage collection; the way to set this object to {} is to just do that:

myObject = {};

If you want to modify the existing reference (i.e. other things point to the object), you can delete each key:

for (var k in myObject) {
    delete myObject[k];
}

Use strict mode, by the way. It makes things saner, including delete.

Upvotes: 1

Related Questions