Five: User space memory allocation

Table of contents

User space memory allocation

mmap

malloc


User space memory allocation

mmap

Map the kernel space directly to the user space, saving the memory copy from the kernel to the user space.

Disadvantages of mmap

  1. When mmap is used, the size of the memory map must be specified, so mmap is not suitable for variable-length files;
  2. If mmap has a lot of operations to update files, the advantage of mmap to avoid two-state copying will be amortized. In the end, it still falls on a large number of dirty page write-backs and the random I/O caused by it. Therefore, in the case of a lot of random writing, the mmap method may not necessarily be faster than ordinary writing with a buffer in terms of efficiency;
  3. For reading/writing small files (for example, files below 16K), mmap has higher overhead and delay compared with the read system call; at the same time, the mmap flushing is fully controlled by the system, but in the case of small data volumes, it is better to manually control it by the application itself;
  4. mmap is limited by the memory size of the operating system: for example, on a 32-bits operating system, the total size of the virtual memory is only 2GB, but since mmap must find a continuous address block in the memory, you cannot completely mmap a 4GB file. In this case, you must divide the mmap into multiple blocks, but at this time the address memory address is no longer continuous, the meaning of using mmap is greatly reduced, and additional complexity is introduced;

Therefore, the applicable scenarios of mmap are actually very limited. You can choose to use the mmap mechanism in the following situations:

Under the mmap mechanism, multiple threads share the same physical memory space. Therefore, mmap is more suitable for multi-threaded communication and shared memory.

malloc

  • Method 1: allocate memory from the heap through the brk() system call
  • Method 2: allocate memory in the file mapping area through the mmap() system call;

A threshold is defined by default in the malloc() source code:

  • If the memory allocated by the user is less than 128 KB, apply for memory through brk();
  • If the memory allocated by the user is greater than 128 KB, apply for memory through mmap();

When malloc() allocates memory, it does not honestly allocate the size of the memory space according to the number of bytes requested by the user, but pre-allocates a larger space as a memory pool . For example, malloc(1) allocates 1 byte of space, but actually allocates a larger space as a memory pool. The memory pool pre-allocates a certain amount of memory when the program is initialized, stores the allocated memory blocks in the memory pool, and obtains them directly from the memory pool when needed, without requesting the operating system to allocate memory, thereby avoiding frequent system calls and improving efficiency. When free, put it back into the memory pool, and take it directly from the memory pool when needed

Regarding the question of " the memory requested by malloc, will the memory released by free be returned to the operating system? ", we can make a summary:

  • The memory requested by malloc through the brk() method, when free releases the memory, does not return the memory to the operating system, but caches it in the memory pool of malloc for the next use ;
  • The memory requested by malloc through mmap() , when free releases the memory, will return the memory to the operating system, and the memory will be truly released

 

Guess you like

Origin blog.csdn.net/qq_52353238/article/details/130217551
Recommended