none
Variable re-assigment, what happens under the hood? RRS feed

  • Question

  • I wanted to further my knowledge and confirm an assumption please. Considering the following code, what happens in the RAM please when the declared someObject object variable is retyped (if that's a word :cP).

    Does the equivalent of a realloc() call happen to resize the memory allocation already taken by the someObject object variable?
    Does the framework simply allocate a new area of memory and reclaim (well or leave - hopefully not though) the original memory allocation?
    Is there a more complicated calculation or routine performed and the framework could do either?

    Private Sub Form1_Load(ByVal sender As Object, _  
    ByVal e As System.EventArgsHandles Me.Load  
        Dim someObject As Object 
     
        someObject = New System.Text.StringBuilder  
        MessageBox.Show(someObject.GetType().ToString())  
     
        someObject = New Process  
        MessageBox.Show(someObject.GetType().ToString())  
    End Sub 


    Many thanks everyone!
    - Alex

    Alex Read
    Friday, June 27, 2008 5:52 AM

Answers

  • Hi Alex,

    Here someObject is local variable. someObject is pointer, located in stack. It is just a pointer, it is Int32 in Win32 systems and Int64 in Win64 systems. When you assign another object to this variable, pointer starts referencing another place in managed heap. Here when you assign someObject to new Process instance, StringBuilder instance become inaccessible and is valid target for garbage collector, and will be collected within next garbage collection.

    This is my understanding.
    Vitaliy Liptchinsky
    • Marked as answer by Bruno Yu Tuesday, July 1, 2008 4:00 AM
    Friday, June 27, 2008 7:36 AM
  • Yup.  Nothing happens to the memory allocated for the StringBuilder instance.  It will just sit in the heap, taking up space until the garbage collector runs.  It will discover that there are no references left to that instance and make no effort to preserve it.  
    Hans Passant.
    • Marked as answer by Bruno Yu Tuesday, July 1, 2008 4:00 AM
    Friday, June 27, 2008 11:53 AM
    Moderator

All replies

  • Hi Alex,

    Here someObject is local variable. someObject is pointer, located in stack. It is just a pointer, it is Int32 in Win32 systems and Int64 in Win64 systems. When you assign another object to this variable, pointer starts referencing another place in managed heap. Here when you assign someObject to new Process instance, StringBuilder instance become inaccessible and is valid target for garbage collector, and will be collected within next garbage collection.

    This is my understanding.
    Vitaliy Liptchinsky
    • Marked as answer by Bruno Yu Tuesday, July 1, 2008 4:00 AM
    Friday, June 27, 2008 7:36 AM
  • Yup.  Nothing happens to the memory allocated for the StringBuilder instance.  It will just sit in the heap, taking up space until the garbage collector runs.  It will discover that there are no references left to that instance and make no effort to preserve it.  
    Hans Passant.
    • Marked as answer by Bruno Yu Tuesday, July 1, 2008 4:00 AM
    Friday, June 27, 2008 11:53 AM
    Moderator
  • Hi guys!

    Many thanks for your time in explaining this. Now you've mentioned it, that makes perfect sense - now why on Earth didn't I think that through haha.

    Again many thanks for clearing this up for me!
    Alex Read
    Monday, June 30, 2008 5:16 AM
  • Another quick question: When exactly does the object become inaccessible and a valid target for garbage collecting?

    For example:
    LargeObject object = new LargeObject("Object1");  
    object.DoStuff();  
    // can "Object1" be garbage collected here?  
    object = new LargeObject("Object2"); 

    Will the GC collect Object1 when trying to allocate Object2? My current understanding is that yes, it will. right?


    Is it the same, in this case, since object is now a static field?:

    Class SomeClass {     
        static LargeObject object;     
        
        public SomeClass()      
        {     
            object = new LargeObject("Object1");     
        }     
        
        public static DoStuff()     
        {     
            object.DoStuff();     
        }     
        
        public static Realloc(string newName)     
        {     
            / can "Object1" be garbage collected here?        
            object = new LargeObject(newName);     
        }     
    }    
     

    What if the field was public? Non static? Is the reference always nulled under the hood before reallocation?



    Thanks
    ___________
    Vincent
    Wednesday, September 10, 2008 9:29 PM
  • It can only be collected one line after your comment.  Assuming you meant "object" to be a variable name, the LargeObject instance cannot be collected until there's no reference left to it.  In other words, when "object" gets a different value.  The odds that it will be collected right after you assign a new value to the reference variable are vanishingly small.  The garbage collector runs asynchronously from your program's execution.  A field being public or static makes no difference.  It doesn't get "nulled under the hood".
    Hans Passant.
    Wednesday, September 10, 2008 11:22 PM
    Moderator