GC garbage collection and gc garbage collection mechanism

Source: Internet
Author: User
Tags finally block

GC garbage collection and gc garbage collection mechanism

We may seldom pay attention to garbage collection when developing our needs, because most of the time we use managed resources to recycle the memory of managed resources. net has already helped us,. net memory collection is not real-time, so we still need to pay attention to it.. net.

What is GCGC, that is, Garbage Collection? The following is a summary: . Net is a built-in garbage collection mechanism.Why does GC exist? Since GC is the built-in garbage collection function of. net, why does GC exist? In fact, in C Language (unmanaged), garbage collection is handled by developers themselves. Although it looks flexible, there are two problems: 1, if you forget to release or use the released memory, it will cause a serious bug. 2. It wastes developers' energy so that they cannot focus on their businesses and affect development efficiency. Therefore, in C # (hosting), Microsoft introduced the GC garbage collection mechanism to free developers from the boring garbage collection work and focus more on the business. Managed resources and unmanaged resources: 1. managed resources can be simply understood as reference types in c #, such as Class and Interface.

 

There may be questions about the value type? In. net, because the value type is on the memory stack, the stack will automatically exit when the scope is exceeded, so there is no so-called concept of garbage.

2. Unmanaged resources, the most common such as database connections.

There are two common garbage collection algorithms in the garbage collection algorithm. net. 1. Mark-Sweep-Compact AlgorithmThe. net gc algorithm is regarded as the Mark-Compact algorithm. Phase 1: Mark-Sweep Mark clearing stageFirst, assume that all objects in heap can be recycled, then find out the objects that cannot be recycled, mark these objects, and finally the objects that are not marked in heap can be recycled; Phase 2: Compact compression phaseThe heap memory space becomes discontinuous after the object is recycled. Moving these objects in heap enables them to be arranged continuously from the heap base address, similar to the disk space fragmentation. The recycling process is as follows. 2. Generational Algorithms. Net divides the memory hosting heap into three generations, Gen 0, Gen 1, Gen 2. If Gen 0 heap memory reaches the threshold, the 0 generation GC is triggered, after 0 generations of GC, the surviving objects in Gen 0 enter Gen1. If the memory of Gen 1 reaches the threshold value, perform 1 generation GC, one-generation GC recycles Gen 0 heap and Gen 1 heap, and the surviving object enters gen2. Although the Dispose mode and using statements seem to have a perfect GC mechanism for. net, there are still two problems: 1. GC can only release managed resources and cannot release unmanaged resources. 2. GC is not real-time, so it may cause performance problems. Since GC is not real-time, under which circumstances will it trigger recovery? The following situations are: 1, 0th full generation. 2. When the Code explicitly calls the GC. Collect method. 3. windows reports insufficient memory, such as an OutOfMemory exception. 4. When the CLR uninstalls the AppDomain. 5. When CLR is disabled. When a process is terminated normally (as opposed to being closed through the Task Manager), the CLR closes. To make up for the GC deficiency,. net provides the Dispose mode to ensure the timely release of resources. In the Dispose mode, only two steps are required: 1. The custom type inherits the IDisposable interface and implements the Dispose method in the interface. For example.

 

 

2. explicitly call the Dispose method of the RabbitMQWrapper instance in the code, as shown in.

Generally, we place the call to the Dispose method in the finally block of exception handling. This ensures that the code for clearing resources is executed.

To simplify this operation,. net provides us with the using statement. The syntax is as follows:

1 using (var proxy = new RabbitMQWrapper () 2 {3 // Business Code 4}

 

 

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.