As the parent process is using huge mount of memory, fork
may fail with errno
of ENOMEM
under some configuration of kernel overcommit policy. Even though the child process may only exec
low memory-consuming program like ls.
To clarify the problem, when /proc/sys/vm/overcommit_memory is configured to be 2, allocation of (virtual) memory is limited to SWAP + MEMORY * ration(default to 50%)
.
When a process forks, virtual memory is not copied thanks to COW. But the kernel still need to allocate virtual memory space. As an analogy, fork is like malloc(virtual memory space size) which will not allocate physical memory and writing to shared memory will cause copy of virtual memory and physical memory is allocated. When overcommit_memory is configured to be 2, fork may fail due to virtual memory space allocation.
Is it possible to fork
a process without inherit virtual memory space of parent process in the following conditions?
if the child process calls exec
after fork
if the child process doesn't call exec
and will not using any global or static variable from parent process. For example, the child process just do some logging then quit.
No, it is not possible. You might be interested by vfork(2) which I don't recommend. Look also into mmap(2) and its MAP_NORESERVE
flag. But copy-on-write techniques are used by the kernel, so you practically won't double the RAM consumption.
My suggestion is to have enough swap space to not being concerned by such an issue. So setup your computer to have more available swap space than the largest running process. You can always create some temporary swap file (e.g. with dd if=/dev/zero of=/var/tmp/swapfile bs=1M count=32768
then mkswap /var/tmp/swapfile
) then add it as a temporary swap zone (swapon /var/tmp/swapfile
) and remove it (swapoff /var/tmp/swapfile
and rm /var/tmp/swapfile
) when you don't need it anymore.
You probably don't want to swap on a tmpfs file system like /tmp/
often is, since tmpfs file systems are backed up by swap space!.
I dislike memory overcommitment and I disable it (thru proc(5)). YMMV.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With