Several techniques can be used to find and isolate native code memory leaks. In general there is no single ideal solution for all platforms. The following are some techniques to diagnose leaks in native code.
A very common practice is to track all allocation and free calls of the native allocations. This can be a fairly simple process or a very sophisticated one. Many products over the years have been built up around the tracking of native heap allocations and the use of that memory.
Tools like IBM Rational Purify and the runtime checking functionality of Sun Studio dbx
debugger can be used to find these leaks in normal native code situations and also find any access to native heap memory that represents assignments to uninitialized memory or accesses to freed memory. See Find Leaks with dbx Debugger.
Not all these types of tools will work with Java applications that use native code, and usually these tools are platform-specific. Because the virtual machine dynamically creates code at runtime, these tools can wrongly interpret the code and fail to run at all, or give false information. Check with your tool vendor to ensure that the version of the tool works with the version of the virtual machine you are using.
See sourceforge for many simple and portable native memory leak detecting examples. Most of these libraries and tools assume that you can recompile or edit the source of the application and place wrapper functions over the allocation functions. The more powerful of these tools allow you to run your application unchanged by interposing over these allocation functions dynamically. This is the case with the library libumem.so
first introduced in Oracle Solaris 9 operating system update 3; see Find Leaks with libumem Tool.
If you write a JNI library, then consider creating a localized way to ensure that your library does not leak memory, by using a simple wrapper approach.
The procedure in Example 3-7 is an easy localized allocation tracking approach for a JNI library. First, define the following lines in all source files.
Then you can use the functions in Example 3-8 to watch for leaks.
The JNI library would then need to periodically (or at shutdown) check the value of the total_allocated
variable to verify that it made sense. The preceding code could also be expanded to save in a linked list the allocations that remained and report where the leaked memory was allocated. This is a localized and portable way to track memory allocations in a single set of sources. You would need to ensure that debug_free()
was called only with the pointer that came from debug_malloc()
, and you would also need to create similar functions for realloc()
, calloc()
, strdup()
, and so forth, if they were used.
A more global way to look for native heap memory leaks would involve interposition of the library calls for the entire process.
Most operating systems include some form of global allocation tracking support.
On Windows, search the MSDN library for debug support. The Microsoft C++ compiler has the /Md
and /Mdd
compiler options that will automatically include extra support for tracking memory allocation.
Linux systems have tools such as mtrace
and libnjamd
to help in dealing with allocation tracking.
Oracle Solaris operating system provides the watchmalloc
tool. Oracle Solaris 9 operating system update 3 also introduced the libumem
tool, see Find Leaks with libumem Tool.
The dbx
debugger includes the Runtime Checking (RTC) functionality, which can find leaks. The dbx
debugger is part of Oracle Solaris Studio and also available for Linux.
Example 3-9 shows a sample dbx
session.
The output shows that the dbx
debugger reports memory leaks if memory is not freed at the time the process is about to exit. However, memory that is allocated at initialization time and needed for the life of the process is often never freed in native code. Therefore, in such cases the dbx
debugger can report memory leaks that are not leaks in reality.
Note: Example 3-9 used two suppress
commands to suppress the leaks reported in the virtual machine, libjvm.so
and the Java support library, libjava.so
.
First introduced in Oracle Solaris 9 operating system update 3, the libumem.so
library and the modular debugger mdb
can be used to debug memory leaks. Before using libumem
, you must preload the libumem
library and set an environment variable as shown in Example 3-10.
Now, run the Java application, but stop it before it exits. Example 3-11 uses truss
to stop the process when it calls the _exit
system call.
At this point you can attach the mdb
debugger, as shown in Example 3-12.
The ::findleaks
command is the mdb
command to find memory leaks. If a leak is found, this command prints the address of the allocation call, buffer address, and nearest symbol.
It is also possible to get the stack trace for the allocation that resulted in the memory leak by dumping the bufctl
structure. The address of this structure can be obtained from the output of the ::findleaks
command.
See analyzing memory leaks using libumem
for troubleshooting the cause for a memory leak.