MyObject = New Object memory use
-
A question I should have probably wondered about a little earlier, but anyway: Let's say I dim an object, for example a dataset, fill that object with data, and use the object, but do not explicitly clear/empty it after use. Then reuse it like:
MyObject = New Object
Will that automatically clear/empty the data from it, that I filled it with earlier? Or does this depend on the object? Perhaps the important question here is: what is best practice? Can I get problems by not explicitly clearing/emptying an object before reusing it?
My advice is free, and you may get what you paid for.
-
A question I should have probably wondered about a little earlier, but anyway: Let's say I dim an object, for example a dataset, fill that object with data, and use the object, but do not explicitly clear/empty it after use. Then reuse it like:
MyObject = New Object
Will that automatically clear/empty the data from it, that I filled it with earlier? Or does this depend on the object? Perhaps the important question here is: what is best practice? Can I get problems by not explicitly clearing/emptying an object before reusing it?
My advice is free, and you may get what you paid for.
Hi, assuming VB.NET, if you do
[1] Dim MyObject As Object
[2] MyObject = New Object ' object 1
[3] MyObject = New Object ' object 2then object 1 will become collectible when line [3] executes, meaning the garbage collector will collect object 1 the next time it runs (normally it runs when a future memory allocation cannot be satisfied without cleaning up memory first). And this is fine, unless it is a type that implements Dispose or Close, as in:
[1] Dim MyObject As Font
[2] MyObject = New Font(...) ' object 1
[4] MyObject = New Font(...) ' object 2now you should insert
[3] MyObject.Dispose()
to make sure the unmanaged resources that might be held by the object get released right away (or the file or stream or whatever gets closed). If you don't Close/Dispose objects that support it, your app will use more memory and other system resources, and its behavior may deteriorate. Yes, the garbage collector will still find those objects, and its finalizer thread will eventually Dispose of them, but that typically will be much later. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, assuming VB.NET, if you do
[1] Dim MyObject As Object
[2] MyObject = New Object ' object 1
[3] MyObject = New Object ' object 2then object 1 will become collectible when line [3] executes, meaning the garbage collector will collect object 1 the next time it runs (normally it runs when a future memory allocation cannot be satisfied without cleaning up memory first). And this is fine, unless it is a type that implements Dispose or Close, as in:
[1] Dim MyObject As Font
[2] MyObject = New Font(...) ' object 1
[4] MyObject = New Font(...) ' object 2now you should insert
[3] MyObject.Dispose()
to make sure the unmanaged resources that might be held by the object get released right away (or the file or stream or whatever gets closed). If you don't Close/Dispose objects that support it, your app will use more memory and other system resources, and its behavior may deteriorate. Yes, the garbage collector will still find those objects, and its finalizer thread will eventually Dispose of them, but that typically will be much later. :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Hi Luc, Thanks, that is really good to know. So far I just assumed that once I reused the object, it would automatically also dispose of the first object and all related data. So from your explanation, can I surmise that it would be good practise to change my current code (simplified) from:
Dim ds As DataSet = Nothing
ds = New DataSet("DataSet1")
'Run a query and fill the dataset here
'Run some loops, and do some stuff to the data
ds = New DataSet("DataSet1")
'Run another query and fill the dataset here
'Run some loops, and do some stuff to the datato
Dim ds As DataSet = Nothing
ds = New DataSet("DataSet1")
'Run a query and fill the dataset here
'Run some loops, and do some stuff to the data
ds.Clear() 'Would this even be necessary ?
ds.Dispose()
ds = New DataSet("DataSet1")
'Run another query and fill the dataset here
'Run some loops, and do some stuff to the dataIn other words, my app would at least use less resources while it is running? Cheers, Johan
My advice is free, and you may get what you paid for.
-
Hi Luc, Thanks, that is really good to know. So far I just assumed that once I reused the object, it would automatically also dispose of the first object and all related data. So from your explanation, can I surmise that it would be good practise to change my current code (simplified) from:
Dim ds As DataSet = Nothing
ds = New DataSet("DataSet1")
'Run a query and fill the dataset here
'Run some loops, and do some stuff to the data
ds = New DataSet("DataSet1")
'Run another query and fill the dataset here
'Run some loops, and do some stuff to the datato
Dim ds As DataSet = Nothing
ds = New DataSet("DataSet1")
'Run a query and fill the dataset here
'Run some loops, and do some stuff to the data
ds.Clear() 'Would this even be necessary ?
ds.Dispose()
ds = New DataSet("DataSet1")
'Run another query and fill the dataset here
'Run some loops, and do some stuff to the dataIn other words, my app would at least use less resources while it is running? Cheers, Johan
My advice is free, and you may get what you paid for.
Calling Dispose()/Close() when done with an object that has such method is good. Calling Clear() or Flush() or ... typically doesn't change a thing when immediately followed by Dispose/Close. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Calling Dispose()/Close() when done with an object that has such method is good. Calling Clear() or Flush() or ... typically doesn't change a thing when immediately followed by Dispose/Close. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Alright, great ! Thanks a bundle for the enlightenment :)
My advice is free, and you may get what you paid for.
-
Alright, great ! Thanks a bundle for the enlightenment :)
My advice is free, and you may get what you paid for.
you're welcome. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.