My Photo
Name:
Location: United States

Tuesday, May 09, 2006

gc.collect

If you have a particular niche scenario where you have to call GC.Collect, consider the following:

Call GC.WaitForPendingFinalizers after you call GC.Collect. This ensures that the current thread waits until finalizers for all objects are called.
After the finalizers run, there are more dead objects (those that were just finalized) that need to be collected. One more call to GC.Collect collects the remaining dead objects.
System.GC.Collect(); // This gets rid of the dead objects
System.GC.WaitForPendingFinalizers(); // This waits for any finalizers to finish.
System.GC.Collect(); // This releases the memory associated with the objects that were just finalized.

0 Comments:

Post a Comment

<< Home