title: fork () & memory allocation behavior
body: <p>I work on a system in which swap is disabled and memory overcommit is disabled.</p>
  
  <p>Lets say my process consumes 100 MB memory currently and the system free memory is less than 100 MB.</p>
  
  <p>If I do a fork() will it fail because kernel tries to allocate 100 MB for the child process as well ?</p>
  
  <p>I have read that Linux uses copy-on-write when forking, so child &amp; parent share all the pages. So I guess fork should succeed ?</p>
  
  <p>Assuming fork succeeds, lets say I have few lines of code in the child process before calling exec(). So the parent &amp; 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 ?</p>
  
  <pre><code>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 &lt; 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 &amp; 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) ?
  </code></pre>