叉()&内存分配行为 [英] fork () & memory allocation behavior
问题描述
我在禁用了交换功能且禁用了内存过量使用的系统上工作.
I work on a system in which swap is disabled and memory overcommit is disabled.
可以说我的进程当前消耗100 MB内存,而系统可用内存少于100 MB.
Lets say my process consumes 100 MB memory currently and the system free memory is less than 100 MB.
如果我执行fork()会失败,因为内核也尝试为子进程分配100 MB吗?
If I do a fork() will it fail because kernel tries to allocate 100 MB for the child process as well ?
我已阅读到Linux在分叉时使用写时复制,因此child&父级共享所有页面.所以我想fork应该成功吗?
I have read that Linux uses copy-on-write when forking, so child & parent share all the pages. So I guess fork should succeed ?
假设fork成功,可以说我在调用exec()之前在子进程中只有几行代码.因此,父级&子进程将继续共享文本段,并且除非子进程触及任何堆内存,否则内存使用量不应有任何变化.这是正确的吗?
Assuming fork succeeds, lets say I have few lines of code in the child process before calling exec(). So the parent & child will continue to share the text segment and there shouldn't be any change in memory usage unless child process touches any of the heap memory. Is this correct ?
Edit: One follow-up question: With swapping/overcommit disabled, can the cumulative VSS
(Virtual Set Size) of all the processes be more than the available physical memory ?
I tried this out. VSS of all the processes can be much more than that of the physical
memory available.
pmap -x output from a process.
total kB 132588 10744 7192
First number - Virtual Set Size
Second number - Resident Set Size
third number - dirty pages
RSS is < 10% of VSS. This is with swapping and overcommit disabled.
For every shared library loaded I see something like the following..
00007fae83f07000 32 24 0 r-x-- librt-2.12.so
00007fae83f0f000 2044 0 0 ----- librt-2.12.so
00007fae8410e000 8 8 8 rw--- librt-2.12.so
00007fae84312000 252 120 0 r-x-- libevent-2.0.so.5.0.1
00007fae84351000 2048 0 0 ----- libevent-2.0.so.5.0.1
00007fae84551000 8 8 8 rw--- libevent-2.0.so.5.0.1
I guess r-x segment is code and rw- is data. But there is a 2 MB segment
that is not loaded. I see this 2 MB segment for every single shared library.
My process loads a lot of shared libraries. That explains the huge difference
between VSS & RSS.
Any idea what is that 2 MB segment per shared library ?
When overcommit is disabled, if this process calls fork() will it fail when
the free memory is less than VSS (132588 Kb) or RSS (10744) ?
推荐答案
是的,如果完全禁用内存过量使用,则fork
将失败.它将失败,因为如果程序希望写入所有页面,它们可能会取消共享所有页面,而严格的过量提交模式将不允许这样做.
Yes, if memory overcommit is completely disabled then fork
will fail. It will fail because the program might unshare all of its pages if it wished to write to them and the strict overcommit mode will not allow this.
您可以将fork
替换为vfork
,这将起作用. vfork
设计为仅在fork/exec模型中与exec
结合使用以启动新进程时使用.
You could replace fork
with vfork
and that would work. vfork
is designed to be used only when combined with exec
in the fork/exec model to launch a new process.
这篇关于叉()&内存分配行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!