2

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.

nwalke
  • 3,170
  • 6
  • 35
  • 60
kofucii
  • 7,393
  • 12
  • 51
  • 79
  • 1
    I doubt you'll get any answers on this one, but have you considered using `GC.Collect()`? ;) – recursive Sep 24 '10 at 21:36
  • 1
    In case you have issue with memory usage I can advice great tool SciTech Memory profiler. It allow to track what is happening and shows why object could not be destructed – st78 Sep 24 '10 at 21:36
  • Could you write what is a purpose of this kind of experiment? Such information could let people to find an answer different then: call explicitly *GC.Collect*. – Skarab Sep 24 '10 at 21:38
  • Is this for playing around or what do you want to achieve? – Dirk Vollmar Sep 24 '10 at 21:39
  • Re *"I've always tought, that GC can not be forced to occur programmaticaly."* That's true in some other environments (the most you can give them is a [suggestion](http://download.oracle.com/javase/6/docs/api/)), but Microsoft actually uses the word "force" in the documentation, so... – T.J. Crowder Sep 24 '10 at 21:41
  • @0xA3. No it's not playing around. I have an expensive operation to do, that requires a lot of memory. And freezing in the middle for GC, will be very bad. – kofucii Sep 24 '10 at 21:42
  • 1
    If you want to wait for the gc to finish you could also call GC.WaitForPendingFinalizers() afer collect to make sure any finalizable objects have performed cleanup – Adam Butler Sep 24 '10 at 21:50

7 Answers7

9

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

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    J.: As the top-voted answer, since the OP appears to require a synchronous GC, consider adding `GC.WaitForPendingFinalizers()` to your answer. – Michael Petrotta Sep 24 '10 at 22:14
4

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
  • 112,638
  • 29
  • 165
  • 179
  • +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 Sep 24 '10 at 22:14
2

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

Community
  • 1
  • 1
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
1

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
  • 687,336
  • 108
  • 737
  • 1,005
0

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

kidjan
  • 1,471
  • 14
  • 16
0

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
-3

Can't you just call GC.Collect()

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

Adam Butler
  • 3,023
  • 5
  • 35
  • 40
  • 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 Sep 24 '10 at 22:02