Hello everyone, I am Xiaolin. This morning I saw readers discussing these interview questions in the group: Among them, the first question "What will happen if I apply for 8G memory on a machine with 4GB physical memory?" is quite controversial. Some people say that the application will fail, while others say that it will succeed. It would be foolish to give the answer to this question without any prerequisites, because the answer is different in the scenarios of 32-bit operating system and 64-bit operating system. In addition, we also need to see whether the 8G memory will be used after it is applied for. If it is used, it is one situation, and if it is not used, it is another situation. So, we need to discuss it in different scenarios. When an application requests memory through the malloc function, it actually requests virtual memory, and no physical memory is allocated at this time. When the application reads and writes this virtual memory, the CPU will access this virtual memory. At this time, it will find that this virtual memory is not mapped to the physical memory. The CPU will generate a page fault interrupt, the process will switch from user mode to kernel mode, and the page fault interrupt will be handed over to the kernel's Page Fault Handler (page fault interrupt function) for processing. The page fault interrupt handler will check whether there is free physical memory:
The sizes of virtual address spaces of 32-bit operating systems and 64-bit operating systems are different. In the Linux operating system, the virtual address space is divided into two parts: kernel space and user space, as shown below: From here we can see:
Now we can answer this question: What will happen if we apply for 8GB of memory on a machine with a 32-bit operating system and 4GB of physical memory? Because of the 32-bit operating system, a process can only apply for a maximum of 3 GB of virtual memory space, so when a process applies for 8GB of memory, it will fail at the virtual memory application stage (I don't have a 32-bit operating system to test it, I guess the reason for the failure is OOM). What will happen if you apply for 8G memory on a machine with a 64-bit operating system and 4GB physical memory? In a 64-bit operating system, a process can use a virtual memory space of 128 TB, so it is no problem for a process to apply for 8GB of memory, because when a process applies for memory, it is applying for virtual memory. As long as the virtual memory is not read or written, the operating system will not allocate physical memory. We can do a simple test. My server is a 64-bit operating system, but the physical memory is only 2 GB. Now, I apply for 4 GB of memory on the machine. Note that the following code simply allocates virtual memory but does not use it: #include <stdio.h> Then run this code, you can see that although my physical memory is only 2GB, the program normally allocates 4GB of virtual memory: We can view the virtual memory size of the process through the following command: # ps aux | grep alloc_4g VSZ represents the size of virtual memory used by the process, and RSS represents the size of physical memory used by the process. As you can see, the size of VSZ is 4198540, which is 4GB of virtual memory. Then, let's change the code. After applying for virtual memory, use this virtual memory through the memset function and see what happens. #include <stdio.h> Running results: As you can see, after applying for 2GB of virtual memory, this virtual memory is used immediately. Since the physical memory of this machine is only 2GB, OOM occurs. At this point, the verification is complete. A brief summary:
|
<<: 5G development is gradually getting better
>>: The second half of the 5G era begins
DesiVPS is a foreign hosting company headquartere...
In order to support technological innovation, hel...
Continuing with the year-end summary, many friend...
As the Internet gradually moves towards mobile te...
A study conducted by Juniper Research reveals pro...
On January 11, at the 2022 (12th) China Internet ...
Continue from the previous article "Introduc...
The story of "A horse gallops across the sky...
Since the Wi-Fi frequency band was first vacated ...
RackNerd has previously launched AMD Ryzen CPU+NV...
In the vast information system of the Internet, D...
Traditional data transmission methods mostly use ...
In order to enable different computer manufacture...
VPSSLIM is a foreign hosting company registered i...
[51CTO.com original article] On November 26, HRA ...