Glibc memory management

  

X86 platform LINUX process memory layout is as follows:

The meaning of the above paragraph is as follows:

text: store the program code, compile time to determine, read-only;

data: store data that can be determined when the program is running, readable and writable;

bss: global variables and static variables defined without initialization;

heap: generally Programmer allocation, if not released, may be recycled by the OS at the end of the program;

stack: has the compiler automatically allocates the release, stores the function parameters, local variables, etc.;

Mmap: Map area;

Programs can use system calls to manage heap and mmap directly, but more often use the malloc and free provided by C to dynamically allocate and free memory. The limitation of stack on Linux is roughly 8M, and on Windows is 2M.

C-style memory management program:

is also malloc and free, mainly through brk or mmap to add extra Virtual memory. Using malloc to manage memory for programs that need to maintain long-term storage can be very disappointing. If there are a large number of unfixed memory references, it is often difficult to know how they are released.

Pooled Memory Management:

Applications can manage memory more easily; memory allocation and reclamation are faster; error handling pools can be pre-allocated so that programs are still exhausted when regular memory is exhausted Can be restored; there is a standard implementation that is very easy to use.

The memory pool is only suitable for operations that can be staged; usually it does not work well with third-party libraries; if the structure of the program changes, you have to modify the memory pool; you must remember which pool you need to perform from distribution.

Reference Count:

Don't forget to call the reference count function; it cannot be released as part of a looping data structure; it is harder and slower in a multithreaded environment.

Garbage Collection:

Never worry about the double release of memory or the lifecycle of an object;

can't interfere with when memory is freed; it can be managed slower than other forms. If you forget to set the pointer that is no longer used to null,

Memory Manager design goals:

Maximize compatibility;

Maximize portability (can Good communication with the OS);

wasted the smallest space (the management of its own data structure also requires memory, there is also a need to pay attention to the fragmentation);

fastest speed ( 2/8 principle, mainly used to optimize hotspots);

Maximize adjustability (can adapt to multiple distribution needs, or adapt by configuration);

Maximize locality (The consideration here is the relationship between CPU cache and memory);

Maximize debugging functions (don't need to be a programmer);

Maximize adaptability (without modification) Adaptability when configuring);

ptmalloc implements malloc and free And a set of other functions to provide support for dynamic memory management. The allocator is located between the user program and the kernel and is used to respond to the user's allocation request, request memory from the operating system, and then return it to the user program. In order to maintain efficient allocation, the allocator generally pre-allocates a large chunk of memory and manages it by some algorithm. The memory that the user releases is not immediately returned to the operating system. At the time of design, ptmalloc compromised design goals such as high efficiency, high space utilization, and high availability. The design assumptions are as follows:

Use mmap to allocate large memory for long life cycles;

Very large memory allocation always uses mmap;

Memory allocation for short life cycles Use brk;

Try to cache only small, temporary memory, and large memory is returned directly to the system;

Small memory blocks are only merged when malloc and free; Br>

The condition of shrinking the heap is that the current free size plus the size of the chunks that can be merged before and after is greater than 64K, and the size of the top of the heap reaches the threshold;

Programs that require long-term storage are not suitable Ptmalloc;

The overall structure is as follows:

The actual data is Chunk, the structure of the Chunk in use is as follows:

Where:

P: indicates whether the previous Chunk is in use;

M: The flag Chunk is the virtual memory obtained from that memory area;

A: Whether the flag is the primary allocation area;

The structure of the free Chunk in memory is as follows:

Use bin to manage empty in glibc Free chunks, the details are not mentioned. When the free chunk is linked to the bin, ptmalloc will check if the chunks before and after it are also free. If so, it will be merged into a large chunk.bin structure is as follows:

ptmalloc Increase the speed of the allocation, will put some small chunks into the Fast bins first. The chunks in fast bins don't change its use flag P, so they can't be merged. When the user allocates small memory, ptmalloc will first find the free block of the response in fast bins, and then look for Unsorted bins. The free chunks. The merged chunks, or the chunks that cannot be placed in the fast bins, are first placed in the Unsorted bin. If the requirements are not met in the Unsorted bin at the time of allocation, the chunks in the Unsorted bin are added. In the bins.

Because the low-address to high-address allocation is used when allocating memory, such an allocated large memory (used to simulate sub-heap) is likely to have a free memory. That is, the Top chunk.Top chunk is considered after the fast bin and bins, so this interval is not in the bins structure. If ptmalloc manages to allocate a space in the top chunk and the top chunk is not large enough, then a new sub-heap is reassigned and the top chunk is migrated to the new sub-heap. The new sub-heap is connected to the existing sub-heap with a singly linked list. As follows:

When the space to be allocated is large enough, ptmalloc will use mmap to directly map the memory to the process space using memory mapping. The chunk thus allocated will be directly in contact with the map when it is free, and a reference to such a memory region will cause a segmentation error.

The Last remainder chunk is another special chunk. If you allocate a small chunk, if you can't find a suitable chunk in the small bins, if the size of the last remainder chunk is larger than the required small chunk size. . Then it will split into two, one for the user and the other for the new Last remainder chunk.

ptmalloc's response to the user's memory allocation request:

Get the main The lock of the allocation area, if it fails, find the non-primary allocation area, and then create a new non-primary allocation area;

Convert the size of the user request to the size of the chunk space that needs to be allocated;

If chunk_size<=max_fast then go to 4, otherwise, jump 5;

Try to allocate in fast bins, if it succeeds, it will end;

If chunk_size<=512B One step, otherwise jump 6;

Find the corresponding small bins, if found, the allocation is successful; otherwise, transfer 7;

merge the chunks in the fast bins, traverse the chunk in the unsorted bin, if There is only one chunk, and this chunk has been used in the last allocation, and the chunk size to be allocated belongs to smallbins, and the chunk size meets the requirements, then the chunk is directly cut and the allocation ends; otherwise it is placed Bins;

at Find in large bins; if failing to turn to 9;

If the top chunk satisfies the requirements, then allocate a block from it; otherwise, turn to 10;

If it is the main allocation area, call sbrk to increase the top chunk Size; otherwise allocate a new sub-heap; or directly use mmap to allocate; if you need to use mmap to allocate turn 11, otherwise turn 12;

use mmap system call to map a space for the program's memory space, then Return the pointer to the user program;

Determine if the malloc is called for the first time, and if it is the main allocation area, perform an initialization. Otherwise it is allocated according to the rules of 10.

In order to avoid the surge of Glibc memory, you need to pay attention to the following points:

The allocated memory is released first (top chunk consideration);

is not suitable for Manage long-lived memory, especially persistent and irregularly allocate and release long-lived memory;

Do not turn off ptmalloc's mmap allocation threshold dynamic adjustment mechanism;

Multi-threaded staged execution Programs are not suitable for using ptmalloc (more suitable for using memory pools);

Minimize the number of threads in the program and avoid frequent allocation and release of memory;

Copyright © Windows knowledge All Rights Reserved