15

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?

Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
Kenneth J
  • 4,846
  • 11
  • 39
  • 56

3 Answers3

15

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.

kangax
  • 38,898
  • 13
  • 99
  • 135
  • 1
    it does not work in firefox, so I'm not sure if it's a standard – xus Apr 19 '12 at 11:37
  • 2
    Like I said, it's part of JScript. Firefox does not implement JScript. It implements JavaScript (+ its own set of non-standard extensions). – kangax Apr 19 '12 at 13:48
  • 5
    According to wikipedia "JScript is Microsoft's dialect of the ECMAScript standard that is used in Microsoft's Internet Explorer". So I guess it's fair to call it IE-only. – Edson Medina Nov 05 '13 at 10:47
3

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.

EndangeredMassa
  • 17,208
  • 8
  • 55
  • 79
  • In addition, the documentation notes that it "is not intended to be used directly from your code." So to answer the original question, don't use it. – bdukes Oct 23 '09 at 17:00
  • 2
    /golfclap at MS extension nonsense *again* – annakata Oct 23 '09 at 17:41
0

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

Distagon
  • 1,018
  • 11
  • 14
  • 3
    see what he says at the beginning of the article: This article should be considered "for historical purposes only"; it does not reflect how JScript works today. (written in 2003) – xus Apr 19 '12 at 17:54
  • Yea, this was changed in JScript 5.7 released with IE7 in 2006. – Yuhong Bao Apr 28 '12 at 03:57