3

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?

8
  • This may help you: stackoverflow.com/a/742666/1249581. Commented Jun 2, 2012 at 17:43
  • Just try to limit its scope by as much as possible and use delete if really needed. Commented Jun 2, 2012 at 17:44
  • @d_inevitable delete removes the reference to the object but not the object itself. However, if you remove the reference, GC will know about useless object data faster. Commented Jun 2, 2012 at 17:45
  • I know. delete has merely the effect of setting it to null. Therefore I said, limit the scope as much as possible. Commented Jun 2, 2012 at 17:46
  • @d_inevitable: delete is not meant for variables. It only removes properties from objects. Commented Jun 2, 2012 at 17:47

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

youtube.com/watch?v=RWmzxyMf2cE That's a nice "conference" by Colt McAnlis about Javascript and GC.. It's interesting :)
1

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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.