2

In Android, when I inspect application memory using adb shell dumpsys meminfo, I observe separate Java and native heaps, but only 1 entry for stack.

              Pss  Private  Private  Swapped     Heap     Heap     Heap
             Total    Dirty    Clean    Dirty     Size    Alloc     Free
             ------   ------   ------   ------   ------   ------   ------
Native Heap     4516     4480        0     1848    11520     7412     4107
Dalvik Heap     9726     9668        0    12924    33436    28477     4959
Dalvik Other     1417     1416        0       28                           
      Stack      288      288        0        0

I want to ask whether in Android, when a java class uses native code via JNI, is the native stack allocated in contiguous memory location from the java stack, or are they non-contiguous (really two separate stacks) ?

From a description of the JVM, it appears that the java stack and native stack are contiguous (but I can't confirm that this image indeed indicates that, or just the page author drew them next to each other).

Also, does anyone have a picture that shows how memory management is done in Dalvik/ART ? I know several SO questions exist, but I still cannot get a good understanding, specifically for:

  1. Difference between java stack/heap and native stack/heap
  2. Shared library locations
Jake
  • 16,329
  • 50
  • 126
  • 202
  • Why do think you need to know this? AFAIK, any native stack in JNI will be "popped" (stack pointer reset) after you return to Java, so I don't see what you can do with said memory. – markspace Feb 21 '16 at 22:00
  • I am exploring sandboxing solutions. For e.g. check this existing work I found: https://taesoo.gtisc.gatech.edu/pubs/2016/flexdroid/flexdroid.pdf – Jake Feb 21 '16 at 22:04

1 Answers1

1

A reasonable implementation of the execution stack mixes Java and native frames in a single stack. That is, if a Java method calls a native function, which calls a Java method, the frames corresponding to the calls are all pushed on the same stack.

In general terms, the native heap is a storage area that is used for dynamically allocated memory. The Java heap is an area within the native heap reserved for Java objects, and its contents are managed by the garbage collector. Depending on the garbage collector implementation, Java heap may be contiguous or it could be split into separate areas.

I'm not familiar with the specifics of Dalvik or ART, but they are probably like other JVMs in this respect.

Joni
  • 108,737
  • 14
  • 143
  • 193