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

Kenneth J picture Kenneth J · Oct 23, 2009 · Viewed 14.8k times · Source

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?

Answer

kangax picture kangax · Oct 25, 2009

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.