开发者

C# garbage collection with object references

In C#开发者_开发技巧, when I have two object obj1, obj2 composed of a List<string> and I assign both of those objects to the same List<string> object.

If my reference to obj1 goes out of scope but my reference to obj2 does not, is obj1 still eligible for garbage collection, or are there some dependency issues because there is still a reference to the List<string> object?


obj1 should be eligible for garbage collection as long as there are no references to obj1 itself.


If my reference to obj1 goes out of scope, but my reference to obj2 does not, is obj1 still eligible for garbage collection, or is there some dependency issues because there is still a reference to the List object?

If I understand you correctly you mean both obj1 and obj2 are of type List<string> and both point to the same List<string> instance.

When obj1 goes out of scope, there still will be still obj2 as an active reference to the List<string> instance, so the list cannot be garbage collected.

If obj1 was part of a reference type on the heap (i.e. one of its properties) the memory space it occupied may be garbage collected as part of the outer object. If it was just a reference on the stack, GC will not be involved since the stack will be just unwound at the end of the method call when obj1 falls out of scope.

Keep in mind that obj1 is just a reference (in a way a pointer) to an object on the heap - this object may be garbage collected only when no reference is pointing to it anymore.


In your case, obj1 must be eligible for garbage collection.

You need to look at Jon Skeet's answer here. It clearly explains how garbage collection works on object references.

A nice tutorial for you on Object's Lifetime in C#.


There are three uses of memory defined in this question:

  • a reference to the single List<string> instance called obj1.
  • a reference to the single List<string> instance called obj2.
  • The single instance of List<string>.

if obj1 goes out of scope, but obj2 does not, then only the following remain after garbage collection:

  • a reference to the List<string> instance called obj2.
  • The instance of List<string>.

It is important to remember that C# abstracts away the concept of references in most cases so that you can safely think of obj1 and obj2 as being List<string> and not references, but reference they are.

It is likely that the obj1 reference is in the local call stack as opposed to the instance itself which is likely on the heap. Therefore obj1 (the reference) is only cleaned up when the call stack is unwound.


If obj1 is a member of a List, it's not available for garbage collection until the parent List is garbage collected.

So:

List<string> l = new List<string>();

string a = "one";
l.Add(a);

{
    string b = "two";
    l.Add(b);
}

At the end of this listing, a is in scope, b is out of scope, but both still have references in the list l, so neither are eligible for garbage collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜