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 memory leak?. I just have preliminary idea that if you donot free the memory which is not in use, u have memory leak in ur program.

It would be really great if someone can post info about memory leak. Some link would do.

Thanks in advance,

-Ashish

could be due to the allocation logic added in the code to reclaim discarded memory

have signal handlers as guards in your program,
whenever the program crashes due to illegal access
somehow you need to free the memory acquired through malloc

dangling pointers is one such case
just assign NULL to freed members

programs resisting to release the memory (heap) eventually increases the heap size

The system calls brk() or sbrk() on behalf of the process to get chunks of memory.
As the program runs and requests more memory, the system calls for more.

Generally, this memory accumulates, with the idea that since the processes needed it before, it may well need it again. Calling brk() & sbrk() is expensive.

The simple way to see a memory leak is have the code run for longer periods. If memory continually grows, then there is probably problem.

If you need to find the leak - try either Electric Fence or Valgrind.
They are available free, and have been ported to most versions of UNIX.

I have a C program that I suspect of memory leak.
However, the memory leak occurs when I kill -9 the program and restart the program to run as a daemon.
I didn't see the memory leak problem when the daemon is not killed and restarted at least 5 times.

The CPU usage on the server drop to ZERO everytime I kill the process BUT whenever I restart the process, it consume 2% more than it did the previous time.

Is this possible? I dun understand why did the CPU util dropped to ZERO after I kill the porcess if it is a memory leak.

Why not create a SIGTERM signal handler that does a graceful exit? Graceful meaning you call an atexit() handler to clean up memory.

kill -9 is generally a bad idea. Even if you don't write an exit handler, do not use
SIGKILL (kill -9) because it can prevent any further operations by the system on behalf of the process. The process leaves junk behind when this happens.