fork () & memory allocation behavior

2019-07-01 23:00发布

问题:

I work on a system in which swap is disabled and memory overcommit is disabled.

Lets say my process consumes 100 MB memory currently and the system free memory is less than 100 MB.

If I do a fork() will it fail because kernel tries to allocate 100 MB for the child process as well ?

I have read that Linux uses copy-on-write when forking, so child & parent share all the pages. So I guess fork should succeed ?

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) ?

回答1:

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.

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.