|
-
Aug 1st, 2007, 09:16 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] What is = Nothing exactly
I want to keep a class level reference to an item. Like this.
Code:
Public Class MyClass
Private MyObject as Object
End Class
So I can just write MyObject = New Object when I need it. I know that my item is a reference item so the reference is stored in the stack, and the item is stored in the heap. So, when I put MyOBject = Nothing, does that remove the item from the heap, which would allow me to go ahead and create a new object when I need to? Or, does it remove the reference from the stack, and the item becomes unusable?
I found this on MSDN:
When you assign Nothing to an object variable, it no longer refers to any object instance. If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. The instance is terminated, and the memory and system resources associated with it are released, only after the garbage collector (GC) detects that there are no active references remaining.
When it says "it no longer refers to an object instance" I get that it means that the item that was created after the new word is used is no longer referenced, but does it mean that it no longer is associated with a type (which would not allow me to create a new instance)?
I hope I am asking that clearly. Thank you for your help.
Ben
Using Visual Basic 2005/2008
-
Aug 1st, 2007, 09:51 PM
#2
Re: What is = Nothing exactly
You don't set objects to Nothing. You set variables to Nothing. As you said, variables are stored on the stack. When you set a variable to nothing you are actually filling the memory location on the stack with all zeroes. If the variable is a value type then you are effectively resetting the value to its default, e.g. 0 for an Integer. If the variable is a reference type then it contains the memory address of an object, so you're setting the variable to refer to no object. The object it previously referred to still exists on the heap though. There may be other variables that refer to that same object, so you cannot destroy the object at that point. If there are no other variables referring to it then the object becomes eligible for garbage collection. That means that the .NET garbage collector will, at some point in the future, reclaim the memory occupied by the object.
Last edited by jmcilhinney; Aug 1st, 2007 at 09:56 PM.
-
Aug 2nd, 2007, 10:54 PM
#3
Thread Starter
Hyperactive Member
Re: What is = Nothing exactly
I got it now. MyObject = Nothing doesn't change the type that the reference was set to, it just releases its use of the object in memory so that the GC can collect it (if no other variable is referencing it). MyObject is still of type Object(or whatever it was assigned to using 'as'), it just isn't instantiated (I think I am using that terminology correctly). I got confused and was thinking that MyObject = Nothing removed the type of variable it was set to, but MyObject is still of type Object (in this example); it just isn't referencing an instance in memory. I think I have that right, but if you could confirm that I am right it would help remove any doubts that may creep up later.
Thank you for your help, jmcilhinney.
Ben
Using Visual Basic 2005/2008
-
Aug 2nd, 2007, 11:29 PM
#4
Re: What is = Nothing exactly
I think you have the idea, but your terminology is a bit off. To say that a variable is "not instantiated" is incorrect. To instantiate a type measn to create an instance of that type. When you set a variable to Nothing it means that the variable no longer refers to an object.
-
Aug 3rd, 2007, 04:52 PM
#5
Thread Starter
Hyperactive Member
Re: What is = Nothing exactly
Cool, thank you for clarifying that for me.
Ben
Using Visual Basic 2005/2008
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|