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