29859

fork () & memory allocation behavior

Question:

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

Answer1:

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.

Recommend

  • How do I turn off custom error handling in IIS for my web site?
  • l18n framework with compiletime checking
  • Solr Custom Similarity - Using a field from the indexed document
  • How to create a Graphics2D instance?
  • How to pin parse relation data in local data store?
  • adding a newly created and uploaded package to pycharm
  • Xcode 4.5 Uploading a iPhone 5 app only
  • change text of corresponding label when checkbox is checked/unchecked
  • Script# - Getting the latest build to compile
  • iOS MDM Enrollment SCEP Specification and PKIOperation and operation=PKIOperation &message=MMIC
  • Swagger UI for Rails API using ActiveModel's Serializer
  • HTTPS request/response in Android
  • How deleteLater() actually works in qt?
  • How to read config files with section in bash shell
  • PostgreSQL in Docker - pg_hba.conf to allow access from host to container
  • How to add a progress ring to the splash screen in Windows 8?
  • Http Requests not getting routed to Https NodeJs
  • pass sessionid through jquery ajax call to php
  • Android NDK refer to external libraries in JNI
  • insert into mysql database, if records already exists, then update [duplicate]
  • Remove stopwords and tolower function slow on a Corpus in R
  • How to reduce a DAG by replacing each longest non-branching path by an edge connecting the start and
  • Jenkins sending notifications to the wrong commit id
  • Videos won't upload
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • MayAVI install on Python 3.6 [duplicate]
  • What does “T extends Junk” mean in a generic class in Java?
  • How do I use libcurl to printf a remote FTP directory listing?
  • How to create subsets of a single set of elements with XSLT?
  • Possible to set default CloudKit container not based on application name?
  • concise way of flattening multiindex columns
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • How to clear a browser cache in Protractor
  • Cloud Code: Creating a Parse.File from URL
  • Terminal run dalvikvm with am.jar
  • Change cell value based on cell color in google spreadsheet
  • How to handle div that is created dynamically in a table
  • Debug `Unexpected end of JSON input Error` on content script
  • Make checkout phone field optional for specific countries in WooCommerce
  • ReferenceError: TextEncoder is not defined