-1

It is not a duplicate, I would like to know about this in context of C#.

I have such classes:

public class A
{
    public List<string> AList { get; set; }
}

public class B
{
    public List<string> BList { get; set; }
}

Imagine, that in row 6 happens Garbage Collection:

1 row: A objectA = new A();
2 row: B objectB = new B();

3 row: objectA.AList = new List<string>() { "1", "2", "3" };
4 row: objectB.BList = objectA.AList;

5 row: objectA = null;
6 row: //Garbage collection occurs

I have two questions:

  1. Will be objectA garbage collected? If yes, why? Cause, in my view these objects will be created in a heap like in the following image: enter image description here

  2. My second question is whether the following image is correct about how objects will be allocated in a heap. I mean, is objectA.AList placed inside a circle(the above image) or near the circle(the following image):

enter image description here

Community
  • 1
  • 1
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • So I'm not sure about this answer, so hopefully somebody with a better understanding will be able to answer, but as I understand it objects that have no references to them whatsoever _should eventually_ be garbage collected, nothing else needed. An object needs active scope eventually (up the chain of references) to the currently executing program in order not to be garbage collected. Once that's gone it's fair game for the collector, whenever it runs. – Ranger May 17 '16 at 16:12
  • This takes 2min to check. But logic dictates in my head, yes as objectB.Alist contains a refference to Alist so if Alist is altered so is the result of objectB.Alist as it is Alist. So if Alist equals null so does objectB.Alist. So the content of Alist is GC'ed. But the object Alist is not, it is now null – Thomas Andreè Wang May 17 '16 at 16:13
  • @HenkHolterman please, see my distinct question http://stackoverflow.com/questions/37293387/how-is-memory-allocated-when-you-create-a-property-of-class – StepUp May 18 '16 at 13:13

1 Answers1

5

Yes, the variable that objectA refers to will be - or at least may be garbage collected. There's nothing referring to it any more. (There's no guarantee that it will collected "soon", but it's eligible for garbage collection.)

The list that objectA.AList referred to won't be garbage collected, because objectB.BList now refers to it.

The problem with your first diagram is the idea that one object is "inside" another. That's not how it works - an object just maintains a reference to another, but each object is effectively an independent piece of memory... and if there are no references from anything to a particular object, it's eligible for garbage collection.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks for answer! Am I right that if I create new field(not a proprety) `int k` in `objectA`, then `k` will be placed somewhere in a heap, but not inside `objectA`? – StepUp May 17 '16 at 16:18
  • 1
    @StepUp: No, `k` would be part of `objectA`, just like the field backing `AList` is - but the *value* of the field backing `AList` is just a reference, it's not the list object. It might be worth you reading http://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference/32010236#32010236 – Jon Skeet May 17 '16 at 16:20