I was wondering ,
An Instance of class is on the Heap. ( value types inside it are also in the heap).
But what about the opposite case ?
There is one question here but it didn’t mention any GC related info.
So – How does GC handle this situation ?
public struct Point
{
object o ;
public int x, y;
public Point(int p1, int p2)
{
o = new Object();
x = p1;
y = p2;
}
}
Pointincludes a reference to an object on the heap. This will become eligible for collection as soon as no more copies of thatPointexist with that reference. Noting that:is 2 copies, each with a reference to the same object on the heap. If those points are stored as fields on an object somewhere, then obviously the lifetime of the object will be at least as long as the object with those fields. If those points are only variables on the stack, then it gets more complex, because the GC may take into account whether a variable is ever read again. If it isn’t, the variable might not effectively exist (or: it might).
The path can be very indirect, but it essentially comes down to: can GC get to the object, starting from the GC roots.