Linux memory delay allocation

Source: Internet
Author: User

In Linux, when you do not use the memory (average physical memory), the operating system will not actually allocate the physical memory,

For example

 
Int *P=(Int*) Malloc (1024 * 1024)

If you do not access the memory pointed to by P next, you will see whether the memory occupied by the process includes this 1 MB of space.

The above sentence is correct, but it is not clear enough. The next question is, if I start to use the memory, how much space will it take?

For example, I wrote anotherCode:

 *P= 4;
Cout
< *P<Endl;

What is the memory occupied by the process at this time? I always thought it would be 1 MB, but in fact the memory occupied here is less than 1 kb.

The reason is that the OS allocates physical memory in units of pages. Therefore, only the memory on a certain page is accessed, the page will be mapped to the physical memory, that is, it will start to occupy the physical memory.

Both malloc and MMAP are similar.

Observe the Process Memory Access: pid = 'ps aux | grep process name | grep-V grep | awk-F "" '{print $2} ''; top-p $ PID

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.