. NET Deep Learning Notes (3): Garbage Collection and memory management

Source: Internet
Author: User
Tags garbage collection

Let's take a moment to discuss it today. NET garbage collection and memory management mechanism, but also complete the last "WCF Distributed development Prerequisite Knowledge" series after a rest bar. I used to be interviewed by other people asked me the principle of GC work, I am now interviewing new person occasionally will ask related questions. So do you have a problem with that? Like you know. NET garbage collection mechanism? Can you briefly explain how the GC works? How can you manage memory effectively? What is the effect of an instantiated object in a using statement body? and other related issues. Let's discuss it in detail below. I believe you can interview others after you have finished reading.

The organization of this section is as follows, 1..Net type and memory allocation 2.GC garbage collector Works 3. What is unmanaged resource 4. How to efficiently free object resources. Summary. Now start our study in this section.

Type and memory allocation for 1..Net

NET all types are (directly or indirectly) derived from the System.Object type.

The types in the CTS are divided into two broad categories-the reference type (reference type, also called the managed type [managed type]), which is allocated on the memory heap, the value type. Value types are assigned on the stack. As shown in figure

Value types in stacks, advanced out, value type variables have a sequential life, this ensures that value type variables release resources before the scope is rolled out. Simpler and more efficient than reference types. The stack allocates memory from a high address to a low address.

The reference type is assigned to the managed heap (Managed Heap), declaring a variable to be saved on the stack, and storing the object's address in the variable when the object is created using new. Managed heap Instead, allocate memory from a low address to a high address, as shown

The working principle of 2.GC garbage collector

In the figure above, when the dataset expires, we do not show the Destroy object, the objects on the heap continue to exist, waiting for the GC to recycle.

The age of the garbage collector through generational support objects is recommended but not required. A generation in memory is a unit of objects of relative age. object's

The symbol or age identification object belongs to that generational. In the life cycle of an application, the nearer the object is created belongs to the newer generation, and the object that is created earlier has

Lower the sub code. The object code in the recent generational is 0.

In the new object, you first search the free list, find the most suitable memory block, allocate, adjust the memory block linked list, merge fragments. The new operation can be done almost at O (1) time, adding the top pointer to the heap 1. The working principle is: The GC runs when the remaining space on the managed heap is insufficient, or the generator 0 space is full, and begins to reclaim the memory. At the beginning of garbage collection, the GC compresses the heap memory, and the objects are concentrated to the top. GC in the scanning of garbage will occupy a certain amount of CPU time slices, the original GC algorithm is really scanning the entire heap, inefficient. The GC now divides the objects in the heap into 3 generations, and the recent entry into the heap is the No. 0 generation (generation 0), followed by Generation 1, Generation2. The first GC scans only the No. 0 generation. You do not have to scan other generation objects if the reclaimed space is sufficient for current use. As a result, GC creates objects more efficiently than C + + and does not need to scan all heap space. It is sufficient to compensate for the CPU time consumed by the GC by scanning the policy, plus the performance improvements brought by the memory management policy.

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.