R function mcfork
is only a wrapper to the syscall fork
(BtW, the man page says, that this call is itself a wrapper to the clone
)
I created a simple C++ program to test fork
's behaviour:
#include <stdio.h>
#include <unistd.h>
#include<vector>
int main(int argc, char **argv)
{
printf("--beginning of program\n");
std::vector<std::vector<int> > l(50000, std::vector<int>(50000, 0));
// while (true) {}
int counter = 0;
pid_t pid = fork();
pid = fork();
pid = fork();
if (pid == 0)
{
// child process
int i = 0;
for (; i < 5; ++i)
{
printf("child process: counter=%d\n", ++counter);
}
}
else if (pid > 0)
{
// parent process
int j = 0;
for (; j < 5; ++j)
{
printf("parent process: counter=%d\n", ++counter);
}
}
else
{
// fork failed
printf("fork() failed!\n");
return 1;
}
printf("--end of program--\n");
while (true) {}
return 0;
}
First, the program allocates about 8GB data on heap.
Then, it spawns 2^2^2 = 8 children via fork call and waits to be killed by the user, and enters an infinite loop to be easy to spot on task manager.
Here are my observations:
- For the fork to succeed, you need to have at least 51% free memory on my system, but this includes swap. You can change this by editing
/proc/sys/vm/overcommit_*
proc files.
- As expected, none of the children take more memory, so this 51% free memory remains free throughout course of the program, and all subsequent forks also don't fail.
- The memory is shared between the forks, so it gets reclaimed only after you killed the last child.
Memory fragmentation issue
You should not be concerned about any layer of memory fragmentation with respect to fork. R's memory fragmentation doesn't apply here, because fork operates on virtual memory. You shouldn't worry about fragmentation of physical memory, because virtually all modern operating systems use virtual memory (which consequently enables them to use swap). The only memory fragmentation that might be of issue is a fragmentation of virtual memory space, but AFAIK on Linux virtual memory space is 2^47 which is more than huge, and for many decades you should not have any problems with finding a continuous regions of any practical size.
Summary:
Make sure you have more swap then physical memory, and as long as your computations don't actually need more memory then you have in RAM, you can mcfork
them as much as you want.
Or, if you are willing to risk stability (memory starvation) of the whole system, try echo 1 >/proc/sys/vm/overcommit_memory
as root on linux.
Or better yet: (more safe)
echo 2 >/proc/sys/vm/overcommit_memory
echo 100 >/proc/sys/vm/overcommit_ratio
You can read more about overcommiting here: https://www.win.tue.nl/~aeb/linux/lk/lk-9.html