Linux virtual memory-related knowledge

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 of the process access is not a real physical address;
2. Virtual address can be mapped by the page table on each process (in the kernel virtual address space of each process) and physical address to obtain a real physical address;
3, if the virtual address corresponding to the physical address is not in physical memory, resulting in a page fault, the actual allocation of physical address, while updating the process of the pages table, if the physical memory is exhausted, the memory replacement algorithm to eliminate the partial page to the physical disk.

How is the Linux virtual address space distributed?
Linux uses virtual address space, which greatly increases the addressing space of the process.

The virtual address space is made up of low address to high addresses, respectively:
1. Read-only segment: This part of space can only be read, not writable; (includes: Code snippet, rodata segment (c constant string and constant defined by # define))
2, Data section: The space to save global variables, static variables;
3, Heap: is usually said dynamic memory, malloc/new most of them from this. The position of the top of the heap can be dynamically adjusted by functions BRK and SBRK.
4, File mapping area: such as dynamic library, shared memory, such as mapping the physical space of memory, is generally the MMAP function allocated virtual address space.
5, Stack: used to maintain the function call context space, generally 8M, can be viewed through ulimit–s.
6. Kernel virtual space: Memory area where user code is not visible, managed by Kernel (page table is stored in kernel virtual space).

The 32-bit system has a 4G address space:

Where 0X08048000~0XBFFFFFFF is the user space, 0XC0000000~0XFFFFFFFF is the kernel space, including kernel code and data, process-related data structures (such as page tables, kernel stacks), and so on. In addition,%ESP holds stacks top, changes to the low address direction, BRK/SBRK function controls the top _edata of the heap change to the high address direction.

What about the results of the 64-bit system? Does the 64-bit system have a 2^64 address space?
In fact, the virtual address space division of a 64-bit system has changed:
1, the address space size is not 2^32, nor 2^64,

This can be viewed through #cat/proc/cpuinfo:

Cache_alignment:64
Address sizes:39 bits physical, bits virtual

How Linux glibc malloc is allocated, reclaims memory:

1. When malloc () is called, does it flush the system heap? Not necessarily. When requesting a small chunk of memory, malloc will first look in its own pool of memory, if any, directly to the memory pool. If there is no memory pool, first request from the system heap (such as SBRK) a large chunk of 128K more into the memory pool, and then allocated. When applying large chunks, they are allocated directly with Mmap.

2. Free (), will the memory be returned to the heap on the system? Not necessarily.

About memory leaks:

A narrow-sense memory leak is a memory space allocated by Malloc/new in the code, which is not free/delete when freed, so that unused variables occupy the memory space of the process.

Generalized memory leaks: As the process runs, the memory space that the process consumes keeps growing. Using the malloc library here, even if the malloc,free is used correctly, there is a potential for memory leaks because internal fragmentation, external fragmentation, and low memory utilization are introduced in malloc memory pool management.

Linux virtual memory-related knowledge

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.