473,485 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why Finalize isn't called immediately on final refcount

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?
Nov 16 '05 #1
4 1630
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
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?

Nov 16 '05 #2
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

"Julie" <ju***@nospam.com> wrote in message
news:eg**************@TK2MSFTNGP11.phx.gbl...
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?

Nov 16 '05 #3
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/is...2/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.

Nov 16 '05 #4
Chris Lyon [MSFT] wrote:
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.

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/is...2/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?


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1850
by: Tobias Langner | last post by:
I overloaded the operator= - but in the program, if the line p3=p1 gets executed, the program doesn't jump to the method. The class definition and the test code. template <class T, template...
8
21070
by: celeong | last post by:
Hi, anybody can help me with this. I've created a singleton class, and now wants to add destructor to it. I know we can implement the IDisposable and also overrides the Finalize method (from...
13
1636
by: | last post by:
I'm curious if anyone knows why the C# and VB.NET compilers don't automatically call Dispose() on objects that support IDisposable when they go out of scope. I asked a co-worker and his response...
3
4122
by: MuZZy | last post by:
Hi, Consider i have a class: class CTest { public static int Counter = 0; public CTest() { <...> Counter ++;
4
5057
by: Joe Abou Jaoude | last post by:
I m preparing to pass the 70-306 exam, so i downloaded Q & A from multiple sites. There's this question that really confuses me, coz i see that both answers A and C are both correct. Can anyone...
20
7464
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
2
993
by: myname | last post by:
Hello, I noticed that when there's a crash in my programs, the finalize methods of my objects are not called (the memory is freed, but some things are not done, like deleting files or tables)....
5
1678
dmjpro
by: dmjpro | last post by:
i know that the finalize method is called by JVM when an object is garbage collected. right??? but the ultimate memory release done by native OS. now my confusion is ... when it is called in...
8
1858
by: Rob | last post by:
This is a weird one... I've got a class called PageInfo that has the following finalize code: Protected Overrides Sub Finalize() MyBase.Finalize() Do While m_TempFolders.Count Dim TempPath As...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.