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
Passive Optical Network (PON) technology has beco...
Hello everyone, I am Xiaolin. This morning I saw ...
As the most commonly used Internet access technol...
[[385177]] 100G transmission in data centers is p...
SLS Alarm Management As a member of the Prometheu...
I was chatting with my boss that day and we menti...
Computing network is an emerging technology conce...
Brocade today announced the expansion of the Broc...
Technological advancements have helped businesses...
Previously, a joke mocking the operators caused a...
Starting from July 1, the mobile data roaming cha...
Recently, the three major telecom operators have ...
As high-speed cellular networks become mainstream...
CloudCone has updated its 2023 promotional progra...
Nowadays, Wi-Fi has become an indispensable part ...