How to release objects and classes

marco.uk

New member
Joined
Jul 6, 2006
Messages
1
Programming Experience
3-5
Hi all,

I'm quite new to VB .NET, so please excuse this very basic questions.

I developed the last few years in Borland Delphi and while some things are quite similar, I'm not sure how to release classes and objects correctly. Hopefully you guys can help me.

My questions are:

1. How to release a simple object, e.g. ArrayList. Currently I use the following code:

VB.NET:
Private Sub LoadData
  Dim objList As ArrayList

  ' create object
  objList = New ArrayList

  ' do something with the list
  
  ' release object
  objList = Nothing
End Sub
In Borland Delphi I would call the Free method (objList.Free), but there is no Free method in VB. Do I release the ArrayList correctly?


2. How to release a class?

VB.NET:
Public Class DataImport

    ' Import Log
    Private m_ImportLog As TextFile
    
' Constructor
    Public Sub New()
        ' Create objects
        m_ImportLog = New TextFile("C:\IMPORT.TXT")
    End Sub

    ' Destructor
    Protected Overrides Sub Finalize()
        ' Release objects
        m_ImportLog = Nothing
    End Sub
End Class
I create and release the class in the following way:

VB.NET:
Private Sub StartDataImport
   Dim objDataImport As DataImport

   objDataImport = New DataImport

   ' do something
  
   ' release object
   objDataImport = Nothing
End Sub
Is this correct?

I would really appreciate if somebody could help me.

Thanks a lot for your help,

Marco
 
Last edited:
The freeing of Objects is done automaticly by the CLR Garbage Collector, so the the code above is fine.
It is possible to force the GC if you wish, using the finalize intrenal sub.
 
What fomhoire has said is not completley correct. The basic principle of garbage collector is that it will release the resources used by an object only if it has gone out of scope or has been disposed of. If you have some objects that you have finished with, but still a has 'pointer attached', and it hasn't been 'disposed' of then it will be promoted to the next generation. By that i mean that it will be slightly longer lived and use memory that could be potentially used elsewhere. The danger here is that if you have a routine that uses such an object recursively you could end up with a rather large memory leak leading to the end of all life as we know it.:)
So to my mind always call dispose on objects that you have finished with (so long as they implement the idisposable interface) and if there are varaibles etc that you think may be hanging around for a while then they should be set to nothing. The GC will not collect objects that still have a purpose. Ultimately it is the job of the CLR to release allocated memory, and there is no way to control this.
When creating classes/object of your own consider the resources that your object will use and you may find that you will need to implement the idisposable interface in your own class.
If you want to force the garbage collector to reclaim memory then (which i don't reccommend) don't use the finalize method just call

VB.NET:
GC.collect
This method is overloaded to recieve an argument which pertains to the generation you would like it to collect. But my advice would be to leave the GC alone it does a good job anyway. Calling finalize could hamper your apps performance if you are unsure of how the GC works. Finalize can sometimes cause an extra round trip for the GC.

Hope this helps.
 
In java, we are minded not to instruct the garbage collector what to do - when there are no survivng references to an object it is scheduled for garbage collection. The GC will come along at some indeterminate point and clear it up, as an idle time process, or when resources are running low

Though i dont claim to know if .net GC works in the same way, I'd imagine it requires the minimum of fiddling..


MARCO:
So to answer the OP's question - be mindful of where you put variable references.. The more things have a reference to on of your objects, the harder it will be to get rid of.. In VB, you can either let it go out of scope, or deliberately set it to Nothing, which is VB's equivalent of null

In your example, marco, setting the object to nothing is moot, becauuse it goes out of scope immediately after.. an arraylist that goes out of scope will cause all the objects within it, that have no surviving references elsewhere, to also become garbage..
 
Back
Top