Initial learning-----C # Garbage collection mechanism

Source: Internet
Author: User

Original address: http://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=206721898&idx=1&sn= Dfaee0ae6fa092b1a6db5779d75c0a2d&scene=5#rd

Below is this rookie of the above-mentioned great God article simple summary, of course, there are many shortcomings, I hope you still see the above link in the article better!

A basic principle:

Computer memory is broadly divided into two types: stacks and heaps. C # are divided into two types: value types and reference types. Where the value type is mainly stored in the stack (the mechanism of the stack is actually implemented by a similar list), the actual memory allocation of the reference type is in the heap, but the stack area maintains the actual address of the reference type, similar to the use of value type storage, to implement the maintenance of pointers

When instantiating a variable of a reference type, the following steps are used:

1. calculate the total number of bytes required to request an instantiated variable

2. compute Extra bytes required for instantiated objects (type object pointer and synchronous Fast index)

3.CLR detects if there is enough memory

A. memory is sufficient for direct allocation,

B. Insufficient memory, perform a garbage collection, check again if memory is sufficient, then throw outmemeryexception Exception

Two Check for objects that are no longer in use

Each application has a set of roots, each of which represents a storage object (especially a reference type)

1. Mark the stage, traverse all the roots, recursively, set the tag on the synchronization block index of the root that has the object reference

2. Compression phase. This stage of the garbage collector traverses the heap linearly to look for contiguous chunks that contain unmarked objects. If the garbage collector finds a smaller block of memory, it ignores memory, and if a large contiguous block of memory is found, the garbage collector moves the non-garbage objects in memory to these contiguous blocks of memory to compress the managed heap

Three, the concept of generation

concept of object generation (generation)

0 Generation, newly created instance,CLR does not have any checks on it

1 generations,theCLR checked once, objects that were not recycled

2 generations,CLR checked two times and above, still not recycled

The CLR allocates a certain amount of memory for each generation at the beginning of the program, and a garbage collection mechanism is performed when the object is created with insufficient memory. when 0 -generation memory is sufficient, 1 - Generation unreachable objects are not reclaimed. objects in Generation 1 are cleaned only when the 0 - generation memory is out of budget, so that the program only needs to tag parts of the object to implement an effective garbage collection mechanism.

Initial learning-----C # Garbage collection mechanism

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.