#931 – Objects with Finalizers Take Longer to Garbage Collect

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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

5 Responses to #931 – Objects with Finalizers Take Longer to Garbage Collect

  1. Pingback: Dew Drop – September 16, 2013 (#1,624) | Morning Dew

  2. Clay says:

    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.

  3. Pingback: Windows Store Developer Links – 2013-09-17 | Dan Rigby

  4. Pingback: #937 – Forcing a Garbage Collection | 2,000 Things You Should Know About C#

  5. Pingback: Object Disposal and BackgroundWorker Object in .NET | Sean's Stuff

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: