views:

193

answers:

7

How can I purposely overflow my memory, to force garbage collection? Can someone propose algorithm like this:

while ( garbage collector starts ) {
      overflow my memory with something easily disposable
}

Edit: To everyone that purposed GC.Collect method. I've always tought, that GC can not be forced to occur programmaticaly. Guess, I was wrong. Thanks for the answers.

+7  A: 

Better yet, how 'bout using GC.Collect? No need to synthesize a condition when there's an explicit feature available...

Also, note the GC.WaitForPendingFinalizers method that Adam Butler (comment above), ChristopheD (answer below), and Michael Petrotta (comment below) pointed out, which takes the next step. Scary quote from the documentation on that method, though: "The thread on which finalizers are run is unspecified, so there is no guarantee that this method will terminate." *shudder*

T.J. Crowder
@T.J.: As the top-voted answer, since the OP appears to require a synchronous GC, consider adding `GC.WaitForPendingFinalizers()` to your answer.
Michael Petrotta
@Michael: Yeah, big time.
T.J. Crowder
A: 

Is there some reason GC.Collect() doesn't work for you? That forces garbage collection to occur.

kidjan
A: 

Why not just use GC.Collect to force a garbage collection instead?

JaredPar
+1  A: 

Like this, for example:

int cnt = GC.CollectionCount(0);
while (GC.CollectionCount(0) == cnt) {
  string s = new String('*', 1000);
}

However, this will of course only run until a garbage collection occurs, but it might not be beacuse of the objects that are created, it could be for any reason.

If you just want the garbage collection to occur, the GC.Collect method would do that.

However, there is rarely any reason to force a garbage collection. Collections will occur when needed, you will usually only degrade performance by forcing collections.

Guffa
Won't `new String('*', 1000)` return the same instance every time?
Elisha
Am I the only one who had to double read the variable name `cnt`?
Sam
@Elisha: No, strings are not interned unless you specifically intern them.
Guffa
A: 

See this SO question: Best Practice for Forcing Garbage Collection in C#

adrift
A: 

Can't you just call GC.Collect()

http://msdn.microsoft.com/en-us/library/system.gc.collect.aspx

Adam Butler
why the downvote?
Adam Butler
Well, you've duplicated **three** earlier answers without adding anything, could have something to do with it. Sometimes duplication occurs, but when you're the fourth (third, or even second in my book, but I am just one person), you should probably consider that "delete" link.
T.J. Crowder
+3  A: 

Apart from using GC.Collect: if you really need the garbage collection to be 'done' synchronously (blocking in other words), you could use GC.WaitForPendingFinalizers: http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx

Note that this may very well unnecessarily freeze your application temporarily.

The link also provides code that could trigger the garbage collector.

ChristopheD
+1 Scary quote, though: *"The thread on which finalizers are run is unspecified, so there is no guarantee that this method will terminate."* Not sure I could bring myself to call a method which is *documented* as possibly never coming back... :-)
T.J. Crowder