Garbage Collection Mechanism

Source: Internet
Author: User

CRL is used to manage the memory allocation and recovery issues of the managed heap. The garbage collection mechanism is designed.

First, the reference type allocates memory on the managed stack. CLR assumes that the heap is infinitely large. In the ApplicationProgramInitialize during loading and recycle when disabled.

However, there is a limit to the size of the heap. When the managed heap is used up, CLR starts garbage collection.

CLR stores a list Of all object addresses. It regularly checks the objects in the Table. First, assume that all objects are not referenced.

Then, check whether the object is referenced. If yes, discard it and mark it as garbage. Wait for recycling.

If it is not recycled, It is upgraded to the first generation of garbage.

CLR allocates limited space for the first generation of garbage. When the space is used up, the first generation of garbage is recycled, thus reducing the number of garbage collections.

The remaining objects are upgraded to the second generation.

There are three generations of CLR spam.

 

Garbage collection frees programmers from the tedious work of memory management.

The type of the corresponding hosting program does not need to be managed by the programmer. However, the local resources used by the corresponding hosting program are also non-managed resources, such as the types used by I/O operations and database connections.

Garbage collection cannot recycle unmanaged resources. Therefore, you must manually release these resources when using such resources.

For example:

Public class test: idispose

{

// Local resource, handle

Intptr res;

Public test (intprt R)

{

Res = r
}

Public void dispose ()

{

Res = intptr. zero;
}
}

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.