Some thoughts on memory management

 

frame:

First of all, no matter how large the storage device is, we usually talk about a few T hard drives, but the memory is not large, such as 4G/8G/16/. My previous laptop was 2G, 32-bit.

For a 32-bit system, even if it is full, the space is 4G, 0x00000000-0xffffffff, no matter 32-bit or 64-bit, for many processes running on it, it has its own virtual memory, which means it is virtual memory, which means it is not true. Give you so much, give you as much as you use.

The code segment/data segment/BSS segment/heap/stack is for the user space 3G above the process, and of course there is 1G kernel space above the process, but it is common to all processes.

The 1G of the kernel space needs to be mapped to the real 4G physical memory, 896M of which is directly mapped, and the remaining 128M is mapped to most of the physical memory, relying on dynamic mapping. Vmalloc is only one of them, there are two more, vmalloc linear memory is continuous, but the mapped physical memory is not necessarily. (64-bit is definitely not this addressing mechanism, relying on a rich family background)

This 4G physical memory also says, DMA area 16M, normal area 880M, this is the source of the directly mapped 896M.

Physical memory allocation:

The basic unit is a page, a 4kb, external fragmentation (continuous allocation/release, but the allocation is as continuous as possible, this is the reason), the solution is the partner allocation method: pages of the same size form a linked list, there are large and small, and the abundance is made by people

Internal fragmentation (a page is not used up, wasted), the slab algorithm, does not divide by page. The concept of the buffer pool is to create a pool to store the initialized objects, and release them back to the pool, not to the partner system.

Divided into high-speed cache (kmalloc and kfree) and dedicated cache (kmem_cache_alloc and kmem_cache_free).

kmalloc is the most used in the driver, mostly used to allocate small memory, continuous, allocated by the slab allocator. vmalloc is used to allocate large memory, the target is physical memory, divided by pages, not necessarily continuous. kmalloc can be configured as sleepable or non-sleepable by configuring flags, and GFP_KERNEL can sleep. GFP_ATOMIC cannot sleep and is suitable for use in interrupt handling functions. Compared with GFP_ATOMIC, the allocation failure using GFP_KERNEL is much less

Virtual memory allocation:

Contains user space virtual memory and kernel space virtual memory.

malloc allocates a large memory pool, and then divides many memory blocks of different sizes in the memory pool, who will give it to whom, and reduce CPU overhead

 

 

Guess you like

Origin blog.csdn.net/beibei_xiansen/article/details/110088124