当前位置:网站首页>Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?

Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?

2022-08-09 12:32:00 Park33448

I saw people in the group discussing interview questions early in the morning

Among them, the first question "What happens when I apply for 8G memory on a machine with 4GB physical memory?" There is a big controversy. Some people say that the application will fail, and some people say it canApplication is successful.

To say the answer to this question without preconditions is hooliganism.Because in 32-bit OS and 64-bit OS scenarios, the answer is different.

In addition, we also need to see whether the 8G memory will be used after the application is completed. It is one situation that it will be used, and it is another situation that it will not be used.

So, we're going to discuss it by scenario.

When an application applies for memory through the malloc function, it actually applies for virtual memory, and physical memory is not allocated at this time.

When the application reads and writes the virtual memory, the CPU will access the virtual memory. At this time, it will be found that the virtual memory is not mapped to the physical memory, the CPU will generate a page fault interrupt, and the process will switch from user modeGo to the kernel mode, and hand over the page fault interrupt 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 there is, allocate physical memory directly, and establish the mapping relationship between virtual memory and physical memory.

  • If there is no free physical memory, the kernel will start the work of reclaiming memory. If the free physical memory still cannot satisfy the physical memory application after the reclaiming memory is completed, the kernel will putThe last big move is to trigger the OOM (Out of Memory) mechanism.

The size of the virtual address space of 32-bit operating system and 64-bit operating system is different. In Linux operating system, the virtual address space is divided into two parts: kernel space and user space, as shown below:

p>

It can be seen here:

  • The kernel space of a 32-bit system occupies 1G, which is located at the highest point, 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 rest of the middle part is undefined.

Now we can answer the question: What happens if you 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 virtual memory space of 3 GB at most, so a process that applies for 8 GB of memory will fail at the stage of applying for virtual memory (I don't have a 32-bit operating system to test, I guess it will failThe reason is OOM).

What happens if I apply for 8G of memory on a machine with a 64-bit operating system and 4GB of 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 the process to apply for memory is to apply for virtual memory, as long as it does not read or write this virtual memory, the operating system will notPhysical memory is allocated.

As a simple test, my server is a 64-bit OS, but only has 2 GB of physical memory.

Now, on the machine, I apply for 4 GB of memory. Note that the following code simply allocates virtual memory and does not use the virtual memory:

#include #include #include #include #include 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, apply1gb memory, the starting address of this memory: 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 check the virtual memory size of the process with the following command:

# ps aux | grep alloc_4gUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 7797 0.0 0.0 4198540 352 pts/1 S+ 16:58 0:00 ./alloc_4g

Where, VSZ represents the virtual memory size used by the process, and RSS represents the physical memory size used by the process.As you can see, the VSZ size is 4198540, which is 4GB of virtual memory.

Then, let's change the code. After allocating virtual memory, use this virtual memory through the memset function to see what happens.

#include #include #include #include #include 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, apply1gb memory, the starting address of this memory: 0X%x\n", addr[i]); //Access virtual memory memset(addr[i], 0, s); } getchar(); return 0;}

Run result:

It can be seen that after applying for 2GB of virtual memory, this virtual memory is used immediately. Since the physical memory of this machine is only 2 GB, OOM occurs.

At this point, the verification is complete.A brief summary:

  • In a 32-bit operating system, because a process can only apply for a virtual memory of 3 GB in size, directly applying for 8 GB of memory will fail.

  • In a 64-bit operating system, because a process can only apply for a virtual memory of up to 128 TB, even if the physical memory is only 4GB, it is no problem to apply for 8G of memory, because the requested memory is virtual memory, and so on.Virtual memory is accessed, and OOM occurs because there is not enough physical space.

Okay, the water is out!

原网站

版权声明
本文为[Park33448]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091143418444.html