Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting native memory leaks in Android JNI code

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

like image 271
Vinay Avatar asked Sep 22 '10 10:09

Vinay


1 Answers

2017 update: valgrind is available for Android. The built-in malloc debug feature was expanded significantly in Android N, and is quite useful for finding memory leaks. You may need to _exit() the app to trigger the dump.

The DDMS feature described below currently gets a brief mention in the official docs.


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.

like image 158
fadden Avatar answered Oct 05 '22 16:10

fadden