Structure with reference types and GC?

It was interesting to me,

The class instance is on the heap. (value types inside it are also on the heap).

But what about the opposite case?

There is one question here , but it does not mention any related information. GC

So - how does the 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;    
   }
}
+5
source share
3 answers

Pointincludes a reference to the object on the heap. It will become available for collection as soon as no copies of it Pointexist with this link. Noting that:

Point p1 = new Point(1,2);
Point p2 = p1;

- 2 , . -, , , , . , , GC , . , (: ).

, , , : GC , GC.

+7

GC

  • GC Handles ( - , GC)
  • CPU

, , , . , . , GC. , PInvoke.

PInvokes, , GCed, , , , . GC .

Edit1: . GC wil .

+3

, Duck & reg; ; . , ; , ; .. , ( ) ( , , ). , .

If you look at things this way, it will be easy to understand that the behavior of the GC structure is essentially the same as the behavior of the GC, which can be obtained by simply replacing the structure with separate fields (the only significant place in the behavior is β€œunusual” with arrays, since usually an array slot contains only one element, not a set of fields, not the structural equivalent of an array of structure type).

0
source

All Articles