wabbit
wabbit

Reputation: 143

fork failures: Cannot allocate memory

I have a program running on a linux machine. It forks a process to send a mail and often logs fork failure messages stating that it could not allocate memory.

When I check the size of resident memory it comes around 12Gb (swap is configured to be only 1Gb on this machine).

Is there a way I can be sure that this huge chunk of memory is not a leak but just memory growth?

Also, Is there a system limit that can be tweaked so that I don't get any fork failures?

Upvotes: 4

Views: 5834

Answers (2)

njahnke
njahnke

Reputation: 1387

I got this just now on an embedded system. No limits were in place and there was plenty of free RAM for a little df process, so it confused me. Then I remembered that fork() works via copy-on-write - so there is a chance that the child process might need as much RAM as the parent process in the near future. Just something to keep in mind if you see that the machine supposedly has lots of free RAM - does it have at least as much free RAM as the process calling fork() is using?

Upvotes: 1

alk
alk

Reputation: 70951

To check for memory leaks you might like to run the program under Valgrind: http://valgrind.org


To get/set limits from the console/shell there is the ulimit command available.

From inside the program the system calls getrlimit()/setrlimit() provide this functionality.


Another workaround for situations where memory might get tight due to fork()ing would be to use vfork() immediately followed by a call to a member of the exec*() family of functions.

From man vfork:

vfork() is a special case of clone(2). It is used to create new processes without copying the page tables of the parent process. It may be useful in performance-sensitive applications where a child is created which then immediately issues an execve(2).

vfork() differs from fork(2) in that the parent is suspended until the child terminates (either normally, by calling _exit(2), or abnormally, after delivery of a fatal signal), or it makes a call to execve(2). Until that point, the child shares all memory with its parent, including the stack. The child must not return from the current function or call exit(3), but may call _exit(2).

Upvotes: 3

Related Questions