C# garbage collection mechanism.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A, managed / unmanaged code

C#C# code through the compiler into the assembly, the assembly is composed of Microsoft Intermediate Language, open up an application domain for the assembly of CLR, the assembly is running in the application domain, the application domain is independent of each other, and do not influence each other.

Managed code: CLR management code.

Unmanaged code: not by the CLR management code.

Allocated on the stack space variables, once complete the scope (i.e. brace scope) will be CLR recovery.

Objects allocated on the heap, when no reference any variables it, the object is marked as “ the garbage objects ” (no variables refer to it), waiting for the garbage collector.

Eg:

1 Person p=new Person();
2 
3 p=null;//P at this time do not point to any object space, this time for the “ the garbage objects”. 

Two,GC

GC periodically clean the heap garbage objects, cleaning frequency programmer GC cannot be determined, the CLR will automatically control. When an object is marked as spam, this object will not be immediately recovered.

Three, the destructor

1,No access modifiers, cannot have parameters.

2,The objects are garbage collector when, GC automatically calls the destructor.

3,When executing some clean up operation.

 1 class Person
 2 
 3 {
 4 
 5   ~Person()
 6 
 7  {
 8 
 9     Console.WrilteLine("I am a destructor");
10 
11  }
12 
13 }

Four, generation

When the object heap when there is 1W, GC is it right? 1W loop to determine whether the &ldquo object, and then garbage; ” for their recovery? The answer is no, Microsoft according to actual needs, using a lot of algorithms to clean up the garbage heap objects, one algorithm is very important is “ generation”. A total of 3 generation heap, For example, When the program is run, There are objects need to be stored in a pile., GC will create first generation (hypothesis space of size 256K), The object will be stored in the zeroth generation., When the program to run, Run to the zeroth generation is not large enough to store the object, And it would create first generation (hypothesis space for 10M), GC will make the zeroth inside the “ the garbage objects ” clear, The “ &rdquo alive; the object is put in the first generation, At this time the zeroth generation will be empty., Object used to store new here, When the zeroth generation full of, Will continue to execute the above operation, With the program running, The first generation can not meet the storage requirements, This time will be the establishment of the second generation, Clean up the same way as above. The process described above for understanding the:

GC.GetGeneration(P)Corresponding to the designated object generation, a total of three generation.

GC.Collect();//Let the garbage collector to reclaim all generations.

GC.Collect(1)//The recovery of zeroth and first generation recovery.

 class Program
    {
        ~Program()
        {
            Console.WriteLine("I am a destructor");
        }
        static void Main(string[] args)
        {
            Program p1 = new Program();
            Console.WriteLine("The P object's generation: " + GC.GetGeneration(p1));//In the zeroth generation
            GC.Collect();//All generations removed, including zeroth generation, then the live objects exist in the first generation. 
            Console.WriteLine("The P object's generation: " + GC.GetGeneration(p1));
            GC.Collect();//All generations removed, including zeroth or first generation, then the live objects exist in the second generation. 
            Console.WriteLine("The P object's generation: " + GC.GetGeneration(p1));
            GC.Collect();//Only a maximum of 3 generation
            Console.WriteLine("The P object's generation: " + GC.GetGeneration(p1));
            p1 = null;//P now in the second generation          
            GC.Collect(2);//At this time P1 was recovered. 
            Console.ReadKey();
        }
    }

Result:


Note: the above article is soft and original, for reprint please indicate the sourceļ¼



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Porter at November 18, 2013 - 7:04 PM