How to use C # code to prove a large object will be assigned at the beginning of the second generation heap?

Refers to a large object is greater than or equal to 85,000 bytes objects. I am here to use a static class method GC were obtained, had no thought of other methods to obtain, also did not expect to get the exact method of this object is not a big object .

byte [] bigArray = new  byte [ 85000 ]; 
Console.WriteLine (GC.GetGeneration (bigArray));

Output:


 It can be seen using GC.GetGeneration (Object) can only see the object in two generations, but it is not really depends on if a large object, you can use some performance tuning software to see. As used herein, CLRProfiler view, as shown below:

We allocated Byte [] is allocated in the LOH. This indicates that, indeed equal to the size of 85,000 bytes are assigned to an object directly large objects in the heap.


 Finally, note that you can not use the following code to test:

Large Objects statement:

public class BigObject
{
    public byte[] bigArray = new byte[85000];

    ~BigObject()
    {
        Console.WriteLine("BigObject Collected!");
    }
}

Upper end of the call:

BigObject bigObj = new BigObject();
bigObj = null;

Console.WriteLine("0代、1代回收");
GC.Collect(1);

Console.ReadKey();

Output:

It can be seen here this BigObject be recovered, even if the GC.Collect is set to 0 is the same generation of recycling. This error is a misunderstanding, and it does not recover the BigObject therein bigArray 85,000 bytes are recovered together, but recovered after BigObject  bigArray  or large objects present in the heap . In fact, the time to start is shown below BigObject allocate memory :

 

Guess you like

Origin www.cnblogs.com/Jeffrey-Chou/p/12578096.html