By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Performance... Need clarification

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
the only time you really need to call dispose on an object it when that
object is utilizing an external or managed resource, i.e. database
connection, file stream, etc. other than that, you really don't have to
worry too much about how the gc works. and, setting an object to nothing
doesn't release any resources.
"G.Ashok" <gw******@hotmail.com> wrote in message
news:u8*************@TK2MSFTNGP11.phx.gbl...
| 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
|
|
Nov 20 '05 #2

P: n/a
Thanks Steve

Regards,
....Ashok

"steve" <a@b.com> wrote in message
news:10*************@corp.supernews.com...
the only time you really need to call dispose on an object it when that
object is utilizing an external or managed resource, i.e. database
connection, file stream, etc. other than that, you really don't have to
worry too much about how the gc works. and, setting an object to nothing
doesn't release any resources.
"G.Ashok" <gw******@hotmail.com> wrote in message
news:u8*************@TK2MSFTNGP11.phx.gbl...
| 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
|
|

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.