views:

172

answers:

1

How to detect Memory leaks in Android JNI code? I am using Froyo

+1  A: 

There is an experimental, unsupported feature that you may be able to take advantage of.

In your DDMS configuration file (e.g. ~/.android/ddms.cfg on Linux), add "native=true". This enables the Native Heap tab.

Next, enable native heap allocation tracking on the device, and restart the app framework:

% adb shell setprop libc.debug.malloc 1 
% adb shell stop 
% adb shell start 

(Note this requires root. Note also that this only applies to recent releases; on older releases you also needed to manually replace libc.so with libc_debug.so in /system/lib on the device.)

You can tell if you've got the device configured correctly by watching the logcat output while issuing a simple command ("adb shell ls"). If you see:

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

then you know you've enabled it.

Now you can use the Native Heap tab features to grab snapshots of heap memory usage.

DDMS will automatically extract symbols from the .../symbols/system/lib shared libraries in your Android source tree. Of course, this requires that you have a full Android source tree, and your device is running code built from it. If not, the stack traces can't be decoded to symbol names, which reduces the usefulness of the feature.

fadden
I did this, but we cannot confirm that it is a memory leak, it just displays the memory allocations.
Vinay
Right. You need to watch the allocations over time. If they're growing continuously you figure out where the growth is from the stack trace, and track it down from there. There is no valgrind for Android yet.
fadden