2

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?

4

3 回答 3

3

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

于 2012-06-02T17:43:28.877 回答
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.

于 2012-06-02T17:43:16.237 回答
0

正如我所说的那样,您需要中断所有引用才能使变量有资格进行垃圾收集。如果您无法追踪对特定对象的最后引用,这可能是一项艰巨的任务,因此请使用您可用的工具来完成此任务。我个人使用 Chrome 的堆分析器,您可以在 chrome 文档中阅读。

另外,请注意只有非原始类型通过引用传递(因此只有非原始类型可以被 GC'd)。

于 2012-06-02T17:49:32.807 回答