Garbage recycling in .NET

xiaoxiao2021-03-06  147

Garbage recycling cloud in .NET Source: 9cbs directory L

l about garbage collection

l garbage collection algorithm

M application roots (Application Roots)

l implementation

M stage i: tag (Mark)

M Phase II: Correction (Compact)

l Termination

l Optimization of garbage recovery performance

M weak reference (Weakreference)

M generation (Generations)

l Garbage recycling related myth

Introduction Microsoft claims that .NET is a revolutionary programming technology. Many elements make it the first choice for most developers. This article we will discuss one of the .NET Framework.

The advantage of the benefits - the convenience of memory and resource management.

About the garbage collector each program uses a certain order of resources, or memory buffers, or network connections, or database resources, and more. In fact, in an object-oriented environment, each type is seen as

It is some effective resources for the program. In order to use these resources, some memory must be assigned to describe this type.

Resource Access Press the steps:

1. For the type allocation memory to describe resources.

2. Initialize resources, set resources into initialization status to make resources available.

3. Use resources by accessing the type of instance (repeated as needed).

4. Destroy resource status to clear resources.

5. Release memory.

The garbage collector (GC) in .NET completely helides developers from tracking memory usage and determination.

Microsoft® .NET CLR (Public Language Runtuance) requires all resources to allocate from managed stacks. You don't need to release objects in the hosted stack - when the application no longer needs these objects

When the object will be released automatically.

Memory is not unlimited. Garbage collectors need to perform recycling to release memory. The optimization engine of the garbage collector will choose the best recovery time for the allocated distribution (accurate standards are provided by Microsoft

). When the garbage collector performs recycling, it first identifies the object that is no longer used by the application in the managed heap and then performs the memory space of these objects.

However, in order to perform automatic memory management, the GC must know the seat of the root. That is, it should know when an object is no longer used by the app. In .NET, GC

It is learned through a thing called metadata. Each data type used in .NET describes it through metadata. With the help of metadata, the CLR knows each pair in memory.

The layout of the icon, is helpful to GC during the stages of garbage collection. Without this information, GC will not know where an object ends and the next start.

Application Roots has a set of roots (ROOTS) each application. Root identifies the storage location, this location or pointing to a hosted heap, or pointing to an empty object (NULL).

such as:

l All global and static object pointers in an application.

l All local variable / parameter object pointers in a thread stack.

l The object pointer registered in the hosted heap.

l The object pointer in the FREACHABLE queue.

The table of the active root is maintained by the JIT compiler and CLR and is accessible to the aggregation of the garbage collector. The garbage collection in .NET is implemented with tracking and recycling, the CLR implements the tag / sorting (COPACT) recovery.

This method has the following two phases:

Phase I: Mark (Mark) Find memory that can be recovered.

When the GC starts running, it assumes that all objects in the stack are garbage. In other words, it assumes that the root of the application does not point to any object in the stack.

Phase I includes the following steps:

1. GC recognizes the reference or application root of the survival object.

2. Start traversing from the root, build a picture of all objects that can be traversed from the root.

3. If GC is ready to try to add an object already in the figure, it stops traversal of this path. There are two purposes in this way, the first is greatly optimized because it will not traverse

A set of objects over time. The second is to prevent a dead cycle when there is an object's loop connection list, so the loop is effectively controlled.

Once all roots are checked, all the trash recovers contains all objects that can be traversed from the application root. Any object in the figure cannot be accessed by the application,

That is, the so-called garbage.

Phase II: Moved all survived objects to the end of the stack, empty the space at the top of the heap.

Phase II contains the following steps:

1. Now GC linearly traverses the pile, looking for neighboring garbage pairs (now being considered free space).

2. The GC then moves the non-garbage object in the memory and removes all the gaps in the heap.

3. Objects in mobile memory cause the object pointer to fail. Therefore, GC needs to modify the root of the application to point to the new location.

4. In addition, if the object contains a pointer to other objects, the GC is also responsible for correcting these pointers.

After all garbage is identified, all non-garbage objects are also organized, and all non-garbage objects have been corrected, and the last non-garbage object is added to the next one.

Plus the location of the object.

FINALIZATION

The .NET Framework's garbage collector can track the life cycle of the object created by the application, but when it encounters an object (such as file, window or network

It is not possible when picking, etc.).

Once the application is no longer using those non-hosting resources, they need to release them. .NET Framework provides an endize method: Reclaim this pair in the garbage collector

When the memory is stored, this method of the object must be performed to clear its unmanaged resources. Due to the default Finalize method, if you need to display the clear resource, you must overwrite this party.

law.

If a Finalize method is used as a function of only C , it is not surprising. Although they are all already given the task of the resource occupied by the object, they still have

Very different semantics. In C , when the object is introduced, the pattern function is called immediately, and the Finalize method is called when the garbage is started.

In .NET, since the presence of the finalizer makes the work of garbage recovery more complex because it adds many additional operations before release the object.

Whenever, when allocating a new object containing the Finalize method, there will be a pointer to this object being added to a number called a Finalization queue.

According to the structure. When the object cannot be traversed again, the GC thinks that this object is garbage. GC first scans the Finalization queue to find the pointer to these objects, when the pointer is found,

Remove it from the Finalization queue and add to another internal data structure called the Freachable queue, so that this object is no longer part of the garbage. At this time, GC completed determining garbage. Then sorted (Compact) recoverable memory, which is responsible for emptying the FINALIZE method for the FREACHABLE queue and executes the object.

When the second garbage collector is triggered, it treats the Finalize object as a real garbage, then simply releases their memory.

It can be understood that when an object needs to end, it will die first, then survive (resurrection), and then die again. It is recommended to avoid using the Finalize method unless there is a need.

The Finalize method will increase the memory pressure because the memory and resources occupied by the object will be released until the two garbage recycles are started. So you can't control the Finalize method

The order of rows, it may result in unpredictable consequences.

Optimization of garbage recovery performance L weak reference (Weakreference)

l generations (Generations)

Weakreference weak reference is a way to improve performance for reducing the pressure of large objects in hosted stacks.

When a root point to an object, it is called a strong reference to this object and this object cannot be recycled because the application can traverse this object.

When an object has a weak reference pointing to it (Weakreference), this object can be reclaimed if there is a memory request and the GC starts, this object can be reclaimed, when an application

When you try to access this object again, the access will fail. On the other hand, in order to access a weaker reference (Weakreference), the application must get one for this

Strong references for objects. If the application gets its strong reference before the garbage collector reclaims this object, the GC will not be able to recycle this object because there is a strong reference to this object.

The hosted stack contains two internal data structures of two management weak references: weak reference tables and long reference tables.

Two types of weak references:

l Weak reference does not track recovery.

That is, an object with a weak reference will be retracted immediately without waiting for the Finalize method.

l The weakest reference tracking recovery.

That is, the GC will only recover this object when the storage space of the object in the long reference table can be recovered. If the object has a Finalize method, it is called in the Finalize method.

The object cannot be resurrected.

These two tables simply store pointers allocated in the hosted stack. Initially, both tables were empty. When you create a weak reference (Weakreference) object, the object is not supported

Allocation in the pile. Instead, an empty storage location is assigned in a weak reference table; the weak reference table uses a weak reference table, and the long reference table is used.

Let us look at an example to see what will happen when the GC is running. The following diagram (Figs. 1 and 2) show the state of all internal data structures before the GC operation and after the run.

Figure 1: GC operation

Figure 2: After the GC is running

The following is the operation of the GC runtime:

1. GC builds a picture for all traverse objects. In the above example, the diagram contains objects B, C, E, G.

2. GC scan shortage reference table. If the object pointing to the pointer is not in the figure, then this pointer identifies an uncomidateable object, this location in the weak reference table is set to null

. In the above example, the position of the object D is set to NULL because it is not part of the figure. 3. GC scans the Finalization queue. If the object refers to the object in the queue is not in the figure, then this pointer identifies an object that is unable to be traverse, the pointer is from the Finalization queue.

Move to the FREACHABLE queue. At this time, the object is considered to be traversable, so add it to the figure. In the above example, the objects A, D, and F are not included in the figure, but it is considered to be traversable.

Because they belong to the Finalization queue. Furthermore, the Finalization queue is emptied.

4. GC Scanning Sweak Reference Table. If the object in the table finger is not in the figure (now the object refers to the object referred to in the Freachable queue), the pointer identifies an unavailable

Object, location is set to NULL. Since objects C and f are included in the figure, they do not place NULL.

5. GC Corruption (Compact) memory, extrusion cannot be traverse the void left by the object. In the above example, the object h is the only object that is unique from the heap, which is allocated by the memory.

Generations (Generations) Since garbage collection is to be completed, they may be interrupted during program execution. GC is also possible to interrupt to meet the real-time system

Requirements of the need to respond in a timely response.

There is a characteristic representation in the GC, which is specifically designed to improve performance. A multi-generation GC is obtained by most of the procedures written in various languages

Two facts have been carefully analyzed:

1. The newly created object has a shorter life cycle.

2. The more old objects, the longer living.

Multi-generation recovers divide it into several groups through the age of objects, and young objects are more frequently recycled in older objects. When initialization, the hosted stack does not contain any objects. All new objects

It is added to the 0th-generation heap until the stack is full and triggered garbage collection. Since most objects have a short period of time, only a small number of young objects survive in the first recovery.

Once an object survives after the first recovery, it is raised to the first generation. It can be said that new objects can be said in the 0th-generation after garbage collection. Only when the 0th generation of stacks are full of garbage

The acquisition will be triggered again. All 0th-generation objects are organized to the first generation. Then, the 0th generation does not contain any object, but all new objects have entered the 0th generation.

Therefore, as an object of "mature" (survived in a multi-generation recovery), they are moved to the next level. The second generation is the largest generation supported by the CLR GC. Later back

The object of the second generation of survival will only be simple to stay in the second generation.

Therefore, the object of the generation of the buning into the object and the recovery and finishing younger generation increases the efficiency of the garbage collection algorithm, because a large amount of meaningful space is recovered from the heap, while

The recipient checks all the objects in all generations.

A GC that can perform more generations, each recycle must ensure that the time required (at least as possible) is less than a maximum time to help do some of the real-time operations for real-time environments.

When it is also prevented from making the user's obvious feeling.

Garbage recycling related myths GC obviously slower than handmade memory

Corresponding explanation: not necessarily. Modern garbage collector looks like running and manual storage allocation (Malloc / Free or New / Delete). In some special procedures, garbage collection

Running may not be as fast as the user-specific custom memory allocation is so fast. But from another aspect, do you add an extra generation to the correct work of manual memory management (for example,

The reference count shown) is often more expensive than GC.

GC will make program interrupt

Corresponding explanation: Since the garbage collector usually stops the entire application while looking and recycling garbage objects, they may result in too long interruption time and let the user aware. However, by the current optimization, these can feel completely avoided.

Handmade memory management does not cause interruption

Correspondence: Handmade memory management does not ensure performance. It may be interrupted due to a large number of allocation or release of memory.

The program with GC is large and bloated; GC is not suitable for small programs or systems

Corresponding explanation: Although it is very advantageous to use GC in a complex system, there is no reason to believe what GC will introduce what big overhead in other sizes.

I have heard that GC will use a lot of memory twice.

Correspondence: This may be a fact for the original GC, but not garbage collector is like this. The data structure used for GC is much larger than those manually managed.

转载请注明原文地址:https://www.9cbs.com/read-127979.html

New Post(0)