views:

243

answers:

5

When I debug .net code, how can I know when the garbage collector runs?

I do not wish to control when the garbage collector runs. I only wish to be able to know when it is running. I have some code that is running out of resources. I know that the resources are not being used; I want to know when the GC is running to free them.

Oh, and the resources I have in mind are connections from a Sql connection pool, not memory :-)

+5  A: 

You shouldn't, in general, ever worry or think about when the GC runs.

The Garbage collector will run as needed, at an indeterminate time chosen by the runtime.

If you want finer grain control over the GC (which I don't recommend except in very specific circumstances), you can use GC.AddMemoryPressure and GC.RemoveMemoryPressure. These will not force the GC to run, but rather hint at it that there is other memory in play other than the CLR-allocated, managed memory. This will potentially cause it to run more frequently, which can be useful if you're allocating large blocks of memory in native code.

There is no direct API to track GC runs. (For example, the GC class does not contain any events notifying of garbage collection occuring.) The only direct way to know, specifically, when the GC is executing is to use the profiling API.

Reed Copsey
I've found some documentation that indicates that .net keeps track of memory and optimizes GC runs based on that. Does that mean I'm out in the cold if I need GC to run to free connections to a SQL server?
Rice Flour Cookies
@Rising Star: No. A SQL Connection is a separate issue from memory. You can close it manually via conn.Close(): http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx
Reed Copsey
Hrm... Why the downvoting here?
Reed Copsey
+2  A: 

In .NET framework, GarbageCollector runs you.

In all seriousness, .Net is set up to optimize GC. It may occur at the end of a method call, or it might run for a day without needing to be GC'd. Is there a specific reason you need to know when it's collected?

taylonr
+1  A: 

Whenever it feels like it

The .NET garbage collector is a generational garbage collector. There's definitely a method to the madness but it's not exactly something you can accurately predict.

Evan Plaice
+1  A: 

You have to use .NET 4.0, what you are asking for isn't supported in eariler versions.

Essentially you call the WaitForFullGCApproach and the WaitForFullGCComplete methods in a loop. WaitForFullGCApproach will block until you see a GC, WaitForFullGCComplete will block until the GC is complete.

Please read this article carefully. If you use this method then you are responsible for makign sure the garbage collection actually occurs. If you mess this up you could break the GC and quickly run out of memory.

http://msdn.microsoft.com/en-us/library/cc713687.aspx

Jonathan Allen
A: 

You should dispose resources when not needed & close connections to database when you are done using them. This will most certainly keep memory issues away & you don't have to rely on garbage collector exclusively.

SoftwareGeek