[Linux Memory]linux Memory Learning (i)

Source: Internet
Author: User

1, meaning of each address range in memory

According to the order of address range from low to High: 0-3G address range:

Code Snippet: The executable file of the code, which is generally read-only and shared. (RO Code/data)

Data Segment : The global variable that has been initialized (RW data) static char *user= "Jiangsu"

BSS segment: save some uninitialized global variables (ZI data) static Char*user

Heap:Maoolc () or new () requested segment, for programmers to use, address to high address range growth (via BRK () function extension)

mmap interval:(some dynamic library files)

Stacks: function parameters, etc., are automatically allocated by the system, the address to the low address range growth (can be ulimit-s to see the maximum use of each process stack size)

Add: The normal code snippet does not start with the user space 0 address, such as ARM Linux embedded device starting from 0x8000 (32K), the preceding 32k can be used to store some error code, if a function returns an address, but the address size is less than 0x8000, Then you think that this function has an error ~

There is a gap between the normal stack and the kernel space, there is a gap between the BSS and the heap, and there is a gap between the heap and MMP, known as the ASLR technology, which prevents the address from being attacked by others.

3-4G space is the kernel space, and user-space code cannot be accessed.

The malloc function is detailed ~

malloc is just a library function, and there are different implementations of malloc on different platforms. malloc allocates memory from the heap, but there is no heap concept in the kernel, and the heap is just the concept of an application that, when created by a process, divides an area in the virtual address space of a process as a heap, and there is no corresponding physical memory for that area. Using malloc to allocate memory is actually just a smaller area from this virtual memory area to the application, and only when the application accesses this small area will a fault of the pages be generated to obtain physical memory. While free does not release physical memory, it returns the small areas allocated on the heap to the heap, which are implemented by GLIBC at the application level.

The use of malloc uses two system calls, BRK and MMAP,BRK, for increasing (or decreasing) the size of the heap, specifying the start address of the heap when the process is created (the heap space is growing upward), and the heap size is 0. When allocating memory using malloc, the size of the BRK growth heap is called when there is insufficient space left in the current heap, in effect BRK the end address of the virtual memory area where the heap resides is increased (or decreased) to a certain location. When malloc allocates more space than a threshold size (for example, 128K), malloc no longer allocates space from the heap, but instead uses mmap to remap a block of virtual addresses and calls Munmap to release the zone when free. Such a strategy is primarily to facilitate heap management, avoiding a large scale management heap, which is based on large memory allocations and does not often use this hypothesis.

It can be noted that if the allocated memory is too large to be allocated and released through the system call, the efficiency will be reduced, so if the application is frequently allocated more than the allocation threshold of memory, the efficiency is very low, this application can adjust the allocation threshold to make the memory allocation and release in the user state (in the heap) completed. You can use mallopt to adjust the parameters of malloc, m_trim_threshold means that if the heap size is larger than this value, the heap size should be shrunk at the appropriate time, m_mmap_threshold indicates that memory allocation requests larger than this value are used MMAP system calls.

[Linux Memory]linux Memory Learning (i)

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.