4

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?

2
  • 1
    You want to say the process had eaten up 12GB? How much memory does the machine provide? What are the memory limits set on the machine? Commented Aug 22, 2013 at 8:05
  • I mean RES which shows up using TOP command. The system here has 32Gb as memory and max memory size in ulimit shows unlimited. Commented Aug 22, 2013 at 8:45

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is informative. valgrind does not show any significant leaks so it guess it was memory growth. I can not change the program to set resource limits, can this be done on system level using ulimit or something like that? vfork() looks like one suitable solution but that will again require code changes.
@user1032035: Please see man bash for a detailed description on ulimit.
1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.