Memory leak of fork()


 
Thread Tools Search this Thread
Top Forums Programming Memory leak of fork()
# 1  
Old 05-08-2006
Question Memory leak of fork()

Today, I wrote a test code for fork/execvp/waitpid. In the parent process, it fork 100 child processes which only execute "date" to print the current datetime. When any child process die, the parent process will receive a SIGCHLD signal. Then, the parent process will re-fork-execvp the child process again.

I find the system memory(NOT includes the buffers and caches) will go higher and higher and even I shutdown all of the parent process and the child processes, it will not go down. Why?

My environment is Linux 2.6.9 and g++ 3.4.2

The following is my code:

//////////////////////////////////////////////////////////////////////////////////
// fork.cpp
// g++ -Wall fork.cpp -o fork

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

char* argv[] = { "data", (char*)0 };

void run()
{
// Child process
if (fork() == 0)
if (execvp("date", argv) < 0)
exit(0);
}

// SIGCHLD signal handler
void sig_child(int sig)
{
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
run();
}

int main()
{
// Setup signal handler
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sig_child;
sigemptyset(&act.sa_mask);
if (sigaction(SIGCHLD, &act, 0) < 0)
return -1;

// Run 100 child processes
for (int n = 0; n < 100; n++)
run();

// Zzz...
for (;Smilie
pause();
return 0;
}

//////////////////////////////////////////////////////////////////////////////////

Thanks,
Richard
# 2  
Old 05-10-2006
What is run() doing in sig_child()? That's an infinite loop, whenever one child exits you create another.
# 3  
Old 05-10-2006
Thanks,

Yes, That's what I want.

My question is after fork thousands and thousands processes. The memory usage will go higher and higher. Even I kill the parent process and all of child process(actually, they will exit very quickly.) The memory does not go down. It looks like memory leak.
# 4  
Old 05-10-2006
If this is Linux, try 'cat /proc/meminfo' to see if it shows where the memory went.

It might even be in signal queues or something. Lots of processes means lots of signals.
# 5  
Old 05-11-2006
Quote:
Originally Posted by Corona688
If this is Linux, try 'cat /proc/meminfo' to see if it shows where the memory went.

It might even be in signal queues or something. Lots of processes means lots of signals.
After check /proc/meminfo, I found the memory gone is LowFree. I think this means kernel memory leak, right?
# 6  
Old 05-11-2006
Quote:
Originally Posted by whererush
After check /proc/meminfo, I found the memory gone is LowFree. I think this means kernel memory leak, right?
Is MemFree shrinking? What about LowTotal? MemFree should equal HighFree + LowFree. It could be that the kernel just gave you some of its unused memory and has no reason to reclaim it.
# 7  
Old 05-11-2006
It could be. I know that "low" memory is important to the kernel and is a somewhat limited resource, but that's all I know about it. Hard for me to say whether it's leaking or just caching something somewhere without being intimately familiar with how the kernel manages memory... Also, I learned about the 'slabtop' command a few weeks ago, which shows some little-known things like inode caches that aren't counted by top; these caches can sometimes cause the typical newbie "OMG linux sez 0 bytes free!!" panic in people that otherwise know better Smilie

What kernel version are you using? If it's a genuine leak, upgrading might help. Also, this might get better viewage in the Linux subforum here, since your program as far as I can tell doesn't have any direct leaks.

One way to test I suppose... If low memory isn't really leaking, nothing should happen when you let it "run out", but if it really does run out of low memory, the kernel will not be happy. I'm not sure whether it'd panic or kill processes or what, but something ought to happen. Run from a real console(ctrl-alt-F1 though F6 brings you to consoles 1-6, ctrl-alt-F7 brings you back to the window manager) to let you see kernel messages.

Last edited by Corona688; 05-11-2006 at 12:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Help regarding memory leak in this C program

I have written this code in C which reads a very large collection of text files and does some processing. The problem with this code is that there are memory leaks which I am not able to figure out as to where the problem is. When I run this code, and see the memory usage using top command, then I... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

2. Red Hat

Memory leak

Hi all I am using RED HAT 5.4, and i am getting memory uses problem. when i use "sync;echo 3 > /proc/sys/vm/drop_cache" command the memory will release after 2,3 hour memory show 95%. pls suggest right way. thanks (37 Replies)
Discussion started by: reply.ravi
37 Replies

3. Programming

Memory Leak

Hi, I am trying a database server which keeps a B+ plus tree structure and works on it. I am trying to find the memory used/leak while executing this process. I check the memory leak by using ps uax command. When i execute a delete query i am sure that my code frees up the existing... (9 Replies)
Discussion started by: kumaran_5555
9 Replies

4. HP-UX

Problem with memory leak

Hi, This is my 1st post here. I am facing a rather weired problem. I have a C++ binary running on HP-UX. The output of top as well as glance shows periodic increase in memory. But when i use gdb to detect it, i dont get the desired output...gdb says...no leaks found. Just to mention that I have... (2 Replies)
Discussion started by: kshk123
2 Replies

5. IP Networking

memory leak?

Hi All, my client server application can work in two modes: 1) one direction - only client sends msgs to server 2) two directions - server gives 'answers' to client. when program run in the first mode it looks OK, but when server answers to client than client's application exit its... (2 Replies)
Discussion started by: lenna
2 Replies

6. UNIX for Advanced & Expert Users

Need to create a memory leak

Hi. This might be a strange request, but does anyone have any idea on a simple shell script that would use more and more memory as it ran? Like a purposeful leak. I want to test the behaviour of an already running program when the machine runs out of memory. Thanks! (4 Replies)
Discussion started by: rebelbuttmunch
4 Replies

7. UNIX for Advanced & Expert Users

Memory leak while using pthread_cancel()

I tried to execute a sample pthread program to cancel a newly created one using pthread_cancel(). but using valgrind on my code shows some memory leak. My Code: #include "iostream" #include "unistd.h" #include "pthread.h" #include "signal.h" using namespace std; void handler(int); void*... (4 Replies)
Discussion started by: kcr
4 Replies

8. UNIX for Advanced & Expert Users

Check memory leak

I am running c++ code on AIX unix.I have a doubt that my code is using some memory but it is not clearing that.Some time i am getting heap allocation problem.In my code i am not using any malloc,new functions also i am justing using pointers and arrays. Is there any way i can find out if the... (2 Replies)
Discussion started by: ukatru
2 Replies

9. Programming

about virtual memory and memory leak

Hi, First of all I appreciate this group very much for its informative discussions and posts. Here is my question. I have one process whose virtual memory size increases linearly from 6MB to 12MB in 20 minutes. Does that mean my process has memory leaks? In what cases does the... (4 Replies)
Discussion started by: shriashishpatil
4 Replies
Login or Register to Ask a Question