0
using System.Collections.Generic;

public class A
{
    private int[] someothersubmemory;
    private List<int> b;

    public A ()
    {
        b = new List<int> ();
        someothersubmemory = new int[1000000];
    }

    public List<int> getList ()
    {
        return b;
    }
}


// elsewhere:

A a = new A();
List<int> c = a.getList();
a = null;                    // assume all references to the A() itself created 2 lines above are gone

I am educated about garbage collection being an automatism taking care of freeing memory.

  1. But does garbage collection -- in C# -- also take care of not freeing memory when there still is a reference to some "submemory" of the memory in question? In above example the memory would be A() and the submemory would be b inside A() now referenced by c.

  2. If the C# GC or some other part of C#/.NET does take care of not freeing (sub)memory when there still is a reference, when does "someothersubmemory" of above example get freed? When A() isn't referenced anymore or when no other submemory is referenced anymore (ie. b is not referenced anymore)?

user5588495
  • 76
  • 1
  • 6
  • It would be a pretty useless memory management system if it _ever_ discarded managed objects that you could still attempt to dereference. (Ignoring when one _explicitly_ asks for this to be possible, via `WeakReference`...it's clear that has no relevance to your question.) – Peter Duniho Jul 23 '20 at 23:19
  • well, the managed object itself as a whole cannot be accessed anymore. Only 1 subpart of it. Perhaps the question should propose to find out whether GC works top-down from whole objects or from bottom slices. In the latter case b is reachable thus it does not collected. But someothersubmemory would not be reachable anymore thus that does get collected. – user5588495 Jul 23 '20 at 23:25
  • 1
    _"the managed object itself as a whole cannot be accessed anymore"_ -- the managed object in question, the "submemory" of which you speak, _can_ be accessed, via the `c` variable. Just because you lost the reference to some _other_ object that was referencing it, that doesn't mean you no longer can reach the object. There is an **enormous** difference between an object, and a variable that refers to the object. You seem very confused about that difference, so that's probably the best place for you to focus your efforts. – Peter Duniho Jul 23 '20 at 23:31
  • _"the m.o. in question is "a"."_ -- no, it's not. You completely misunderstand the nature of reference types (i.e. C# classes). The `List` object that is references by the `A.b` field is an whole object unto itself, and it does not in any way rely on the lifetime of the instance of `A`. This is fundamental, and it is beyond the scope of a Stack Overflow Q&A to provide the kind of education required for someone that doesn't understand this. – Peter Duniho Jul 23 '20 at 23:46
  • well, the entities might be what is "managed" according to the spec, but "c" is only referencing "b". What about "someothersubmemory"? Will that get "disposed" of when "a" is gone or only when"c" would be gone? – user5588495 Jul 23 '20 at 23:49
  • _" "c" is only referencing "b""_ -- no, it's not. `c` is referencing the `List` object that `b` referenced. But `c`'s value is completely independent of `b`'s value, and once `b`'s value has been copied to `c`, nothing that happens to the variable `b` later affects `c`. `someothersubmemory` refers to an array object, and as clearly explained in the duplicate, if that array is no longer reachable (i.e. was only reachable in the first place via the instance of `A` referenced by the variable `a`), it's _eligible_ for collection. It may or may not ever actually be collected, but it _may_ be. – Peter Duniho Jul 23 '20 at 23:51
  • As a general life tip - don't ask for help and then attack those trying to help. I am not saying I completely agree with @PeterDuniho's words and approach here - but he is spending **his** time to help **you**. And his snark was largely in response to **your starting it** (if someone in my office said `if you have sufficient ability to understand them` to my face they'd better be my boss or there will be trouble). – mjwills Jul 24 '20 at 00:50

0 Answers0