C # garbage collection mechanism

Source: Internet
Author: User

I. managed code/unmanaged code

C # The code is compiled into an assembly through the C # compiler. The Assembly is composed of Microsoft's intermediate language. CLR will open up an application domain for the Assembly, where the Assembly runs, application domains are independent of each other and do not affect each other.

Managed code: Code managed by CLR.

Unmanaged code: Code not managed by CLR.

The variables allocated in the stack space will be recycled by CLR once the execution is complete (that is, the scope of braces.

The object allocated in the heap. When no variable references it, the object is marked as a "spam object" (no variable references it), waiting for the garbage collector to recycle.

Eg:

 Person p=  p=;

Ii. GC

GC regularly clears junk objects in the heap. The GC cleaning frequency cannot be determined by the programmer. CLR automatically controls the GC. When an object is marked as garbage, it may not be immediately recycled.

Iii. destructor

1. Access modifiers and parameters are not allowed.

2. When the object is recycled by the garbage collector, the Destructor is automatically called by the GC.

3. Perform some cleanup operations.

       ~        Console.WrilteLine(    }

Iv. Generation

When there are 1 million objects in the heap, does GC cycle for 1 million times to determine whether it is a "spam object" and then recycle it? The answer is no. Microsoft uses many algorithms to clean up the junk objects in the heap based on actual needs. One of the most important algorithms is "generation ". There are a total of three generations in the heap. For example, if an object needs to be stored in the heap when the program is running, GC will create 1st generations (assuming the space size is 256 K ), the object will be stored in the 0th generation. When the program continues to run and the size of the runtime to The 0th generation is insufficient to store the object, the 1st generation will be created (assuming the space is 10 MB ), GC will clean up the "Garbage objects" in The 0th generation and put the "living" objects in the 1st generation. At this time, the 0th generation will be empty and will be used to store new objects, when the 0th generation is full, the above operations will continue. with the running of the program, the 1st generation cannot meet the storage requirements. At this time, the 2nd generation will be created, and the cleaning method is the same as above. Used to understand the process described above:

GC. GetGeneration (P) obtains the generation of the specified object. There are three generations in total.
GC. Collect (); // Let the Garbage Collector recycle all generations.

GC. Collect (1) // reclaim 0th and 1st.

 ~  Main(=  + GC.GetGeneration(p1));            GC.Collect();            Console.WriteLine( +            Console.WriteLine( +            Console.WriteLine( += ;            GC.Collect();

Result:

 

Note: All the above articles are original. If you need to reprint them, please indicate the source!

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.