Linux Applications Debugging Techniques/Resource leaks

Zombie threads
Any thread that has terminated but has not been joined or detached will leak OS resources until the process terminates. Unfortunately, neither /proc nor gdb will show you these zombie threads, at least not on some kernels.

One way to get them is with a gdb canned command:

Another way is to use (again) an interposition library:

The output is a bit rough but it can be refined down to file and line by replacing backtrace_symbols_fd with appropriate code:

File descriptors
As just about anything is a file (folders, sockets, pipes, etc.), just about anything can result in a file descriptor that needs to be closed. /proc can help:

The trace_call command for gdb can help with the call stack.

If gdb is not available on the machine, an interposition library hooking open, pipe, <tt>socket</tt>, etc. can be built.

Other tools that can be used:
 * <tt>lsof</tt>
 * <tt>fuser</tt>

Ports
Which process is using a port? As root:

Other tools:
 * <tt>fuser</tt>

IPC
For semaphores, shared memory and message queues.


 * <tt>ipcs</tt>
 * <tt>ipcrm</tt>

DYI: an interposition resource counter
libmemleak can be easily modified to keep track of whatever resources are leaking. Hook the right API (e.g. <tt>open/close</tt>).