Performance... Need clarification

  • Thread starter Thread starter G.Ashok
  • Start date Start date
G

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
 
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.


| 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
|
|
 
Thanks Steve

Regards,
....Ashok

steve said:
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.


| 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
|
|
 
Back
Top