Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering a .NET garbage collection externally

Is there a way to trigger a garbage collection in a .NET process from another process or from inside WinDBG?

There are the Managed Debugging Assistants that force a collection as you move across a native/managed boundary, and AQTime seems to have button that suggests it does this, but I can't find any documentation on how to do it.

like image 987
Ian G Avatar asked Sep 19 '08 11:09

Ian G


People also ask

Can you force garbage collection in C#?

You can force garbage collection either to all the three generations or to a specific generation using the GC. Collect() method. The GC. Collect() method is overloaded -- you can call it without any parameters or even by passing the generation number you would like to the garbage collector to collect.

What triggers garbage collection in C#?

Garbage Collection occurs if at least one of multiple conditions is satisfied. These conditions are given as follows: If the system has low physical memory, then garbage collection is necessary. If the memory allocated to various objects in the heap memory exceeds a pre-set threshold, then garbage collection occurs.

Can garbage collection be forced in net?

GC.Collect() Net Framework provides a new mechanism for releasing unreferenced objects from the memory ,this process is called Garbage Collection (GC). In some rare situations, forcing a Garbage Collection may improve your application's performance. The garbage collection (GC )class provides a GC.


2 Answers

Answered in another question :

Basically, use PerfView:

PerfView.exe ForceGC [ProcessName | Process ID] /AcceptEULA 

It's not intended for production use.

like image 132
Eric Boumendil Avatar answered Oct 04 '22 00:10

Eric Boumendil


Well... there's the immediate window. If you have the luxury of attaching to the process, I supposed you could manually GC.Collect in the immediate window.

Bigger question: why would you want to manually induce GC.Collect? It's a nasty habit, and indicative of much bigger design issues.

like image 23
David Hill Avatar answered Oct 04 '22 01:10

David Hill