473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Freeing class resources

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
5 879

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" <iv*****@optonline.net> schrieb im Newsbeitrag
news:u2**************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2
* Ivan Weiss <iv*****@optonline.net> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4
On 2003-11-24, Ivan Weiss <iv*****@optonline.net> wrote:
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.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #5
* Ivan Weiss <iv*****@optonline.net> scripsit:
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).

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: | last post by:
Hi, What is the best method to close database connections? Is it enough if we just use the Close() method. Does the connection get released immediately? or do we have to do anything in the...
10
by: dmanhr | last post by:
Hi! I'd like to free-up - destroy (myself not GC) object n (Node) with implementing Dispose(). I have linked list: class Node{ CNode nextNode; object data;
6
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
15
by: yehoshua | last post by:
Is there anyway to explicitly tell the garbage collector you are done with an object? In my app I have a point where I am sure there should be no references to an set of objects (well i remove...
7
by: Adam | last post by:
I have a managed cpp wrapper. Im using this in a native dll as a static variable. I need to free this library when the dll is done being used. The perfect place to do this is DllMain for...
11
by: mark | last post by:
Please consider the following thread setup intended to free-up the UI while a huge process takes place: (declare delegates) (A B and C all global) Private Sub AB_Click(ByVal sender As...
0
by: Greg | last post by:
The intermediate resources file has name: <Root Namespace>.<form .resx file name>.resources (e.g. App1.Form1.resources) By the time this standard form code executes: ...
4
by: Atul Sureka | last post by:
Hi, I want to free the object memory in C# - like we do using 'delete' keyword in C++. Lets say I have an object of some class and I want to explicitly free the memory. C# do not have any free...
3
by: billq | last post by:
Hello, I am new to c# and have become confused about freeing resources. I need to know if using an objects Dispose method does the same thing as setting the object to null? Pen bluePen = new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.