473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

When does the Finalize event of a class fire?

VB 2005.

I have the following code in a Sub.

Dim oFred As SillyClass
oFred = New SillyClass
oFred.Gloop()
oFred = Nothing

Exit Sub

The Finalize code in the class does not fire until the .exe is closed.
I expected it to fire at either:-
- the point at which oFred is set to Nothing, or
- the point at which we exit from the Sub, and variable oFred goes out of
scope.

What are the rules governing when the Finalize code runs?

Thanks

Barry
Jun 27 '08 #1
15 7390
Neither, this is called by the garbage collector on which .NET memory
management is based.

Try :

http://msdn.microsoft.com/en-us/library/0s71x931.aspx for finalize details
and more generally http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx for
the Garbage Collector itself...

--
Patrice
"Barry Flynn" <Anonymousea écrit dans le message de groupe de discussion :
uN**************@TK2MSFTNGP02.phx.gbl...
VB 2005.

I have the following code in a Sub.

Dim oFred As SillyClass
oFred = New SillyClass
oFred.Gloop()
oFred = Nothing

Exit Sub

The Finalize code in the class does not fire until the .exe is closed.
I expected it to fire at either:-
- the point at which oFred is set to Nothing, or
- the point at which we exit from the Sub, and variable oFred goes out of
scope.

What are the rules governing when the Finalize code runs?

Thanks

Barry

Jun 27 '08 #2
Barry Flynn wrote:
The Finalize code in the class does not fire until the .exe is closed.
I expected it to fire at either:-
- the point at which oFred is set to Nothing, or
- the point at which we exit from the Sub, and variable oFred goes out of
scope.
Read up on "Garbage Collection" and the "IDisposable" pattern (or
Interface).

The CLR will reclaim only Managed memory /if/ and when it needs to, i.e.
when it needs to allocate some /more/ memory to something else. For a
program as simple as the one you showed, it doesn't need to do so, so
the memory doesn't get reclaimed until the program dies.

Setting objects to Nothing in Visual Basic is [usually] a pointless
exercise. It does /not/ get rid of the object but, instead, just makes
it "eligible" for Garbage Collection which, as I've said, might take a
very long time to run and get rid of the object.

Please Note: This usually /does not/ matter.

You only need to "worry" about it if your class uses any "unmanaged"
resources. A simple example might be a file that you use for munging
data around through some external tool; when your object dies, it really
ought to "tidy up" (delete) its temporary file.

For things like this, implement the IDisposable pattern and have your
"client" code call the object's Dispose() method.

Class X
Implements IDisposable

Sub Finalize()
Me.Dispose( False )
End Sub

Public Sub Dispose()
Implements IDisposable.Dispose

Me.Dispose( True )

' If we've cleaned up after ourselves, we can /lighten/
' GC's load by telling it to /ignore/ this object.
GC.SuppressFinalize( Me )

End Sub

Private Sub Dispose( ByVal disposing as Boolean )
If ( disposing ) Then
' Called from "our" code, we can kill off "related" objects

If ( File.Exists( m_sTemporaryFilePath ) ) Then
File.Kill( m_sTemporaryFilePath )
End If

End If

' Otherwise, we've been called from the finaliser,
' Other objects may or may not exist,
' so we only do our /own/ stuff.

End Sub

End Class
Module Y
Sub Main()
Dim x2 as New X()

x2.DoSomething()
x2.DoSomethingElse()
x2.DoSomethingElseAgain()

' then, the important bit
x2.Dispose()

End Sub
End Module

HTH,
Phill W.
Jun 27 '08 #3
Barry,

It is managed code, which means as you have to bother about finalizing, then
start again with your design.

The clean up is done by Net, as it is fact so difficult that you cannot do
that anymore yourself.

Cor

"Barry Flynn" <Anonymouseschreef in bericht
news:uN**************@TK2MSFTNGP02.phx.gbl...
VB 2005.

I have the following code in a Sub.

Dim oFred As SillyClass
oFred = New SillyClass
oFred.Gloop()
oFred = Nothing

Exit Sub

The Finalize code in the class does not fire until the .exe is closed.
I expected it to fire at either:-
- the point at which oFred is set to Nothing, or
- the point at which we exit from the Sub, and variable oFred goes out of
scope.

What are the rules governing when the Finalize code runs?

Thanks

Barry

Jun 27 '08 #4
The clean up is done by Net

Dangerous words Cor.

I know that this is a very sore topic between us, but in my opinion
you can help .NET do the clean up by calling Dispose on IDisposable
objects when you're done with them and not letting this be done by the
GC / Finalizer.

Thanks,

Seth Rowe [MVP]

Jun 27 '08 #5
>The clean up is done by Net
>>
Dangerous words Cor.

I know that this is a very sore topic between us, but in my opinion
you can help .NET do the clean up by calling Dispose on IDisposable
objects when you're done with them and not letting this be done by the
GC / Finalizer.

Since Moving to VB 8 and then 9 in quick succession I have found the new
"Using" keyword very useful here.

--
Rory
Jun 27 '08 #6
Thanks for the various responses.

Barry
"Barry Flynn" <Anonymousewrote in message
news:uN**************@TK2MSFTNGP02.phx.gbl...
VB 2005.

I have the following code in a Sub.

Dim oFred As SillyClass
oFred = New SillyClass
oFred.Gloop()
oFred = Nothing

Exit Sub

The Finalize code in the class does not fire until the .exe is closed.
I expected it to fire at either:-
- the point at which oFred is set to Nothing, or
- the point at which we exit from the Sub, and variable oFred goes out of
scope.

What are the rules governing when the Finalize code runs?

Thanks

Barry


Jun 27 '08 #7
Seth,

There is a differnce what you can do and what you should do.

What is the sense to clean up memory and resources as there is more then
enough available, while the managed code can do that in fact better and
cleaner.

It sounds for me a little bit the same as that you remove every evening
almost all the petrol out of your car to be able to go to a gas station in
the morning and be able to get every morning the same new quantity of
petrol.

However, if you like to do that, feel free.

Cor

"rowe_newsgroups" <ro********@yahoo.comschreef in bericht
news:33**********************************@x41g2000 hsb.googlegroups.com...
>The clean up is done by Net

Dangerous words Cor.

I know that this is a very sore topic between us, but in my opinion
you can help .NET do the clean up by calling Dispose on IDisposable
objects when you're done with them and not letting this be done by the
GC / Finalizer.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #8
On 2008-06-14, Cor Ligthert[MVP] <no************@planet.nlwrote:
Seth,

There is a differnce what you can do and what you should do.

What is the sense to clean up memory and resources as there is more then
enough available, while the managed code can do that in fact better and
cleaner.

It sounds for me a little bit the same as that you remove every evening
almost all the petrol out of your car to be able to go to a gas station in
the morning and be able to get every morning the same new quantity of
petrol.

However, if you like to do that, feel free.

Cor
What exactly are you suggesting here, Cor? That you should not call dispose
on disposable objects? If so, that seems like just plain bad advice to me.
Not only can you tie up unmanaged resources for longer periods then are
necessary, you can actually affect the performance of GC itself. Considering
that most disposable objects also override finalize.... If you do not call
dispose on them, then the GC will and that means that gc will have to interact
with that object twice to remove it. I realize, there are some objects that
derive from component that it's probably safe to ignore dispose - but, I
certainly wouldn't suggest that as normal or best practice...

If I misunderstood your suggestion, then please forgive my rant :)

--
Tom Shelton
Jun 27 '08 #9
You mistunderstood my message however, something you added is something I
disagree with you.

In my idea you should use dispose as it is needed and were it is made for,
not because that is there given because it is inheritted by component.

I have too often read the sentence, it is a method in the class so it is
best practise to use it. I get the idea you write that in a way.

It is for me not good practise to use an method, just because that it is in
a class. I don't use ToString as I need to count two integers.

Dim a as integer = 1
Dim b as integer = 1
Dim C as integer = Cint(a.ToString) + Cint(b.ToString)

In that way as is written so often wrong about dispose this above would be
also best practise. For me it's not.

There is in my idea nothing wrong with doing someting twice as that makes
the total proces shorter, especially as that is done at the best managed
moment.

(This including that the "using" automaticly executes dispose, what makes
the using at least more simple to describe)

Which does not mean that I don't use dispose, I always use that for drawing
objects, dialogforms, etc.

Just my idea.

Cor


"Tom Shelton" <to*********@comcastXXXXXXX.netschreef in bericht
news:wI******************************@comcast.com. ..
On 2008-06-14, Cor Ligthert[MVP] <no************@planet.nlwrote:
>Seth,

There is a differnce what you can do and what you should do.

What is the sense to clean up memory and resources as there is more then
enough available, while the managed code can do that in fact better and
cleaner.

It sounds for me a little bit the same as that you remove every evening
almost all the petrol out of your car to be able to go to a gas station
in
the morning and be able to get every morning the same new quantity of
petrol.

However, if you like to do that, feel free.

Cor

What exactly are you suggesting here, Cor? That you should not call
dispose
on disposable objects? If so, that seems like just plain bad advice to
me.
Not only can you tie up unmanaged resources for longer periods then are
necessary, you can actually affect the performance of GC itself.
Considering
that most disposable objects also override finalize.... If you do not
call
dispose on them, then the GC will and that means that gc will have to
interact
with that object twice to remove it. I realize, there are some objects
that
derive from component that it's probably safe to ignore dispose - but, I
certainly wouldn't suggest that as normal or best practice...

If I misunderstood your suggestion, then please forgive my rant :)

--
Tom Shelton
Jun 27 '08 #10
On 2008-06-14, Cor Ligthert[MVP] <no************@planet.nlwrote:
You mistunderstood my message however, something you added is something I
disagree with you.

In my idea you should use dispose as it is needed and were it is made for,
not because that is there given because it is inheritted by component.
But, unless you dive in with reflector, or it is explicitly documented, how do
you know that it isn't necessary? It is definately necessary for many objects
that do inherit from component - how do you distinguish? And what if the
implementation of said object changes in the future?

Sure, I know how to find out if the call is necessary or not - but I
prefere these sort of things to be a black box. I don't really want to know
it's implementation details. And in general, a few clock cycles to make the
call isn't going to noticibly impact the performance.
I have too often read the sentence, it is a method in the class so it is
best practise to use it. I get the idea you write that in a way.

It is for me not good practise to use an method, just because that it is in
a class. I don't use ToString as I need to count two integers.

Dim a as integer = 1
Dim b as integer = 1
Dim C as integer = Cint(a.ToString) + Cint(b.ToString)
That's sort of stretching the point, Cor. I am sure there aren't many people
who would do such a thing. But, I think the call to dispose falls in the
category of defensive coding and OOP's principle of information hiding...
In that way as is written so often wrong about dispose this above would be
also best practise. For me it's not.
Nobody is advocating calling methods just because they exist.
There is in my idea nothing wrong with doing someting twice as that makes
the total proces shorter, especially as that is done at the best managed
moment.
I'm not sure I get you here? Not calling dispose can negatively affect the
performance of the gc, because it normally has to make two passes to remove and
object that implements idispose if dispose insn't called... That has nothing
to do with being disposable of course - but the fact that most objects that
implement IDisposable also include a finalizer. How is that in anyway a good
thing?
(This including that the "using" automaticly executes dispose, what makes
the using at least more simple to describe)
Using is the same as calling dispose, yes. And I use it :)
Which does not mean that I don't use dispose, I always use that for drawing
objects, dialogforms, etc.

Just my idea.
With which, I respectfully disagree :)

--
Tom Shelton
Jun 27 '08 #11
Tom,
>
I'm not sure I get you here? Not calling dispose can negatively affect
the
performance of the gc, because it normally has to make two passes to
remove and
object that implements idispose if dispose insn't called... That has
nothing
to do with being disposable of course - but the fact that most objects
that
implement IDisposable also include a finalizer. How is that in anyway a
good
thing?
I have seen that the GC works as the video processor is doing its job, I am
not completely sure of that, but as it is like that, it is in fact null
time, while your dispose takes needed proces time.

I agree that this is not for non video processes. I am working at one at the
moment, it runs at night, nobody cares if it takes 2 or 6 hours.

Cor

Jun 27 '08 #12
On Jun 14, 2:30 pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
Tom,
I'm not sure I get you here? Not calling dispose can negatively affect
the
performance of the gc, because it normally has to make two passes to
remove and
object that implements idispose if dispose insn't called... That has
nothing
to do with being disposable of course - but the fact that most objects
that
implement IDisposable also include a finalizer. How is that in anyway a
good
thing?

I have seen that the GC works as the video processor is doing its job, I am
not completely sure of that, but as it is like that, it is in fact null
time, while your dispose takes needed proces time.

I agree that this is not for non video processes. I am working at one at the
moment, it runs at night, nobody cares if it takes 2 or 6 hours.

Cor
I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong. I've
also seen this topic enough to know it doesn't really matter what
arguments we give, you aren't going to change your opinions. For this
reason I encourage readers of this thread who want the "full" debate
to do some searching in the archives.

However, just to restate my opinion, I believe you you should use
IDisposable every time unless you are absolutely sure that there is no
reason to call it (such as Cor's example of inherited Components).
This has nothing to do with calling the method simply because it
exists (such as Cor's attempt at saying this through his "ToString"
sample), it has to do with calling the method since the developer's
say it is necessary (even though sometimes it is not). Personnally, I
would not allow code to go to production that does not use the Dispose
functionality where it is needed (db transactions, streams, graphics,
etc). This to me is a worse "no-no" than writing code without having
Option Strict turned on.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #13
Seth,

What do you want to say with your complete message. You write exactly how I
think and always write about it.

Even in this thread I have explicitly given situations where it should be
used.

But that as well as you create your own classes where IDispose is not
implemented and unmanaged resources are used.

But not as you always tells to dispose things as datatables, datasets or a
Sqlconnection (the latter has the dispose implemented in the close).

So what do you mean with this cheap agusing message what tells nothing then
some demagogie.
I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong
Cor
"rowe_newsgroups" <ro********@yahoo.comschreef in bericht
news:8f**********************************@c65g2000 hsa.googlegroups.com...
On Jun 14, 2:30 pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
>Tom,
I'm not sure I get you here? Not calling dispose can negatively affect
the
performance of the gc, because it normally has to make two passes to
remove and
object that implements idispose if dispose insn't called... That has
nothing
to do with being disposable of course - but the fact that most objects
that
implement IDisposable also include a finalizer. How is that in anyway
a
good
thing?

I have seen that the GC works as the video processor is doing its job, I
am
not completely sure of that, but as it is like that, it is in fact null
time, while your dispose takes needed proces time.

I agree that this is not for non video processes. I am working at one at
the
moment, it runs at night, nobody cares if it takes 2 or 6 hours.

Cor

I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong. I've
also seen this topic enough to know it doesn't really matter what
arguments we give, you aren't going to change your opinions. For this
reason I encourage readers of this thread who want the "full" debate
to do some searching in the archives.

However, just to restate my opinion, I believe you you should use
IDisposable every time unless you are absolutely sure that there is no
reason to call it (such as Cor's example of inherited Components).
This has nothing to do with calling the method simply because it
exists (such as Cor's attempt at saying this through his "ToString"
sample), it has to do with calling the method since the developer's
say it is necessary (even though sometimes it is not). Personnally, I
would not allow code to go to production that does not use the Dispose
functionality where it is needed (db transactions, streams, graphics,
etc). This to me is a worse "no-no" than writing code without having
Option Strict turned on.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #14
I'll do my best to answer your questions Cor, though I am having a bit
of trouble understanding some of what you are saying here.
What do you want to say with your complete message. You write exactly how I
think and always write about it.
The purpose of my message was to encourage readers to find the
previous threads where we have been over this.
Even in this thread I have explicitly given situations where it should be
used.
Again, many of the areas where we differ in opinion is whether or not
you should call Dispose if you do not know whether it is needed. We've
covered this in other threads, and so as to not waste time here I will
not go over it again.
But that as well as you create your own classes where IDispose is not
implemented and unmanaged resources are used.
Are you saying that I never reference unmanaged code in my projects?
If so you are a fool for assuming that I don't do this. And I assure
you that when I reference unmanaged code I implement IDisposable for
cleanup.
But not as you always tells to dispose things as datatables, datasets or a
Sqlconnection (the latter has the dispose implemented in the close).
Close and Dispose methods do different things. Again so as to not
waste time please read the following thread:

http://groups.google.com/group/micro...7e0a66a380a387
So what do you mean with this cheap agusing message what tells nothing then
some demagogie.
I have no idea what demagogie is, but I'll attempt to answer what I
think you are asking.
I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong
Not sure how I could get any clearer. You have listed many arguments
against using the IDisposable pattern as it was intended and I and
many others have shown you in previous threads where you are mistaken,
however I don't think you have ever changed your opinion. Remember
though Cor, just because you don't understand why you are wrong or you
refuse to admit you are wrong does not mean you aren't still wrong.

Some of the many things you have said which I believe are blatantly
wrong are:

That calling Dispose is the same as calling ToString()
The Garbage Collector cleans up any resource better without any
interaction by the developer
The Garbage Collector runs during "idle" times and never affects
performance
Dispose should be avoided unless you absolutely know it must be called
More I'm sure I've forgotten

Again, I'm not going to list rebuttals here, I encourage readers to
use the archives and find the previous threads and form their own
opinions. As for me, I'm dropping this thread.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #15
Seth,

snip
http://groups.google.com/group/micro...7e0a66a380a387
snip

In my idea you really should take some more time before you write something.
This was the situation in 2002 were was told that the connection dispose was
overloaded with the removal of the connection from the connectionpool. In
fact that has nothing to do with the dispose. To Implement Dispose to remove
*unmaneged resources* be done by this.

http://msdn.microsoft.com/en-us/libr...tw(VS.85).aspx

Luckely is IDisposable standard implemented in th by the designer created
templates like Form and Component where that is needed.

I have the ides, that what you are talking about all the time is the in my
idea the overloaded Dispose (boolean) method, which is strongly adviced not
to use on its own.

Things as DataTables don't need dispose because it is only a bunch in memory
objects, which have not any unmanaged resource.

By the way, the by you refered message is more times corrected by Pablo
Castro. Here just one I found. Maybe you could use Google in future and not
come direct with something you found from 2002, while giving the idea in
your message that I had to do with it while I was not.

http://groups.google.com/group/micro...11c0671640ad38

Maybe you can use Google to find something Jay B. Harlow wrote, he did made
before he wrote that forever the same discussions like you with me about
dispose.

Then he did some investigation in it and wrote the article. In that article
Jay B. made more clear where dispose should be used. His article was mainly
covering the aspects as I write about dispose. But he added some for me new
aspects, which I use when I write about dispose, by instance the dispose of
the dialogs, which I did but I did not know why before his article.

Cor


Jun 27 '08 #16

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

Similar topics

8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
4
by: Anatoly | last post by:
Put any control on web page. create Init event for ths control. Write Response.Write("here") inside this event. Compile\build\run. I never saw "here" string appear on web page. Why???
4
by: ryu | last post by:
I have a aspx page that loads a ascx file ie Web Control. My question is when does the postback of the web control occur? Is it when it is called via LoadControl? Or after the calling page's...
20
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,...
0
by: Charles Law | last post by:
Is there a way to get the MouseEnter event to fire for a user control that inherits from UpDownBase? The event is hidden, and does not fire when the mouse enters the control. TIA Charles
1
by: the friendly display name | last post by:
I am using .net 1.1 In the global.asax.cs file, there is this entry: protected void Application_AuthenticateRequest(Object sender, EventArgs e) as far as I know, it is wired with the ...
1
by: John Dalberg | last post by:
What causes a server event to fire when something happens on the client? Say a user changed the gridview's page index, the server side PageIndexChanged event is fires. What makes...
0
by: manywolf | last post by:
I have an aspx page that fires the page load event twice for every load. I tried every fix that was suggested in all the posts on this and other forums. None changed the behavior. After one post that...
7
by: ghd | last post by:
In Windows XP when does the JVM start (JRE version 1.4 and higher)? And when does it halt? Does the JVM start when we launch a java application (or by executing java classfile)? And does the JVM...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.