Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way, I can find the memory leak of a running process? I can use Valgrind for finding memory leaks before the start of a process. I can use GDB to attach it to a running process. How could I debug a memory leaks of a running process?

share|improve this question
    
Valgrind is very useful, I would even call it intuitive. – user400344 Jul 15 at 9:23

On Linux, you could enable mtrace in your program, but it is a code change.

On OpenBSD, you could try the malloc stats.

Google's leak checker might also be worth a look, and unlike mtrace you may be able to use LD_PRELOAD to avoid recompilation.

share|improve this answer

I think memleax, which detects memory leak of running process, is exact what you want.

  • It works like gdb and strace. It attaches a running process, hooks memory allocate/free APIs, records all memory blocks, and reports the blocks which lives longer than 5 seconds (you can change this time by -e option).
  • It's very convenient and suitable for production environment. It need not re-compiling or restarting target process, and gives memory leak report in real time.
  • It supports Linux-x86_64 and FreeBSD-amd64.

NOTE: I'm the author

share|improve this answer

I think without providing support for allocation monitoring after program start directly in the source code, you're out of luck. Here are two reasons I can think of:

  • Heap checkers initialize when the program begins. Some offer the ability to tweak the exact timing, but the environment variables that start them must be set when the program runs. This is because they watch to make sure each allocation has a corresponding deallocation, and they would miss some otherwise.
  • Heap checking usually requires elevated privileges, or hooks, to be provided by the operating system. If those hooks aren't provided at the time of program start, the heap checkers cannot leverage them. I don't believe OSes provide these privileges after the program in question is started.

If, however, you're program is running inside a virtual machine, that environment may provide support for monitoring allocations. I know Java has several allocation and garbage collection monitoring tools (like visualVM) that attach to running programs or VMs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.