#931 – Objects with Finalizers Take Longer to Garbage Collect
September 16, 2013 5 Comments
The purpose of the garbage collector is to discover objects on the managed heap that are no longer reachable and to reclaim the memory occupied by these objects.
Once an object is discovered to be no longer reachable, the garbage collector checks to see if the object has a finalizer. If the object doesn’t have a finalizer, it is marked for collection and its memory is reclaimed by the garbage collector.
If an object has a finalizer, it will not be marked for garbage collection, but instead be marked for finalization. It will not get garbage collected during the current pass of the garbage collector. Instead, its finalizer will be run at some point after garbage collection finishes. It will then be eligible for collection during the next pass of the garbage collector.
Reclaiming memory for objects with finalizers therefore requires at least two passes of the garbage collector.
Pingback: Dew Drop – September 16, 2013 (#1,624) | Morning Dew
Another thing to note is that when an object is put in the finalization queue it gets promoted to the next generation, which will cause it to stay in memory for a lot longer.
Pingback: Windows Store Developer Links – 2013-09-17 | Dan Rigby
Pingback: #937 – Forcing a Garbage Collection | 2,000 Things You Should Know About C#
Pingback: Object Disposal and BackgroundWorker Object in .NET | Sean's Stuff