Linux memory study notes

Source: Internet
Author: User
Linux memory learning notes-general Linux technology-Linux programming and kernel information. For more information, see the following. I have been using and learning linux for a long time. Below are some notes I have prepared by reading articles from other people for my reference, at the same time, you can also review.

1. Two Concepts of memory: Virtual Memory and physical memory are supported by the kernel in linux.

Linux has two levels of memory management. The first level is a linear zone, which is similar to 00c73000-00c88000. It corresponds to the virtual memory and does not actually occupy the actual physical memory. The first level is a specific physical page, it corresponds to the physical memory on our machine.

In addition, there is a concept of memory delay allocation. when a user applies for memory, the Linux kernel only allocates a linear zone (that is, virtual memory) to the user and does not allocate the actual physical memory. Only when the user uses the memory, the kernel allocates a specific physical page to the user, which occupies valuable physical memory. The kernel releases the physical page by releasing the linear zone, finding the corresponding physical page, and releasing it all.

Example:

Char * p = malloc (2048) // only 2048 of the virtual memory is allocated and does not occupy the actual memory.

Strcpy (p, "123") // allocates a physical page. Although only three bytes are used, the memory still allocates 2048 bytes of physical memory for it.

Free (p) // find the corresponding physical page through the Virtual Address, release the physical page, and release the linear area.

We know that the user's process and kernel run at different levels, and the communication between the Process and the kernel is completed through system calls. Processes are applying for and releasing memory, mainly through brk, sbrk, mmap, and unmmap system calls. The passed parameters are mainly the corresponding virtual memory.

Note that a process can only access virtual memory, but does not actually see the use of the kernel physical memory, which is completely transparent to the process.

2. glibc memory manager

So every time we call malloc to allocate a piece of memory, what about system calls?

The answer is no. Here I want to introduce a new concept, Memory Manager of glibc.

We know that functions such as malloc and free are included in the library functions in the glibc library. Let's think about how inefficient the program will be if every memory operation calls a system call.

In fact, glibc uses a wholesale and retail method to manage memory. Glibc requests a large block of memory (Virtual Memory) each time it calls the system. When the process requests the memory, glibc extracts a block from its own memory to the process.

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.