tags:

views:

67

answers:

3

I was fairly sure that I had read (in Richter's C# book) that objects that implement IDisposable and/or which have a Finalizer live until Generation 2. However, I can't find the reference, and my test application doesn't seem to support my belief.

Can anyone else confirm/deny?

+3  A: 

They certainly survive to Gen 1 since they are placed on a queue for the finalizer to get round to them. However if the finalizer gets to them before a Gen 1 collection they won't survive to Gen 2.

I haven't got that book for comparison but are you sure your not confusing this with items on the large object heap? The large object heap is only collected on a Gen 2 collection.

AnthonyWJones
Thanks, that supports what I'd seen in my testing. It is entirely possible that I'd confused this with the LOH - I obviously haven't been interviewing enough, as I'm starting to forget stuff like this :-)
endian
+1  A: 

As Anthony says, finalization delays garbage collection (i.e. the object will typically be promoted while it waits for the finalizer to run).

Implementing IDisposable on its own has no effect on garbage collection - the GC doesn't "know" about IDisposable.

Jon Skeet
A: 

Something I wanted to add: a properly implemented IDisposable pattern should call

GC.SuppressFinalize(this);

That way, the garbage collector won't have to wait until the object is finalized. See here.

Thorarin