MONO interpretation of memory management and recycling

Brief introduction

Mono supports automatic memory recovery, because MONO integrated memory collection algorithm. In 1.X to 2.X version, MONO Fritillaria integrated memory management and recovery algorithm; and in version 3.X or later, you begin to enable SGEN memory management and recovery algorithm.

This week study 2.6.X versions BOEHM (Fritillaria) and GC memory management algorithms portion of the code. BOEHM belongs to an open source project, implemented as supports C / C memory management and the GC, the C / C project allocated memory part of the interface (the malloc or other allocate memory interfaces) replacing memory allocation interfaces BOEHM provided (GC_malloc ), it is possible to achieve automatic memory management in C / C ++ projects, such as free calls without having to manually release the memory interface. The MONO is based BOEHM, to achieve automatic memory management and GC.

 A, GC realized the way 

1. The reference counting algorithm: cited art algorithm is not the only algorithm used GC root set concept. The basic idea is to add a counter for each object, all the counters recorded the number of reference points to the object. Each time a new reference point to the object, a counter is incremented; the other hand, if the reference point to the object is blank or links to other objects, the counter is decremented. When the counter is 0, the object is automatically deleted.

Mark 2. & Sweep algorithm: also known as mark sweep algorithm, mark by mark all stages up to the root of the object, the object is not marked, said no references, recyclable, BOEHM is the use of the algorithm automatic memory recovery; node copy algorithm: copy the new node to the live memory area, time release the old memory area, the object is transferred, the metadata should also need to set the intermediate layer, embodied without making studies;

 Two, BOEHM memory management algorithm 

BOEHM algorithm clear labeling method, the mark phase by accessing the root and leaf nodes traversed eventually all existing references to memory are marked, but not labeled memory (all are recorded from memory heap allocated BOEHM , thus partially cleared unlabeled relieved).

2.1 Here BOEHM process allocates memory, before introducing the allocation process, introduce critical data structures.

2.1.1 GC_size_map[2049]

A MAXOBJBYTES (2048 + 1) array elements, each array element to an integer value, allocates the memory to quickly locate a multiple of the particle size distribution in the allocation of memory (in fact, may be used (bytes + 16-1) / 16 circumvented this array should do is to reduce the amount of computation ...), but this is the minimum size of 16 BOEHM allocate memory, that allocates 1 byte, BOEHM will allocate 16 bytes of memory for you (I here viewed macro code analyzing machine 64 according to the definitions, different platforms bytes slightly different, do not affect the analysis).

While if the allocated memory size is greater than 2048, this time to perform another branch large memory allocation (since the process is similar, so the large memory allocation is temporarily not study, the following description are within the memory allocated 2048 bytes and recycling introduction).

2.1.2 GC_obj_kinds[3]

Three-element array, the array is defined BOEHM allocated in order to distinguish the type of memory, in BOEHM, there are pointers stored in memory, the normal memory and non-memory recovery points, this analysis is a general memory allocation process (as this is the application calls most often, but not reclaim the memory belonging to the memory BOEHM order to do their own memory management and allocation of these memory not require labeling and recycling), each element contains a ok_freelist [MAXOBJGRANULES + 1] array, MAXOBJGRANULES 128 , 128 for the reason that each GRANULES is a particle size distribution, i.e., 16 bytes, 128 * 16 = 2048, the upper limit is just small memory can be allocated.

2.1.3 ok_freelist the array each element is a pointer to the free list, pointing to the memory block size index * 16, i.e., the memory block pointer to the first element of the list stores 16-byte block size, and the first storage element 128 the linked list of memory block size is 128 * 16 = 2048 pointers of the memory block size.

The array stored in memory block size role: when allocating memory, checks the priority list if there FREE block, if present, can be removed directly returned to the application of an upper layer in the list, if not, it will then call distribution function , a larger memory allocation, memory is divided into large and small memory linked list stored in the ok_freelist (particularly storage memory size allocated by the upper layer INDEX requires several GRANULE determined).

2.1.4 GC_hblkfreelist[N_HBLK_FLS+1]

Array N_HBLK_FLS = 28 (this is why the design is such because the value is), GC_hblkfreelist each element is stored in a linked list of free memory, and the difference is ok_freelist, its memory size is PAGE_SIZE basic block, i.e. 4096, and the like ok_freelist, block size for each list element associated with the index is stored, can be understood, the size of the first memory block list element is PAGE_SIZE, the second is PAGE_SIZE * 2, and so on. As the article describes only a small memory allocation, there is no need to consider handling a particularly large memory allocation.

Allocating memory when needed, for example when requesting allocation of 16 bytes of memory (the upper layer has a particle size down call GRANULE aligned), then it will be (bytes + PAGE_SIZE-1) / PAGE_SIZE index obtained, and then detected GC_hblkfreelist [index] whether there is an idle speed, without traversal will continue rearwardly, after finding, split into pieces large pieces, two pieces again after the push GC_hblkfreelist a suitable location, a front for dispensing, because the upper allocated only 16 bytes of memory, when the upper layer to the size of the memory PAGE_SIZE, this time will be the 16-byte memory is divided, and generates a list (list pointer without additional memory, only 16 bytes in the first 8 bytes as pointers to) the lists stored ok_freelist [1], in which case the upper memory allocation can be done.

GC_hblkfreelist the memory is allocated calls by the following :

As can be seen from the above, a memory allocation process, first detects ok_freelist list, if the FREE block exists, returns directly taken out (of course provided using a labeled, the GC will be used), if no corresponding ok_freelist memory size, from GC_hblkfreelist call to get PAGE_SIZE memory and storage, the next redistribution can be taken directly from the ok_freelist, the equivalent of doing a simple type of memory pool.

2.1.5 Hblkhdr data structure

This data structure is a critical data structures, GC can block the normal operation thanks to the description. When dispensing PAGE_SIZE of a memory from GC_hblkfreelist, generates a hblkhdr object, this object describes the PAGE_SIZE memory block, memory block size transfer the hblkhdr stores (hb_sz, hb_mark5, descr information), hb_sz stored upper allocated, when allocating 16 bytes, hb_sz will be set to 16.

The role of this element is set to the size of its element stored in the PAGE (if allocated 16 bytes, the upper layer indicates that an object allocation of memory storage up to 16 bytes), and for storing the hb_mark PAGE which blocks are used ( each bIT may describe a tile, each tile a minimum of 16 bytes, 8 bytes and therefore 5 bIT is sufficient to describe a PAGE, can be described as 16 * 588 bytes, more than 4096 words section).

After good distribution HBLKHDR structure , it will be stored in two arrays, storage of (herein assumed as the start position P pointer PAGE)  top_index [>> 22 is P] -> bottom_index [P 12 is & >> 1024] of position, because each PAGE 12 to 4096 bytes, i.e., 12 th power of 2. I.e., each of the high address will PAGE of 10 as an index, as the index 10, which is stored in the secondary array HBLKHDR (of course, the two arrays are not so much a promoter generates secondary array, but generated during operation (otherwise too wasteful of memory, if a start is generated, you need at least 102 410 248, that is, 8M memory, and many nest site during operation will not be used)),

After generating this data, a pointer any can find it belongs HBLKHDR descriptor, and based on the descriptor, the maximum can be aware of the pointer to the memory block size of the object and its flag case (hb_sz and hb_mark).

After introduction to the above, there is probably an idea, general memory allocation process: check ok_freelist -> GC_hblkfreelist-> assign a PAGE-> and partitioned described HBLKHDR.

Function call stack which was as follows:

Setup_header also called to initialize the corresponding HBLKHDR, to allocate calls GC_install_header second stage array and distribution HBLKHDR (This structure is recovered from the non-allocated memory).

As can be seen above description, the memory allocation pointer BOEHM 32BIT described by the same if there is a low memory and memory address bits 64BIT 32BIT simultaneously, it will conflict, BOEHM circumvent this problem (since allocation of memory by specifying by other means starting position (here did not look in detail, linux it is to allocate memory by sbrk, GDB also found that, because they do not affect the analysis process, so without making too much of the research for details).

Released three original articles · won praise 3 · views 60000 +

Guess you like

Origin blog.csdn.net/jinangl/article/details/104747577