JNI Android Memory Leak Detection

How to detect memory leaks in JNI code for Android? I use Froyo

+6
android memory-leaks jni
source share
3 answers

2017 Update: valgrind is available for Android. The built-in malloc debugging feature has been greatly expanded in Android N and is very useful for finding memory leaks. You may need an _exit() application to run the dump.

The DDMS function described below is currently being briefly mentioned in white papers.


There is an experimental, unsupported feature that you can use.

In your DDMS configuration file (for example ~/.android/ddms.cfg on Linux) add "native = true". This allows the Native Heap tab.

Then enable tracking of the placement of the native heap on the device and restart the application framework:

 % adb shell setprop libc.debug.malloc 1 % adb shell stop % adb shell start 
Please note that this only applies to the latest versions: in older versions, you also had to manually replace libc.so with libc_debug.so in / system / lib on the device.)

You can find out if the device is configured correctly by looking at the output of logcat when issuing a simple command ("adb shell ls"). If you see:

 I/libc ( 4847): ls using MALLOC_DEBUG = 1 (leak checker) 

then you know you turned it on.

Now you can use the Native Heap tabs to capture snapshots of heap memory usage.

DDMS automatically extracts symbols from .../symbols/system/lib shared libraries into the Android source tree. Of course, this requires that you have the full Android source tree, and the code built on it runs on your device. If not, stack traces cannot be decoded into character names, which reduces the usefulness of this function.

+12
source share

U need to take a picture before and after running the test. Then press the +/- button, then it will show the saved memory location.

0
source share

I used in JB, JB +, KITKAT and candy. Its a very good tool in android to find leaks.

0
source share

All Articles