Freeing class resources

  • Thread starter Thread starter Ivan Weiss
  • Start date Start date
I

Ivan Weiss

I declared a class called database.

I have the following code:

Dim myDatabase as Database

myDatabase = Nothing

Does the nothing keyword free the object for garbage collection or do I
need to create a dispose method?

-Ivan
 
I'm not sure but I think nothing and dispose ist the same.

But the Garbage Collection is not free from Object at the time you dispose
it.

The GarbageCollection clears it at an unkown time.

You can clear the Garbage by hand with "gc.collect"
see MS Documentation.

If you do something in a loop this can be necessary
 
* Ivan Weiss said:
I declared a class called database.

I have the following code:

Dim myDatabase as Database

myDatabase = Nothing

It doesn't make sense to set the variable to 'Nothing', if it's a local
variable. If there are other references pointing to the same object,
the object will not be "destroyed" by the GC.
Does the nothing keyword free the object for garbage collection or do I
need to create a dispose method?

In order to implement the dispose method, your class should implement
the 'IDisposable' interface. 'Dispose' is used to free, for example,
_unmanaged_ ressources like file handles, Windows GDI handles, etc.
 
So in my case since I am declaring a local variable to an object of the
type Database (a class created by me) I do not need to worry about
setting it equal to nothing or any type of dispose method?

Does this mean once the function is finished the variable is released
for garbage collection? Also when I close a form does this implicitly
free all variables objects created within that form for garbage
collection?

-Ivan
 
So in my case since I am declaring a local variable to an object of the
type Database (a class created by me) I do not need to worry about
setting it equal to nothing or any type of dispose method?

That depends on what your Database class contains. If for instance, if
it has references to other disposable objects, such as database
connections, then you will probably want to implement IDisposable.
Does this mean once the function is finished the variable is released
for garbage collection? Also when I close a form does this implicitly
free all variables objects created within that form for garbage
collection?

The object is availabe for GC, as long as there are no other references.
You do understand though that GC is a non-deterministic process. In
other words, you can't be sure when it will run. Going out of scope
makes the object available for collection, but does not guarentee that
the object is going to be immediately collected.
 
* Ivan Weiss said:
So in my case since I am declaring a local variable to an object of the
type Database (a class created by me) I do not need to worry about
setting it equal to nothing or any type of dispose method?

If your database class contains unmanaged ressources and so on, it's
better to implement 'IDisposable'. You don't need to set the variable
to 'Nothing'.
Does this mean once the function is finished the variable is released
for garbage collection? Also when I close a form does this implicitly
ACK.

free all variables objects created within that form for garbage
collection?

Yes, if they are not referenced any more. Notice that the variables will be
destroyed when the GC runs the next time (that's indeterministic).
 
Back
Top