Garbage Collection - I already took it out!

  • Thread starter Thread starter Mythran
  • Start date Start date
M

Mythran

Group,

Got a quick question that I have asked in the past but still, no answer that I
think is sufficient for the question.


The question I have pertains to the following snippets:

Public Function Fetch() As DataTable
Dim bllObject As BLL.MyObject = New BLL.MyObject
Dim row As Schema.MyDataSet.MyDataRow

Try
' blah.
row = bllObject.Fetch()
Finally
' Cleanup.
bllObject.Dispose()
End Try

Return row
End Function

Compared To:

Public Function Fetch() As DataTable
Dim bllObject As BLL.MyObject = New BLL.MyObject
Dim row As Schema.MyDataSet.MyDataRow

Try
' blah.
row = bllObject.Fetch()
Finally
' Cleanup.
bllObject.Dispose()
bllObject = Nothing
End Try

Return row
End Function


The difference between the two snippets above is the fact that I am specifically
releasing the reference to bllObject by setting it to Nothing. In VB6, this was
important. I know that the garbage collector in .Net is more efficient and we do
not need to set our object references to Nothing, but is it "More" efficient if I
do set it to Nothing when I'm done with it and not let it release automatically
when it goes out of scope?

Basically, if I have a very large routine (that has already been broken down into
smaller routines, this is the largest one of the smaller ones), shouldn't I
release as soon as possible rather than let it hang around until it goes out of
scope?

Also, if I do set object references to Nothing right before they go out of scope
(before containing routine ends), will that be more efficient than the reference
going out of scope?


Thanks for the help, this has been bugging me for awhile :)

Mythran
 
The difference between the two snippets above is the fact that I am
specifically releasing the reference to bllObject by setting it to
Nothing. In VB6, this was important. I know that the garbage
collector in .Net is more efficient and we do not need to set our
object references to Nothing, but is it "More" efficient if I do set
it to Nothing when I'm done with it and not let it release
automatically when it goes out of scope?

Probably not the direct answer you are looking for, but a good resource
on the subject is the following MSDN article.

http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx

I would read this, and its second part as mentioned in the conclusion of
the article. It does a great job describing how the garbage collector
works in .NET.

For example, unless your bllObject is an "Expensive" object, you may gain
more performance from your app by not calling the Dispose method. See the
section labeled "Forcing an Object to Clean Up" near the end.
 
For example, unless your bllObject is an "Expensive" object, you may
gain more performance from your app by not calling the Dispose method.
See the section labeled "Forcing an Object to Clean Up" near the end.

er... let me expound on what I just said, this is based on whether the
object needs finalization or not. When possible, avoid objects that need
finalization immediately, they will force a GC and this can have
performance implications on your app if the GC is not needed otherwise.
And again, read the article, it describes more gracefully what I am
trying to say.
 
Mythran said:
Group,

Got a quick question that I have asked in the past but still, no answer that I
think is sufficient for the question.


The question I have pertains to the following snippets:

Public Function Fetch() As DataTable
Dim bllObject As BLL.MyObject = New BLL.MyObject
Dim row As Schema.MyDataSet.MyDataRow

Try
' blah.
row = bllObject.Fetch()
Finally
' Cleanup.
bllObject.Dispose()
End Try

Return row
End Function

Compared To:

Public Function Fetch() As DataTable
Dim bllObject As BLL.MyObject = New BLL.MyObject
Dim row As Schema.MyDataSet.MyDataRow

Try
' blah.
row = bllObject.Fetch()
Finally
' Cleanup.
bllObject.Dispose()
bllObject = Nothing
End Try

Return row
End Function


The difference between the two snippets above is the fact that I am specifically
releasing the reference to bllObject by setting it to Nothing. In VB6, this was
important. I know that the garbage collector in .Net is more efficient and we do
not need to set our object references to Nothing, but is it "More" efficient if I
do set it to Nothing when I'm done with it and not let it release automatically
when it goes out of scope?

Setting the reference to Nothing does not help in .NET, though it
shouldn't hurt. The JIT compiler will determine that bllObject is not
referenced after the Dispose() call, and it creates markers that the GC
uses to determine that the object is no longer referenced.
 
not need to set our object references to Nothing, but is it "More"
efficient if I
do set it to Nothing when I'm done with it and not let it release automatically
when it goes out of scope?

No. It is LESS efficient, as you're adding instructions to your code.
Setting an object variable to Nothing (null) does nothing but change your
variable value unnecessarily. The object doesn't just go away. The variable
simply no longer points to it. Therefore, you're adding processing cycles to
the app without effect.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top