When does the .net garbage collector run?
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 :-)
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.
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
garbage collection does not free up your pooled database connections. connections are returned to the pool when you close/dispose the connection object. the connection pool itself manages when it needs to open more connections and close them again based on how hard you are hitting your pool and your configuration of the pool in the connection string. garbage collection is not related to this in any way other than reclaiming the memory allocated to instances of SqlConnection. you are barking up the wrong tree in asking about garbage collection and getting (and accepted) answers that don't have anything to do with what you are after (if i understand your question correctly).
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?
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.
Two of the prominent reasons for GC to run are a) if generation 0 is full b) when heap is full. courtesy - CLR Via C#, Jeffrey Richter
精彩评论