3

With an Array I know you can use the delete keyword to remove items out of the Array. However, delete will not delete a variable.

var b = "some stuff";
delete b;
console.log(b); // "some stuff"

What's the correct way to "free" the memory used by b ? Does doing just b = null; do the trick?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 1
    In case you don't know: If `b` is declared in a function scope, you may not have to worry about it at all. Once execution leaves the function scope, if you have no other references to it, it is as free as you can hope for. – James Clark Dec 05 '11 at 01:15
  • 1
    @JamesClark but one might run into the case where `b` is loading so much data, that it must be freed before loading another chunk of data – puk Dec 05 '11 at 01:20

2 Answers2

3

Possible duplicate of this question about unsetting variables in Javascript with some excellent answers. Short answer - null is probably fine.

Community
  • 1
  • 1
Tak
  • 11,428
  • 5
  • 29
  • 48
1

In Javascript, you cannot really 'free' any memory yourself; all you can do is remove all references to the memory that an object uses, and the JS engine's garbage collector will recover it. Setting the variable / property to null would be a good starting point.

As regards the use of 'delete', I haven't found a better resource for understanding it than http://perfectionkills.com/understanding-delete/.

JamieJag
  • 1,541
  • 2
  • 11
  • 18