Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# Memory Question

Started by GeneDefekt Aug 29, 2007 at 1:23 PM 1 replies 2k views
Original Post
GeneDefekt
GeneDefekt
I am running through some C# tutorials and wanted to know a little about how C# handles memory. StringBuilder first = new StringBuilder(); StringBuilder second = first; first.Append ("hello"); first = null; Console.WriteLine (second); In the above code snipit I know that first and second are just handles to a StringBuilder object. My question is, what happens when you null out the variables first and second? If you null out first, second is still live so hello will still be printed to the screen. But if you null them both out in my mind all that has happened is a loss of handles with memory still floating around on the heap. Is this a mem leak?
Conner McCloud
Conner McCloud
Nothing happens when you null out both. Absolutely nothing.

At some point later, the system will go through and move all your live objects to a new spot in memory. Your StringBuilder isn't live, so it gets ignored. Once the move is complete, all the old memory is marked free and the memory is effectively reclaimed.
GeneDefekt
GeneDefekt
argh, :slaps head on comp:

Garbage collection i forgot.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.