当前位置:网站首页>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_4gWhere, 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!
边栏推荐
猜你喜欢

Senior told me that the giant MySQL is through SSH connection

Double pointer - the role of char **, int **

问题来了:4GB物理内存的机器上申请8G内存能成功吗?
![[现代控制理论]6_稳定性_李雅普诺夫_Lyapunov](/img/94/e9c214f5a89e01c3f33414419767b6.png)
[现代控制理论]6_稳定性_李雅普诺夫_Lyapunov
![[现代控制理论]5_系统的可控性_controllability](/img/a6/29c89163290eb03b4ce28274d036ca.png)
[现代控制理论]5_系统的可控性_controllability

电解电容漏电流及均压

How should the acceptance criteria for R&D requirements be written?| Agile Practices

BISS绝对值编码器_TI方案_线路延迟补偿

Fapi_StatusType Fapi_issueProgrammingCommand使用注意事项

GRPC整体学习
随机推荐
LeetCode 1413.逐步求和得到正数的最小值
PAT1007
Django 无法链接mysql数据库
基于STM32+铂电阻设计的测温仪
标准C语言学习总结14
父类的main方法可以被继承么?有什么特殊?
ACM01 Backpack problem
We really need DApp?Really can't meet our fantasy App?
Redis的下载安装
[现代控制理论]4_PhasePortrait爱情故事动态系统分析
win10 outlook邮件设置
ZOJ1298(单源最短路径)
IDEA 关闭/开启引用提示Usages
【重要】C语言进阶 -- 自定义类型:结构体、枚举、联合
[现代控制理论]2_state-space状态空间方程
PAT1005
Django cannot link mysql database
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
ACM longest non-descent subsequence problem
JS封装防抖(代码持续优化)