uinc
uinc

Reputation: 1213

in c# when returning a string from a function does the memory get freed?

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?

Upvotes: 1

Views: 1343

Answers (7)

Guffa
Guffa

Reputation: 700432

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.

Upvotes: 0

cjk
cjk

Reputation: 46445

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.

Upvotes: -1

Gant
Gant

Reputation: 29889

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

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415931

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.

Upvotes: -2

MSalters
MSalters

Reputation: 179927

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

Upvotes: 7

RossFabricant
RossFabricant

Reputation: 12492

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

Upvotes: 15

Jon Skeet
Jon Skeet

Reputation: 1501153

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

Upvotes: 7

Related Questions