In-depth .NET hosted pile (on)

zhaozj2021-02-17  105

In all techniques of .NET, the most controversial may be garbage collection (GC). As an important part of the .NET framework, the hosted stack and garbage collection mechanism are unfamiliar with most people in us. In this article, you will discuss the hosted pile, and how you will get it from it.

Why do you want to host your stack?

The .NET frame contains a hosted pile, all .NET languages ​​use it when assigning a reference type object. Lightweight objects such as a value type are always allocated in the stack, but all class instances and arrays are generated in a memory pool, which is a hosted stack.

The basic algorithm of the garbage collector is simple:

● Mark all managed memory as garbage

● Find the memory block being used and mark them as valid

● Release all memory blocks that are not used

● Squiring piles to reduce fragmentation

Hosting heap optimization

It seems to be very simple, but the steps actually adopted by the garbage collector are not negligible, which often involves optimized design for improved performance. For example, garbage collection is highly overhead throughout the memory pool. However, studies have shown that most of the objects allocated on the hosted stack have only a short survival period, so the heap is divided into three segments, referred to as generations. The newly allocated object is placed in Generation 0. This generation is the first time to reclaim - in this recognization of memory that is no longer used, because its size is small (small enough to put it in L2 Cache in the processor), therefore recycled in it Will be the fastest and most efficient.

Another optimization operation of the hosted stack is related to the Locality of Reference rules. This rule indicates that the object allocated is often used together. If the objects are very compact in the stack, the performance of the cache will be improved. Due to the nature of the hosted stack, the objects are always allocated on a continuous address, and the hosted stack is always compact, the results have always been close to each other, never point far. This is a distinct contrast with the non-hosting code provided by the standard heap. In the standard heap, the heap is easy to become a fragment, and the objects allocated are often far away.

There is also an optimization that is related to a large object. Typically, large objects have a long life. When a large object is generated in the .NET hosted heap, it is assigned to a special part of the heap, which is never organized. Because the overhead of mobile large objects exceeds the performance of this part of these stacks.

Problems about external resources (External Resources)

The garbage collector can effectively manage resources released from the hosted stack, but the resource recycling operation is only implemented when a recycling action is triggered in memory. So how do the class manage limited resources like a database connection or window handle? Waiting until garbage collection is triggered, it is not a good way to clean up the database connection or file handle, which will seriously reduce the performance of the system.

All classes with external resources have been executed when these resources are no longer used, they should execute a Close or Dispose method. From beta2 (translation: all Beta2 in this article refers to .NET Framework Beta2, no longer specifically indicate), DISPOSE mode is implemented via IDisposable interface. This will be discussed in subsequent part of this article.

A class that needs to be cleaned out of the external resource should also implement a Finalizer. In C #, the preferences for creating termination operations are implemented in the destructor, and the implementation of the Framework layer, the implementation of the operation is overloaded the System.Object.Finalize method. The following two ways to terminate operation are equivalent:

~ OverduebookLocator ()

{

Dispose (false);

}

And: public void finalize ()

{

Base.Finalize ();

Dispose (false);

}

In C #, the termination operation of the Finalize method and the destructive function will result in errors.

Unless you have enough reasons, you should not create a patterns or Finalize methods. Termination operations reduce the performance of the system and increase the memory overhead of the execution period. At the same time, you can't guarantee when a termination operation will be performed because of the way the operation is executed.

Detail of memory allocation and garbage collection

After the GC has a general impression, let us discuss details about the allocation and recycling work in the hosted stack. The hosted pile looks unlikely the traditional pile of C programming we have already familiar with. In traditional stacks, the data structure is accustomed to using a large block of idle memory. The memory block that looks for a specific size is a very time consuming job, especially when the memory is full of fragments. Unlike this, in the hosted stack, the memory is set into a continuous array, and the pointer always patrols the boundary movement between memory that has been used and the boundary between the unused memory. When the memory is assigned, the pointer only simply increments - one of the advantages of this is that the efficiency of allocation operations has been greatly improved.

When the object is assigned, they are put in Generation 0. When the size of Generation 0 is to reach its upper limit, a recycling operation only executed in Generation 0 is triggered. Since Generation 0 is small, this will be a very fast GC process. The result of this GC process is the complete refresh of Generation 0. The object that is no longer used is released, and it is indeed the object being used and moved into the generation 1.

When the size of Generation 1 is close to its upper limit with the increase of the number of objects that move from Generation 0, a recycling action is triggered to execute the GC process in Generation 0 and Generation 1. As in Generation 0, the object that is no longer used is released, and the object being used is organized and moved into the next recognization. The main goal of most GC processes is Generation 0 because there is most likely to have a large number of unused temporary objects in Generation 0. The recycling process of Generation 2 has a high overhead, and this process is only triggered when the GC procedure in Generation 0 and Generation 1 cannot be released. If the GC process of Generation 2 still cannot release enough memory, then the system will throw outofmemoryexception.

The garbage collection process with objects with termination operation is slightly more complicated. When an object with termination operation is marked as garbage, it will not be released immediately. Instead, it will be placed in a finalization queue, which is a reference to this object to avoid this object being reclaimed. The background thread performs their respective termination operations in each object in the queue, and deletes objects that have been terminated from the termination queue. Only those objects that have already been terminated will be deleted from memory during the next garbage collection process. One consequence of this is that the object waiting to be terminated may be moved into a higher level of Deneration before it is cleared, thereby increasing the delay time it cleared.

An object that needs to be executed should implement the IDisposable interface so that the client quickly performs termination operations via this interface. The IDisposable interface contains a method - Dispose. This interface introduced by beta2, which is implemented in a widely used mode before Beta2. In essence, a subject that needs to terminate operations exposes the Dispose method. This method is used to release external resources and suppress the termination operation, just as shown below:

Public Class OverduebookLocator: IDisposable

{

~ OverduebookLocator ()

{

INTERNALDISPOSE (FALSE);

}

Public void dispose ()

{

INTERNALDISPOSE (TRUE);

}

Protected void innaldispose (Bool Disposing)

{

IF (Disposing)

{

Gc.suppressFinalize (this);

// Dispose of management Objects if disposing.

}

// Free External Resources Here

}

}

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

New Post(0)