.NETGURU
Performance... Need clarification
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.performance.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

G.Ashok
Hi,

Is it necessary to dispose the objects created in procedure be disposed
manually? Why can't we just rely on the GC to do the job?

For example:

I have this:

Sub SomeSub1()

'...some code...
If IsSubgroupOf(code, GetDataSet()) Then
'...
End If

'Here I cannot dispose the object passed above (Inline)
'This object is pushed on the stack during the parameter and
'will be disposed by the GC later

End Sub

Sub SomeSub2()

Dim ds As DataSet = GetDataSet()

'...some code...

If IsSubgroupOf(Me.ListData.Rows(0)("Code"), ds) Then
Me.ListData.Rows(0)("Type") = 1
ElseIf IsSubgroupOf(Me.ListData.Rows(0)("Code"), ds) Then
Me.ListData.Rows(0)("Type") = 2
ElseIf IsSubgroupOf(Me.ListData.Rows(0)("Code"), ds) Then
Me.ListData.Rows(0)("Type") = 3
ElseIf IsSubgroupOf(Me.ListData.Rows(0)("Code"), ds) Then
Me.ListData.Rows(0)("Type") = 4
End If
....

' Here I can dispose the object manually
ds.Dispose()
ds = Nothing

End Sub

Function GetDataSet(ByVal filter As String) As DataSet

Dim ds As New DataSet()
' Retrieve the data as per the filter passed from the data source...
'...
Return ds

End Function

Function IsSubgroupOf(ByVal code As String, ByVal ds As DataSet) As Boolean

'...
'I cannot dispose the object (ds) here as it is used by the caller
SomeSub1() repeatedly

End Function

I came to know that putting the GC in pressure is little performance
penalty. Which approach is better. If GC can do the job then why should I
manually dispose the object in SomeSub2(). If I don't dispose it manually
will it slows down the execution?

If I want to dispose the object in SomeSub1() manually then I need to
declare a local variable to hold it like in SomeSub2().

Can anybody through some thoughts on this?

Regards,
....@shok
------------------------------------------------------------------------
"It is beautiful for an engineer to shape and design
the same way that an artist shapes and designs.
But whether the whole process makes any sense,
whether men become happier - that
I can no longer decide." - Rudolph Diesel

Reply to this message...
 
    
Bob Grommes
IDisposable objects have references, directly, or indirectly, to unmanaged
resources. The CLR takes a non-deterministic approach to freeing up unused
objects, meaning it does so when it gets around to it, at no particular
time. Time-sensitive resources therefore have to be explicitly released.
If you Google on the following you'll find extensive discussions of the whys
and wherefors of this:

deterministic non-deterministic .NET GC

--Bob

"G.Ashok" <Click here to reveal e-mail address> wrote in message
news:%23BH%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
G.Ashok <Click here to reveal e-mail address> wrote:
[Original message clipped]

Things which implement IDisposable either contain unmanaged resources
or references to other objects which contain unmanaged resources. Those
unmanaged resources aren't watched by the GC, so you may run out of
those resources (or not be able to reuse the existing resource) until
you have disposed of the object.

For instance, if you have a FileStream open to read a file, but you
later want to write to the same file, unless you've manually closed or
disposed the stream, you may or may not be able to open the file for
writing, depending on the GC.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
G.Ashok
Thanks all you.

Regards,
....Ashok

"G.Ashok" <Click here to reveal e-mail address> wrote in message
news:#BH#Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Chris Lyon [MSFT] (VIP)
Hi Ashok

You'll want to call Dispose manually for a few reasons:
1- The GC does not call Dispose() itself, unless there is a call to it in the object's finalizer
2- Since the GC does not run finalizers right away, you'll be holding on to whatever unmanaged resources Dispose should be releasing for an undetermined amount of time.
3- When an object is eligible for finalization, it is put on an internal finalization queue, and promoted a generation, so it actually hangs around in memory longer than you'd
expect. And that includes not only those unmanaged resources, but any managed objects your object has references to.

So I would stronly advise you to call Dispose manually (in C# you can also use the "using" construct).

Be sure to read this article on proper Dispose pattern implementation (such as suppressing finalizers in your Dispose, to avoid #3 above):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconfinalizedispose.asp

Hope that helps
-Chris

--------------------
[Original message clipped]

--

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.

Reply to this message...
 
 
System.Data.DataSet
System.IDisposable
System.IO.FileStream




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification