简体   繁体   English

Android NDK:Dalvik Heap和Native Heap - 两者之间的区别

[英]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. 我知道在Android平台上有Dalvik(JVM)堆和Native堆。 And the Dalvik GC has no work on native heap. Dalvik GC在本机堆上没有工作。 But I'm not sure how this work, I mean how the Android OS separate them? 但我不确定这是如何工作的,我的意思是Android操作系统是如何将它们分开的?

possible situation 1: composed by separate memory hardware (I don't believe much) 可能的情况1:由独立的内存硬件组成(我不相信太多)

possible situation 2: Android OS has FIXED amount of memory for both heap 可能的情况2:Android操作系统对两个堆都有固定的内存量

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. 可能的情况3:Android OS必须在必要时将部分Dalvik内存堆分配为本机堆,因此本机堆和Dalvik堆的大小是灵活的。

Which one is true, or possibility that I didn't mention? 哪一个是真的,或者我没有提到的可能性?

The native heap is managed by dlmalloc() , which uses a combination of mmap() and standard calls like sbrk() to allocate memory. 本机堆由dlmalloc()管理,它使用mmap()和标准调用(如sbrk()来分配内存。 The managed ("Dalvik") heap is (mostly) one large chunk allocated with mmap() . 托管(“Dalvik”)堆(大部分)是使用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. 它全部运行在Linux内核之上,所以如果您了解Linux内存管理,那么您已经知道了低级部件的工作原理。

You can read more about how Dalvik returns empty pages from the managed heap to the OS in this post . 您可以在本文中阅读有关Dalvik如何将空页从托管堆返回到操作系统的更多信息

Edit: the canonical post for information about Android memory management is this one . 编辑:有关Android内存管理信息的规范帖子就是这个 I don't think it directly answers your question, but it has a lot of good information and links to informative sites. 我不认为它直接回答你的问题,但它有很多很好的信息和信息网站的链接。

Since Android is open source, you can check out the source code yourself . 由于Android是开源的,您可以自己查看源代码 Looks like it calls create_mspace_with_base() . 看起来它调用create_mspace_with_base() I'm not exactly sure what that does, but according to this post , it maps memory from /dev/zero . 我不确定那是做什么的,但根据这篇文章 ,它从/dev/zero映射内存。

So it really is using a "separate" heap. 所以它真的是使用“独立”堆。 It allocates its own memory pages directly and manages that itself. 它直接分配自己的内存页面并自行管理。

It is almost your second case 这几乎是你的第二种情况

There are 2 separate heaps , one for the runtime Art (previously DalvikVM ), and one for the native programs. 有两个单独的heaps ,一个用于runtime Art (以前是DalvikVM ),另一个用于本native程序。

You can easily see these two different areas by performing a cat on the maps pseudo-file of the proc filesystem. 您可以通过在proc文件系统的maps伪文件上执行cat来轻松查看这两个不同的区域。

Consider the following output: 考虑以下输出:

2a028000-2a029000 rw-p 00000000 00:00 0          [heap]
b6400000-b6c00000 rw-p 00000000 00:00 0          [anon:libc_malloc]

In the above example, the first area, that is only 1 page long, it is managed by the ART Runtime . 在上面的例子中,第一个区域只有1页长,它由ART Runtime管理。 Both dlmalloc and rosalloc are supported, but ART uses rosalloc since it is faster. 支持dlmallocrosalloc ,但ART使用rosalloc因为它更快。 This area, in contrast to what @fadden said (at least for Lollipop), is managed by sbrk . 与@fadden所说的(至少对于Lollipop)相比,这个区域由sbrk管理。 It grows upwards . upwards

The second area, that is 2048 pages long, it is the native heap . 第二个区域,即2048页长,它是本native heap It is used by the bionic library, which is an implementation of libc for Android. 它由bionic库使用,它是Android的libc实现。 Both dlmalloc and jemalloc are supported, and the one that it is being used depends on the device. dlmallocjemalloc都受支持,正在使用的那个取决于设备。 I haven't found a call that extends this heap, but I guess mmap would suffice. 我还没有找到扩展这个堆的调用,但我想mmap就足够了。 It grows downwards , towards the runtime heap. downwards runtime ,朝向runtime堆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM