tags:

views:

375

answers:

8

This is what I mean:

class mycalss
{
   string myfunc()
   {
      string str="hello";
      return str;
   }
}
...
static void main()
{
   string str2;
   str2=myfunc();
   ...
   ...
}

In this case is there a risk that the garbage collector might delete the contents of str2 because str went out of scope?

+7  A: 

No, there's no risk here. The reference will be returned, and if the garbage collector runs it will be able to see the reference in main, so it won't free the string.

Note that even when the string is eligible for garbage collection, it won't be freed immediately - only when the garbage collector next runs (or even later, depending on what generation it ends up in).

Note that the garbage collector can collect objects which won't be used any more, even if there's a variable still in scope - so long as it knows that the variable won't be read again:

object x = new object();
Console.WriteLine(x);
// Lots more code not touching x
// The object could be collected at any time here
Jon Skeet
+15  A: 

No. The garbage detector will see that str2 has a reference to the data, and it won't be collected.

RossFabricant
+7  A: 

str and str2 are both references to one (string) object. The garbage collector will only reclaim objects that have zero remaining references.

MSalters
A: 

I don't believe that's a real concern. I've certainly never had that problem.

Matt Grande
A: 

Incorrect answer:

String is immutable, which I believe means that in your example, you will have a new copy of your string in memory, so when str is cleaned up, str2 has its own data.

Jon Skeet's clarification:

No - they're both references to the same string object. It copies the reference not the string.

ck
No - they're both references to the same string object. It copies the *reference* not the string.
Jon Skeet
Thanks for the education
ck
A: 

You'll be fine. Strings are value types. Normally when you return a value type you actually get a copy of the object. So when the str variable goes out of scope and is collected you're str2 variable will be just fine.

Now, because these are strings we're talking about here and not just any run-of-the-mill value type there are a few other things going on as well. Your "hello" string is most likely placed by the compiler into a string table and your string variables will be references to that same entry in the table, to avoid keeping around lots of copies of the same string.

Either way, don't worry about it being collected.

Joel Coehoorn
String isn't a value type. It's a reference type. You don't get a copy of the object, you get a copy of the *reference*.
Jon Skeet
(You're right about the literal being interned, admittedly - but I'd imagine that in the real code it isn't a literal being returned.)
Jon Skeet
A: 

No. As long as the reference to the same string is held, by str2 in this case. The GC will not collect it.

m3rLinEz
A: 

No, the garbage collector will not delete the contents of the string, for two reasons:

  • You have a reference to the string, and as long as there are any active references to an object the garbage collector won't delete it.

  • It's a string literal, so it's a constant in the assembly. It's created when the assembly is loaded and removed when the assembly is removed (i.e. when the application ends). The garbage collector will never delete a string literal.

Guffa