Do you know these three memory management algorithms?

This article is shared from the China Mobile OneOS official account "Memory Management".

According to the difference of application requirements and system resources, the operating system provides different memory allocation management algorithms, namely small memory management algorithm , slab management algorithm and memheap management algorithm .

The small memory management algorithm is mainly aimed at the situation where the system RAM space is relatively small . It is generally used in systems with less than 2MB of memory space, and is also the most widely used memory management algorithm.

The slab memory management algorithm mainly provides a fast algorithm that approximates memory pool allocation when the system RAM space is relatively abundant .

The memheap algorithm is used when there are multiple memory heaps in the system. It can connect multiple memories together to form a large memory heap, and the application program cannot perceive the existence of multiple memory heaps.

Only one of these types of memory heap management algorithms can be selected or not used at all when the system is running, and the API interfaces they provide to the application are exactly the same.

Small Memory Management Algorithms

In the small memory management algorithm, it is managed in the form of memory blocks.

Each memory block contains a data header, and the memory blocks are connected by a doubly linked list through the data header. Whether the used block or the free block is managed in the same linked list. The header data structure of the memory block is defined as follows:

struct os_heap_mem 
{ 
    os_uint16_t     magic;    /*魔数*/
    os_uint16_t     used;     /*使用标识*/
    os_size_t       prev;      /*指向前控制块的指针*/
    os_size_t       next;     /*指向后控制块的指针*/
};  

Among them, next and prev are used for the connection of the doubly linked list, magic is a magic number, the system uses 0x1ea0 to identify the head of the memory management block, and the problem of illegal memory rewriting can be detected by this value. If the value changes, it means that the memory is illegally written. . used indicates the allocation status of the memory block.

The performance of memory management is mainly reflected in the allocation and release of memory. When allocating memory, it starts to traverse from the position of the free memory pointer. When a memory block of sufficient size is found, it is divided according to the situation. The first half is returned to the calling program for use, and the remainder after division The part continues to be inserted into the linked list.

The following example illustrates the process of applying for memory. The free pointer of the free linked list points to a 64-byte memory block. When the allocation function is called to apply for a 512-byte memory block, the 64-byte memory is insufficient, and the memory manager starts to traverse the memory block, next The used of the next memory header pointed to by the pointer is 1 and has been used; the next unused memory is found and the size is 1024 bytes, which satisfies the requested size. The manager uses the split function to add 12 to the requested 512 bytes The byte header space is divided into the first half, and the used mark is used and returned to the application; the remaining 512 bytes of space, the first 12 bytes are used for the data header, the remaining 500 bytes are used for the data body, the used mark is 0, and insert In a doubly linked list, as shown below.

<Schematic diagram of memory allocation>

Freeing memory is the opposite process. The memory manager will check whether the adjacent memory blocks are free. If they are free, they will be merged into a large free memory block to prevent memory fragmentation after the system runs for a long time.

 slab management algorithm

Slab is a buffered memory pool algorithm. The slab allocator manages memory by multiple areas, each area corresponds to a class of objects that occupy different sizes of space, and the system supports up to 72 types of objects.

The size of a zone is between 32K and 128K bytes, which is automatically adjusted according to the size of the heap when the system is initialized. The slab manager can allocate a maximum of 16K of memory space at a time. If it exceeds 16K, it will be allocated directly from the page allocator. The size of the memory block allocated on each zone is fixed and managed using a linked list, while the zone linked list of 72 objects is placed in an array for unified management, as shown in the following figure.

<slab management structure>

The following example illustrates the process of memory application. Assuming that a 16-byte memory is allocated, the slab memory allocator finds the corresponding zone linked list from the zone array linked list header array. If the linked list is empty, a new zone is allocated to the page allocator, and the first free memory block is returned from the zone. If the linked list is not empty, the first node in the linked list must have a free block, and the corresponding free block is returned. If all the free memory blocks in this zone are used up after the allocation is completed, the allocator will delete the zone node from the linked list.

When the memory is released, the allocator traverses the zone_array, finds the zone node where the memory block is located, and then connects the memory block to the free memory block list of the zone. If all the memory blocks in the free list of the zone have been released at this time, when the number of all free zones in the zone list reaches a certain number, the system will release the free zone to the page allocator.

memheap management algorithm

If the system contains multiple memory heaps with discontinuous addresses, the first two management algorithms are not applicable. The operating system provides the memheap management algorithm to solve this scenario.

If this algorithm is specified, multiple pieces of discontinuous memory can be added to the memheap management linked list when the system is initialized. When memory needs to be allocated, the allocator first checks whether there is any remaining space available in the default memheap. If there is a direct allocation and returns, if there is insufficient allocation, it traverses the linked list until it finds enough allocated memheap, and allocates memory from here and returns it to the application. Applications don't need to care where memory is allocated from, simplifying usage.

The process of releasing memory is similar, traverse the linked list to find the memheap where the memory address is located, and release the address space.

The memory management interface is designed as follows:

(1) Initialize the memory heap

(2) Allocate memory blocks

(3) Release the memory block

(4) Reallocate memory blocks

(5) Allocate multiple memory blocks

(6) Memory allocation callback function

(7) Memory release callback function

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324190881&siteId=291194637