Linux memory layout, memory allocation principle __linux

Source: Internet
Author: User
Linux's virtual memory management has several key concepts


1. each process has a separate virtual address space, the virtual address that the process accesses is not a real physical address


2. virtual addresses can be mapped with physical addresses through the page tables of each process to obtain a true physical address


3. If the physical address of the virtual address is not in physical memory, then a page break is generated and the physical address is actually assigned, and the pages of the process are updated , and if physical memory is exhausted at this time, part of the page to the physical disk is eliminated based on the memory substitution algorithm.


Linux process virtual address distribution


Linux uses virtual address space, which greatly increases the addressing space of the process, from low address to high address, respectively


Read-only segment: This part of the space can only be read, not writable. Includes code snippets, Rodata segments (C-constant strings, and #define-defined constants)

data Segments : Saving global variables, static variables space

 

Heap: Is usually said dynamic memory, malloc/new most of them from this. The position of the heap top can be dynamically adjusted by function BRK and SBRK.

 

file Map areas : such as Dynamic Library, shared memory , such as mapping physical space memory, is generally the mmap function of the virtual address space allocated .

Stacks: The context space used to maintain function calls, typically 8M, can be viewed through ulimit–s.

Kernel virtual space: an area of memory that is not visible to user code, managed by the kernel.


32-bit system has 4G address space, where 0x08048000~0xbfffffff is User space, 0XC0000000~0XFFFFFFFF is kernel space, including kernel code and data, process-related data structure (such as page table, kernel stack, etc.).


64-bit Linux typically uses 48 bits to represent the virtual address space, and 40 bits represent the physical address, which can be viewed by/proc/cpuinfo

Address sizes:40 bits physical, bits virtual


malloc How the memory is allocated.


malloc is the memory allocation function in GLIBC and the most commonly used dynamic memory allocation function, whose memory must be freed by free, or it will lead to memory leaks. The implementation of malloc to obtain virtual storage space is related to the version of GLIBC, but the general logic is:


1. Apply for the memory within 128k, call SBRK (), allocate in the heap, move the top pointer to the high address, get the new virtual storage space. Virtual addresses are relatively small.

2. The first 16 bytes (64-bit system, 32-digit 8-byte) of each allocated memory address is the control information for the memory block (for free).

3. Not every malloc will lead to the increase in the top of the heap, if there is enough space in the heap, the top will not change.

4. When the allocation size is greater than 128k, use mmap in the file map area to allocate anonymous virtual storage space to obtain address space, the address is generally relatively large (near the stack interval)

5 can be modified by function mallopt (M_mmap_threshold, 64*1024) to use the MMAP threshold of 64k, subsequent distribution greater than 64k will use MMAP.


How much physical memory space does the malloc allocate?


The memory allocated by malloc is a virtual address space

1.VSZ does not grow after every malloc, because the remaining space in the top of the heap can be reused, so the malloc is very lightweight and fast.

2. If the vsz changes, the basic is equivalent to the amount of allocated memory, because VSZ is the total size of the virtual address space.

3.RSS of small increments, because the malloc allocated memory does not immediately allocate the actual storage space, only the first use, found that the virtual storage corresponding to the physical page is not allocated, resulting in a page break, before the actual allocation of physical pages, while updating the Process page mapping relationship (on demand ).

4. Because each physical memory page size is 4k, regardless of memset 1k or 5k, 7k, physical memory consumption is always a multiple of 4k. So the increment of RSS is always multiples of 4k.

How to view the usage of the process virtual address space.

Pmap


free memory is really released?


Memory allocated using MMAP calls the MUNMAP system call to release, and the space is actually freed.

Free frees up memory, in glibc, just mark as usable, form a memory hole (fragment), and does not really release

In the free implementation of GLIBC, as long as the total space (including the merged space) is released near the top of the heap over 128k, a sbrk (-size) is invoked to backtrack the heap top pointer and return the original heap top space back to the OS. Otherwise all become fragmented (fragmentation is properly merged if adjacent)


malloc Memory in the program code has a corresponding free, there will be no memory leaks.


As the system frequently malloc and free, especially for small chunks of memory, the heap will produce more and more unusable fragments, resulting in "memory leaks." And this "leakage" phenomenon using valgrind is not detectable.


since the memory in the heap cannot be released directly, why not use mmap to allocate it all.


Using Mmap to allocate 1M space, the first call resulted in a large number of page faults (1m/4k). The fault of the page is kernel behavior, which will result in the CPU consumption of the kernel state is larger.

The heap is a contiguous space, and if the fragmentation of the heap is not returned to the OS, if reusable fragmentation, access to the memory again is likely to result in no system calls and missing pages interruption, which will greatly reduce CPU consumption.


How to view the process's page break information.


Ps-omajflt,minflt-c <program_name>

Ps-omajflt,minflt-p <pid>


Which Majflt table major fault, refers to the big error. Mnflt represents minor fault, which refers to a small error. These two numbers represent the number of missing pages that have occurred since the start of a process. The difference between Majflt and Minflt is that Majflt indicates the need to read and write the disk, possibly the memory corresponding page in the disk needs to load into physical memory, or may be at this time the physical memory is not enough, need to eliminate some physical pages to disk


If the process of the kernel state CPU use too much, one of the reasons may be the number of page breaks per unit time multiple, can be viewed by the above command.


If the Majflt is too large, it is likely that there is not enough memory.


If the Minflt is too large, it is likely to be allocating/releasing large chunks of memory (128k) frequently, malloc using Mmap. In this case, the critical value can be increased by mallopt (M_mmap_threshold, <SIZE>), or the program implements the memory pool.


How to view the fragmentation of memory in the heap


It is worthwhile to use the fsmblks, Smblks, ordblks in the mallinfo structure, which represents the total number of fragments in different size ranges, which are 0~80 bytes, 80~512 bytes, and 512~128k. If the value of Fsmblks and smblks is too large, the fragmentation problem may be more serious.


In addition to GLIBC's malloc/free, there are other Third-party implementations.


Google's Tcmalloc and Facebook's Jemalloc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.