.NET GC Essentials (b)

This article describes some of the details of the knowledge of .NET GC, content mostly from books Under at The Hood of .NET Memory Management
(Note: This article assumes that you understand the basics of .NET, such value types, reference types, etc.)

Advanced

Before we talked about the process that performs memory compression SOH (Small Object Heap), but if there are a lot of (small) objects in the program, then execute it again in the SOH complete memory compression will consume a lot of time; furthermore after the process is generally the program is running, in fact, most objects are temporary objects created using will no longer be quoted (we should promptly clean up these objects), while the other objects that are not temporary, often cited time and are long (GC every time we do not need to go through the tag them).

Based on this, in turn SOH .NET objects stored in a generational process for further optimization of GC properties:

  • Generation 0 (Gen 0): No subject experienced GC examination (newly created objects)
  • Generation 1 (Gen 1): 1 GC undergone inspection has not been cleaned objects
  • Generation 2 (Gen 2): experienced greater than or equal to 2 times GC inspections had not been cleaned object

The following is a correlation diagram:

Here Insert Picture Description

GC process will probably triggered automatically executed when the following conditions are fulfilled:

  • Each generational achieve different respective memory size threshold:
    • Gen 0 reached ~ 256 K (GC will be recovered Gen 0)
    • Gen 1 reaches ~ 2 MB (GC Gen 1 will be recovered)
    • Gen 2 reaches ~ 10 MB (GC Gen 2 will be recovered)
  • GC.Collect () is called (GC Gen 2 will be recovered)
  • OS (operating system) to send a low memory (Insufficient memory) notice (GC Gen 2 will be recovered)

Of course, each generational threshold mentioned above are only the initial value, .NET will be dynamically adjusted according to the operating condition of the program.

Each generational GC process, there are many differences, we turn to see:

Gen 0 recovery

Object of GC on Gen 0 area objects through the tag, and the tag (referenced) is transferred to Gen 1 generational memory area (the object becomes a Gen 1 object), object unlabeled is to perform cleanup operations, and finally , Gen 0 region will always be empty.

Take the above schematic example, Gen 0 after recovery, the SOH memory distributed as follows:

Here Insert Picture Description

(Object Y changes from the Gen 0 Gen 1, Object Z is clean, Gen 0 emptied the entire region)

Gen 1 Recycling

GC of Gen 0 Gen region and the region of the object to traverse the tag and the tag into the object area Gen 1 and Gen 2 corresponds, then performs a compression process associated memory, Nature Gen 0 region would eventually be emptied.

Schematically illustrate still take initial, Gen 1 after recovery, the SOH memory distributed as follows:

Here Insert Picture Description

(Object Y changes from the Gen 0 Gen 1, Object Z is clean, Gen 0 whole area is cleared, Object X becomes the Gen 2)

Gen 2 recovery

After about Gen 0 Gen 1 recovered and recycled, Gen 2 can be directly recovered analogy process, and is not repeated herein (of course, the Gen 2 marked object does not move away, remains in the region Gen 2 )

Take for example the initial schematic still, after recovery Gen 2, the SOH memory distributed as follows:

Here Insert Picture Description

(Object Y changes from the Gen 0 Gen 1, Object Z is clean, Gen 0 whole area is cleared, Object X becomes the Gen 2, Object W to be cleaned)

To be continued (to be continued)

Published 144 original articles · won praise 146 · Views 250,000 +

Guess you like

Origin blog.csdn.net/tkokof1/article/details/104082887