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
What is the Intelligent Edge? The so-called intel...
Now in the motherland, more than 10,000 5G base s...
WiFi 6 wireless routers are being upgraded and re...
[[356187]] About two years ago, when 5G had just ...
[51CTO.com original article] In recent years, spa...
Zoom, the leading video conferencing company, wit...
We all know that TCP is a reliable, connection-or...
Data center migration is a complex undertaking th...
Strictly speaking, the Singapore node is not a ne...
Recently, the network security operation and main...
For a long time, the relationship between the thr...
[[400174]] In the 5G era, in order to rapidly adv...
DiyVM is a Chinese hosting company founded in 200...
Recently, Aruba, a subsidiary of Hewlett Packard ...
27 years ago, anyone who mentioned the word "...