What happens if you apply for 8G memory on a machine with 4GB physical memory?

What happens if you apply for 8G memory on a machine with 4GB physical memory?

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:

  • If so, physical memory is allocated directly and a mapping relationship between virtual memory and physical memory is established.
  • If there is no free physical memory, the kernel will start to reclaim memory. If the free physical memory still cannot meet the application for physical memory after the memory reclaiming work is completed, the kernel will use its last big move to trigger the OOM (Out of Memory) mechanism.

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:

  • The kernel space of a 32-bit system occupies 1G, which is located at the top, and the remaining 3G is user space;
  • The kernel space and user space of a 64-bit system are both 128T, occupying the highest and lowest parts of the entire memory space respectively, and the remaining middle part is undefined.

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>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

int main() {
int ret;
char* addr [ 4 ] ;
printf( "Use cat /proc/%d/maps to view memory allocation\n" , getpid());
size_t s = 1024 * 1024 * 1024;
int i = 0;
for(i = 0; i < 4; ++i) {
printf( "alloc size = %d\n" , s);
addr [ i ] = (char*) malloc(s);
printf( "After the main thread calls malloc, it applies for 1gb of memory. The starting address of this memory is: 0X%x\n" , addr [ i ] );
}
getchar();
return 0;
}

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
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 7797 0.0 0.0 4198540 352 pts/1 S+ 16:58 0:00 ./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>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

int main() {
int ret;
char* addr [ 4 ] ;
printf( "Use cat /proc/%d/maps to view memory allocation\n" , getpid());
size_t s = 1024 * 1024 * 1024;
int i = 0;
for(i = 0; i < 4; ++i) {
printf( "alloc size = %d\n" , s);
addr [ i ] = (char*) malloc(s);
printf( "After the main thread calls malloc, it applies for 1gb of memory. The starting address of this memory is: 0X%x\n" , addr [ i ] );
// Access virtual memory
memset(addr [ i ] , 0 , s);
}
getchar();
return 0;
}

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:

  • In a 32-bit operating system, since a process can only apply for a maximum of 3 GB of virtual memory, directly applying for 8G memory will fail.
  • In a 64-bit operating system, because a process can only apply for a maximum of 128 TB of virtual memory, even if the physical memory is only 4GB, it is no problem to apply for 8GB of memory, because the applied memory is virtual memory. When this virtual memory is accessed, OOM will occur because there is not enough physical space.

<<:  5G development is gradually getting better

>>:  The second half of the 5G era begins

Recommend

The key role of optical transceivers in passive optical network technology

Passive Optical Network (PON) technology has beco...

What happens if you apply for 8G memory on a machine with 4GB physical memory?

Hello everyone, I am Xiaolin. This morning I saw ...

Why does Wi-Fi need 6GHz?

As the most commonly used Internet access technol...

PAM4 and Coherent Technology in 100G DWDM Optical Modules

[[385177]] 100G transmission in data centers is p...

Getting to the bottom of HTTP and WebSocket protocols

I was chatting with my boss that day and we menti...

Data Cabling: Seven Tips for Office Renovations and Relocations

Technological advancements have helped businesses...

The "tragic" situation of operators' operations

Previously, a joke mocking the operators caused a...

How cloud services enable a 5G-driven future

As high-speed cellular networks become mainstream...

Wi-Fi Alliance announces Wi-Fi 6 as new logo

Nowadays, Wi-Fi has become an indispensable part ...