Forcing a Garbage Collection
Again, the whole purpose of the .NET garbage collector is to manage memory on our behalf. How-
ever, under some very rare circumstances, it may be beneficial to programmatically force a garbage
collection using GC.Collect(). Specifically:
• Your application is about to enter into a block of code that you do not wish to be interrupted
by a possible garbage collection.
• Your application has just finished allocating an extremely large number of objects and you
wish to remove as much of the acquired memory as possible.
If you determine it may be beneficial to have the garbage collector check for unreachable
objects, you could explicitly trigger a garbage collection, as follows:
static void Main(string[] args)
{
...
Do'stlaringiz bilan baham: |