Talking about the memory management under the Unix/linux

Source: Internet
Author: User

Concepts of program and memory

The program refers to the executable file on the hard disk. When the program is run, the executable file needs to be loaded into memory, the executable in memory is formed, and a process (file) can have multiple processes (memory) at the same time.

Partitioning of memory areas

When you run the program, you need to load the executable file into memory to form a process. each process occupies a separate area of memory, which is divided into different areas, from the low address to the high address: Code area, read-only constant area, global zone/Data area, BSS segment, heap area, stack area.

Code Area: Store executable directives (e.g., function addresses).

read-only constant area: holds literal constants, global and static local variables with constant attributes and initialization (such as: Global variables modified by the const keyword and static local variables modified by the const keyword).

Global Zone/Data region: holds initialized global variables and static local variables.

BBS segment: holds uninitialized global variables and static local variables, and initializes their values to 0.

Heap Area: storage of dynamically allocated memory.

Stack Area: non-static local variables are stored.

Summarize:

(1) According to the address from small to large arrangement, the memory area of the process is: Code area read-only constant zone global zone/Data area BSS section heap area.

(2) Where the code area and the read-only constant area are generally referred to as code areas, where the global zone/data area and BSS segments are generally referred to collectively as the global zone/data area.

(3) There is no strict separation between the stack area and the heap area, can be fine-tuned, and the allocation of the heap area is generally in accordance with the address from small to large, and the allocation of the stack area generally in accordance with the address from large to small distribution.

Virtual Memory management Technology

All unauthorized access to virtual memory will result in a segment error. For example, attempting to access virtual memory that is not mapped to physical memory or attempting to access virtual memory in an illegal way, such as writing to read-only memory.

Memory space and address space

Each process's user space address is between 0~3g-1, but their corresponding physical memory is independent, the system for each process user space to maintain a dedicated memory mapping table for the process, record the virtual memory to the physical memory of the corresponding relationship, Therefore, it makes no sense to Exchange virtual memory addresses between different processes. The Memory mapping table for user space changes continuously as the process transitions.

User space and kernel space

The kernel space addresses of all processes are between 3g~4g-1 and they correspond to only one copy of the physical memory, and the system maintains a memory-mapped table for the kernel space of all processes Init_ MM.PGD, the correspondence between virtual memory and physical memory is recorded, so that the kernel code and data accessed by the different processes through the system call are the same. The memory-mapped tables in the kernel space do not have to change as the process switches.

Requesting dynamic memory using the malloc function

(1) Use malloc to apply for dynamic memory considerations. When using the MOLLC function to request memory, in addition to the amount of memory required for the request, Ken can request an additional 12 bytes of seawater to store some information about managing memory, such as the size of the memory, and so on.

To use the memory requested by malloc, be careful not to cross-border access to the requested memory space to avoid data structure corruption.

(2) General principles for using malloc to request memory size

In general, when using malloc to request smaller dynamic memory, the operating system allocates 33 pages of memory at a time, with the ultimate goal of increasing efficiency.

Use size to view the memory allocation of the program.

Use the free function to release dynamic memory

In general, when using a larger memory than malloc, the system allocates 34 pages of memory, and when the requested memory exceeds 34 pages, the system allocates 33 memory pages (that is, the base units are allocated according to 33 memory pages).

When freeing memory with free, how much is freed, and when all memory is freed with free, 33 pages of memory may be retained for re-use to improve efficiency.

Related functions for memory management

(1) GetPageSize function

function function:

Primarily used to get the size of the memory pages in the current system, typically 4kb.

(2) SBRK function

#include <unistd.h>

VOID*SBRK (intptr_t increment);

function function: Mainly used to adjust the size of memory

Parameter increment > 0, indicates the requested memory space

Parameter Increment < 0, which means free memory space

Parameter increment = 0, indicates the current location of the acquisition memory space

Return value: The position before the resize memory was successfully returned, failed return (void*)-1

Attention:

In General, when using SBRK to request a smaller memory, the system allocates a memory page size by default, and once the requested memory exceeds one memory page, a memory page is allocated again (that is, the base unit is allocated according to a memory page), and when the memory is freed, If the remaining memory is sufficient to be represented by a memory page after the release, a memory page is freed once.

When you use SBRK to request memory, you do not request additional space to store information about the management memory.

Using the SBRK function to request memory is easier than freeing the memory.

(3) BRK function

INTBRK (void* addr);

function function:

Mainly used to adjust the memory size based on the target location specified by the parameter.

If target location > previous target location = Request Memory

If target location < previous target location = free memory

If the target position = Previous target location = = Memory is not changed

return value:

Successful return 0, Failure returns-1.

Attention:

It is easier to use the BRK function to free up memory, so use the SBRK function with the BRK function in general, use the SBRK function to request memory, and use the BRK function to free memory.

Talking about the memory management under the Unix/linux

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.