Why Finalize isn't called immediately on final refcount

  • Thread starter Thread starter Julie
  • Start date Start date
J

Julie

Does anyone know why the Finalize method (if present) isn't called immediately
after the instance refcount is set to 0?

It seems to me, that if it were, it would solve at least two issues:

- simplify the garbage collector (according to the documentation, the GC is
internally run twice)

- do away with a lot of /using/ code dealing w/ instances that wrap
externally accessible resources (files through StreamReader is a good example)

If Finalize was immediately called, objects like StreamReader could close open
handles w/o relying on the programmer to either specifically close or use
using, and it would simplify the GC internally.

Comments?
 
How do you know the CLR uses a reference count for GC? I think the GC runs
in another low priority thread that occasionally preempts the running thread
for gabbage collection??! Correct???

ben
 
Julie,

..NET objects are not reference counted.

If you Google for:

"non-deterministic finalization" .NET

.... you should get quite a few basic discussions of this. Try "IDisposal
interface" too, to learn about design patterns for managing the release of
unmanaged or memory-intensive resources under program control.

--Bob
 
Hi Julie

The Garbage collector doesn't use reference counting, instead uses a mark-and-sweep algorithm. For complete descriptions on the wasy the GC works, please check out the
following resources:

Maoni Stevens' blog (GC):
http://blogs.msdn.com/maoni/

Chris Brumme's Blog (Finalization):
http://blogs.msdn.com/cbrumme

Jeffrey Richter's (slightly outdated) articles on GC:
http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx

Hope that helps
-Chris

--------------------
Does anyone know why the Finalize method (if present) isn't called immediately
after the instance refcount is set to 0?

It seems to me, that if it were, it would solve at least two issues:

- simplify the garbage collector (according to the documentation, the GC is
internally run twice)

- do away with a lot of /using/ code dealing w/ instances that wrap
externally accessible resources (files through StreamReader is a good example)

If Finalize was immediately called, objects like StreamReader could close open
handles w/o relying on the programmer to either specifically close or use
using, and it would simplify the GC internally.

Comments?


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Chris said:
Hi Julie

The Garbage collector doesn't use reference counting, instead uses a mark-and-sweep algorithm.

Yeah, for some reason I was thinking that it was still based on refcounts and
then doing a lazy discard.

But, realizing that it uses mark and sweep, now the lazy finalization makes sense.

Thanks for the info an links.
 
Back
Top