when to release the object?

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
I am just wondering whether the following statement is true?

Any variable that is declared in a subroutine is released when the subroutine is done.
 
A variable is only a memory allocation, this is released at the end of the code block.

What the variable points to could be too if not another reference is added. For example you declare an X variable of type Label, assign it a new label instance then add this label to the form. When code block is finished the memory allocation of the X variable is released, but the Label control instance is kept because the form has a reference to it. The same goes for events, you could for example create a timer, only add event handler and start it. When variable allocation is released the event reference keeps the timer alive still.
 
I am just wondering whether the following statement is true?

Any variable that is declared in a subroutine is released when the subroutine is done.

False

Any variable heap data is scheduled for garbage colelction when there are no longer anywhere (in the entire app), any surviving stack references to it. Garbage collection causes the release of the data, and can occur at any time, but is not guaranteed to occur at a particular time
 
False

Any variable heap data is scheduled for garbage colelction when there are no longer anywhere (in the entire app), any surviving stack references to it. Garbage collection causes the release of the data, and can occur at any time, but is not guaranteed to occur at a particular time

Does this mean that in any of my subroutines/functions, whenever a variable that I have decleared in a subroutine or a function is useless, I should used command like VARIABLE_A = Nothing, thus to save the computer resources?
 
Does this mean that in any of my subroutines/functions, whenever a variable that I have decleared in a subroutine or a function is useless, I should used command like VARIABLE_A = Nothing, thus to save the computer resources?

No.

As soon as you leave the procedure VARIABLE_A = Nothing is called implicitly
If you anticipate that the procedure will run for a long time then you can of course call this yourself, but as I said before you dont really have any control over when the garbage colelctor will get round to picking up the trash
 
No.

As soon as you leave the procedure VARIABLE_A = Nothing is called implicitly
If you anticipate that the procedure will run for a long time then you can of course call this yourself, but as I said before you dont really have any control over when the garbage colelctor will get round to picking up the trash


I am sorry that I don't get what you mean by the last sentence...

Does that mean that I don't have to do anything about the variables that I have decleared within a subroutine/function?

Thanks!
 
There isnt any blanket rule I can give for you to follow, youre going to have to use your brain and apply the knowledge we've given here:

When the executing thread leaves a block of code (in C# this is easy to define.. a block is everything between { }, in VB its harder to explain.. but a block is anything that causes the IDE to INDENT your code) all variables declared within it are dissolved.
THis is called "scope" - when a variable falls out of scope, it is dissolved

Variables are just pointers to data in memory

When there are no more surviving pointers to that data, the data is put out to trash, but it isnt released straight away

Periodically, like in the real world the garbage truck comes to your house once a week, the Garbage Collector cleans up all the trash data. You have no control over when it does this, but rest assured that some very clever people at Microsoft has spent a long time figuring it out.

If you dont put your bins out, the garbage truck cant collect. If you set A_VARIABLE = Nothing, you are putting the bins out. You might decide to do this before you go on a long holiday (start performing a long operation) or you might not.. If you do, there isnt any guarantee that the garbage truck will come while youre away but if it does come it will clean up.


-

It might help you to know that it is very, very rare that I Dispose() of objects and set them to Nothing myself.. I do it for streams, and sockets as soon as I'm finished with them, because keeping reference can keep a lock on the operating system file meaning other programs cant use it for extended periods of time, but I dont do it for integers and crap like that

So, I guess a blanket rule would be: Close(), Dispose() and Set Nothing your streams, but the rest of the stuff, let .NET do it with scoping
 
Disposing an object and setting a variable to Nothing are two different things. A variable is a memory allocation, that variable and allocation can be used to assign a value or an object instance. If it's a value then that value "dies" when the variable goes out of scope, for object references the object does not end if another reference to it is made (like an event reference or a reference made to a control collection, examples mentioned). If you dispose the object that the variable points to then all resources made by that object is released (this could be file locks and unmanaged resources/memory), but the memory allocation made by the variable is held until the variable goes out of scope like before. The object it points to is also not fully released until there are no more references to it and the GC has done it's job. Disposing objects is very common and required in many cases, setting a variable to Nothing has in most cases no value.

To sum it up, for value type variables you are right, the variable and the value can be seen as one, when the variable ends the value ends. For reference type variables you have to see the variable and the object it may reference independently.
 
Back
Top