Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK: Dalvik Heap and Native Heap - How Separate Between the two

I know there's Dalvik(JVM) heap and Native heap in an android platform. And the Dalvik GC has no work on native heap. But I'm not sure how this work, I mean how the Android OS separate them?

possible situation 1: composed by separate memory hardware (I don't believe much)

possible situation 2: Android OS has FIXED amount of memory for both heap

possible situation 3: Android OS has to allocate part of Dalvik memory heap to become native heap when necessary, and so the size of native heap and Dalvik heap is flexible.

Which one is true, or possibility that I didn't mention?

like image 992
Keith Mak Avatar asked Feb 03 '14 05:02

Keith Mak


2 Answers

The native heap is managed by dlmalloc(), which uses a combination of mmap() and standard calls like sbrk() to allocate memory. The managed ("Dalvik") heap is (mostly) one large chunk allocated with mmap(). It's all running on top of the Linux kernel, so if you understand Linux memory management then you already know how the lower-level parts work.

You can read more about how Dalvik returns empty pages from the managed heap to the OS in this post.

Edit: the canonical post for information about Android memory management is this one. I don't think it directly answers your question, but it has a lot of good information and links to informative sites.

like image 149
fadden Avatar answered Jan 02 '23 04:01

fadden


Since Android is open source, you can check out the source code yourself. Looks like it calls create_mspace_with_base(). I'm not exactly sure what that does, but according to this post, it maps memory from /dev/zero.

So it really is using a "separate" heap. It allocates its own memory pages directly and manages that itself.

like image 31
mpontillo Avatar answered Jan 02 '23 06:01

mpontillo