Detecting memory leaks in native C code ?

3,067 views
Skip to first unread message

Daman

unread,
Aug 30, 2010, 1:21:55 PM8/30/10
to android-ndk
How can I detect memory leaks in native C code (in a native lib in my
sample JNI app), in Froyo ?
Earlier I used libc_debug.so and ddms to capture allocations, but
can't do that in Froyo.
Any suggestions....

laurent mallet

unread,
Aug 31, 2010, 10:58:25 AM8/31/10
to android-ndk
on Froyo, it seems that there is:
/system/lib/libc.so
/system/lib/libc_malloc_debug_leak.so
/system/lib/libc_malloc_debug_qemu.so

In source code, you can activate malloc trace with:
setprop libc.debug.malloc 1 or 5 or 10

Warning you should restart zygote.

But it is not sufficient to see something on ddms

Something is missing

fadden

unread,
Aug 31, 2010, 6:10:21 PM8/31/10
to android-ndk
On Aug 31, 7:58 am, laurent mallet <laurent.mal...@gmail.com> wrote:
> In source code, you can activate malloc trace with:
> setprop libc.debug.malloc 1 or 5 or 10

The appropriate library is now used based on the system property.
Renaming the library is no longer required.

> But it is not sufficient to see something on ddms
>
> Something is missing

Remember to add "native=true" to your ddms.cfg to get the Native tab.

David Turner

unread,
Aug 31, 2010, 7:43:37 PM8/31/10
to andro...@googlegroups.com
Use the -memcheck option when starting the emulator. See -help-memcheck for details.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


laurent mallet

unread,
Sep 1, 2010, 3:51:27 AM9/1/10
to android-ndk
Hello,

I have the "native=true" on ddms and restarted zygote without success.
But it was on
a real device. With lsof, i'm sure that libc_debug_debug_leak is
loaded. And i see logs with malloc
checks when i use shell commands. But nothing with ddms on my app.

I will try on emulator on test app but i use OpenGL ES 2.X so i hope
to use it with a real
device (Nexus One on Froyo).

Thanks

Ps: i have readed the bionic source to understand the
libc.malloc.debug trick. so i didn't rename libc_malloc.. into
libc.so!

On 1 sep, 01:43, David Turner <di...@android.com> wrote:
> Use the -memcheck option when starting the emulator. See -help-memcheck for
> details.
>
>
>
> On Mon, Aug 30, 2010 at 7:21 PM, Daman <damanji...@gmail.com> wrote:
> > How can I detect memory leaks in native C code (in a native lib in my
> > sample JNI app), in Froyo ?
> > Earlier I used libc_debug.so and ddms to capture allocations, but
> > can't do that in Froyo.
> > Any suggestions....
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > .

Dianne Hackborn

unread,
Sep 1, 2010, 1:18:46 PM9/1/10
to andro...@googlegroups.com
Android applications don't cleanly exit, so you will never get a leak report at the end.  (And if you did, it would be huge and junky, because the platform makes no attempt to clean up all resources when exiting, because it never cleanly exits.)

To look for leaks, you should probably run your app for a while until the leak is visible and then use the memory allocation information to see where the allocation is coming from.  Note that this is the correct way to look for leaks anyway: it will show leaks that may not appear in an exit report (such as objects being put into a global list that don't get removed until the app is exiting) and doesn't show "leaks" that don't matter (such as some static initialization that just doesn't grow while running but isn't freed when shutting down).

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

mic _

unread,
Sep 1, 2010, 1:39:52 PM9/1/10
to andro...@googlegroups.com
One option is to compile an x86 version of your library and run it through Valgrind on your PC to look for memory leaks. Whether that's practical or not depends on how portable your code is and how it's organized.

/Michael


Tim Mensch

unread,
Jun 17, 2011, 6:28:59 PM6/17/11
to andro...@googlegroups.com
Sorry to resurrect an old thread, but after adding native=true to ddms.cfg and running ddms, I get a Native Heap tab -- that's entirely worthless. It never displays anything, no matter what I try.

I've tried hitting the Full Update button on:

1. My app running on a Milestone (2.1)
2. My app with debugging=true running on a Milestone (2.1)
3. My app with debugging=true running on a Droid (2.2)
4. My app with debugging=true running on an emulator (2.3.3)
5. My app on an emulator with ./emulator -avd 2.3.3 -prop libc.debug.malloc=5

Running an emulator with -memcheck seems to lock the emulator on start-up after spewing out only about 50 memcheck log messages. I gave up after waiting about 15 minutes. What I get is:

06-17 22:27:40.981: INFO/ddm-nativeheap(292): Handling NHGT chunk
06-17 22:27:41.061: DEBUG/DdmHandleNativeHeap(292): **** read 25661 bytes from '/proc/292/maps'
06-17 22:27:41.135: INFO/ddm-nativeheap(292): Sending 25681 bytes
06-17 22:27:41.201: INFO/dalvikvm(292): dvmDdmHandleHpsgChunk(when 1, what 1, heap 1)

Docs on this seem to be rather light. I've got a pain of an intermittent crash, and I'd like to eliminate the possibility that the Android code is somehow leaking resources. I've dug around for something approaching documentation on these topics, but I've come up empty. Any help would be appreciated, even if it's to point me at the docs or howto that I missed. :)

Tim

Peter Teoh

unread,
Jun 17, 2011, 10:09:14 PM6/17/11
to android-ndk
since u are resurrecting an older thread - FYI - there is a newer
development in Valgrind on Android since Feb 2011 and it seemed
successful?

https://bugs.kde.org/show_bug.cgi?id=266035
> >     <mailto:di...@android.com>> wrote:
> >     > Use the -memcheck option when starting the emulator. See
> >     -help-memcheck for
> >     > details.
>
> >     > On Mon, Aug 30, 2010 at 7:21 PM, Daman <damanji...@gmail.com
> >     <mailto:damanji...@gmail.com>> wrote:
> >     > > How can I detect memory leaks in native C code (in a native
> >     lib in my
> >     > > sample JNI app), in Froyo ?
> >     > > Earlier I used libc_debug.so and ddms to capture allocations, but
> >     > > can't do that in Froyo.
> >     > > Any suggestions....
>
> >     > > --
> >     > > You received this message because you are subscribed to the
> >     Google Groups
> >     > > "android-ndk" group.
> >     > > To post to this group, send email to
> >     andro...@googlegroups.com <mailto:andro...@googlegroups.com>.
> >     > > To unsubscribe from this group, send email to
> >     > > android-ndk...@googlegroups.com
> >     <mailto:android-ndk%2Bunsu...@googlegroups.com><android-ndk%2Bunsubscri be@googlegr
> >     oups.com <http://oups.com>>
> >     > > .
> >     > > For more options, visit this group at
> >     > >http://groups.google.com/group/android-ndk?hl=en.
>
> >     --
> >     You received this message because you are subscribed to the Google
> >     Groups "android-ndk" group.
> >     To post to this group, send email to andro...@googlegroups.com
> >     <mailto:andro...@googlegroups.com>.
> >     To unsubscribe from this group, send email to
> >     android-ndk...@googlegroups.com
> >     <mailto:android-ndk%2Bunsu...@googlegroups.com>.
> >     For more options, visit this group at
> >    http://groups.google.com/group/android-ndk?hl=en.
>
> > --
> > Dianne Hackborn
> > Android framework engineer
> > hack...@android.com <mailto:hack...@android.com>

Tim Mensch

unread,
Jun 17, 2011, 10:29:45 PM6/17/11
to andro...@googlegroups.com
Interesting; I'll have to dig more, but my first thought is that
valgrind would need to be run on Dalvik, unless there's a way to make it
run on just a shared object.

I may have tracked down my issue regardless; time to play through 40
levels again to find out. :/

Tim

Olivier Guilyardi

unread,
Jun 19, 2011, 9:58:15 AM6/19/11
to andro...@googlegroups.com
Hello Tim,

give a try to this minimalistic Malloc Debug Library:
http://www.hexco.de/rmdebug/index.html

It's easy to integrate with a few minor changes (replace fprintf with
__android_log_*, etc..).

Olivier

Tim Mensch

unread,
Jun 19, 2011, 1:13:04 PM6/19/11
to andro...@googlegroups.com
On 6/19/2011 7:58 AM, Olivier Guilyardi wrote:
> give a try to this minimalistic Malloc Debug Library:
> http://www.hexco.de/rmdebug/index.html
>
> It's easy to integrate with a few minor changes (replace fprintf with
> __android_log_*, etc..).
Thanks Olivier, I might have to try that at some point, though I'm
pretty sure that I've narrowed it down at this point, and if I'm right,
it not a malloc bug. But it can't hurt to have something like that
around for future issues. :)

Tim

Reply all
Reply to author
Forward
0 new messages