String Init

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Alvin Bruney [MVP] (vapor)
Date: 02/09/04


Date: Mon, 9 Feb 2004 15:22:26 -0600

Sometime ago, i posted a concern about initializing string variables to null
v string.empty. I can't find the thread to reply to it so excuse this one.

I just now thought of this. Isn't it dangerous to initialize variables to
null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of
the routine i intialize an object to null. I intend to use this variable at
line 160 for example. At line 10, a garbage collection occurs. Is my code at
line 160 safe, the object is eligible for garbage collection because it has
no roots? This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't given
it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which would
prevent this situation from occuring?

-- 
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b


Relevant Pages

  • Re: String Init
    ... i posted a concern about initializing string variables to null ... the object is eligible for garbage collection because it has ... > no roots? ... object, it's a reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: String Init
    ... i have a routine spanning 200 lines of code. ... > the routine i intialize an object to null. ... the object is eligible for garbage collection because it ... > no roots? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to destroy arrays
    ... eligible for garbage collection in the Sub below? ... When a is an array reference, ... Actually, Scott didn't write that, fortunately - because it's not ... "roots" by the garbage collector until the end of the method. ...
    (microsoft.public.dotnet.general)
  • Garbage Collection and Threads
    ... the fact that when the GC runs it compacts the managed heap, ... the roots to point to the new memory location for the objects. ... pass it's threshold size and forcing a garbage collection. ... the same goes for the threads in appdomain B. ...
    (microsoft.public.dotnet.framework.clr)