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