Say I instantiate a large object with var and new. After I'm done with it, can I "free" it by setting it to null? Is this something Javascript GCs look for?
3 Answers
The garbage collection is interested in objects, that are not referenced by ANY other object. So make sure there are no references left anywhere in your application (Arrays etc.)
1 Comment
You can break the reference by setting the variable to null, but it doesn't break any other references.
All references need to be broken individually before the object can be GC'd.
So yes, if the only reference to the object that is held by that variable, then setting it to null will free it for eventual GC.
Comments
as am not i am stated, you'll need to break all references in order for a variable to be eligible for garbage collection. This can be a difficult task if you can't track down that last reference to a specific Object, so use the tools available to you for this task. Personally I use the Chrome's Heap Profiler which you can read about in the chrome docs.
Also, note that only non-primitive types are passed by reference (and therefore only non-primitive types can be GC'd).
deleteif really needed.deleteremoves the reference to the object but not the object itself. However, if you remove the reference, GC will know about useless object data faster.deletehas merely the effect of setting it to null. Therefore I said, limit the scope as much as possible.deleteis not meant for variables. It only removes properties from objects.