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.
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.
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)?