What is the Javascript method CollectGarbage()? When and why should it be used?

不问归期 提交于 2019-12-03 23:49:04

问题


I notice in the VS2010 javascript intellisence there is a method called CollectGarbage().

Is this an IE only method?
When should it be used?
What are the advantages (or disadvantages) to using it?


回答1:


You might want to take a look at this post by Eric Lippert.

Is this an IE only method?

Nope. It's part of JScript, so anything that implements JScript (including IE, of course) should support it.

When should it be used?

When you want to hint garbage collector to start doing its job. Arguably, it shouldn't be used at all, and instead just let things happen on their own.

What are the advantages (or disadvantages) to using it?

I'm not aware of any disadvantages. Before using it, I would perform some tests to see if there are actual benefits in memory usage.




回答2:


It's part of the Microsoft JScript library. The documentation for it is pretty poor. I'm not sure how it works, but I would assume that it looks through the members of an object for things that it can delete.

And, as bdukes mentioned, this should not be called in your code. Leave it to the inner workings of the JScript library.




回答3:


How Do The Script Garbage Collectors Work? @ Eric Lippert's Blog



来源:https://stackoverflow.com/questions/1614662/what-is-the-javascript-method-collectgarbage-when-and-why-should-it-be-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!