好的,我等了很长时间,期待专家回答这个问题。让我分享一下我所知道的。
回答第一个问题:“如果每个应用进程都包含 Dalvik VM,它不会占用更多内存吗?”
Dalvik 堆由 zygote 预加载了类和数据(从 Android 版本 2.2 开始加载了 1900 多个类)。当 zygote fork 启动一个 android 应用程序时,新应用程序会获得这个堆的写时复制映射。正如 Dan Borstein 在下面所说,这有助于减少内存以及缩短应用程序启动时间。
It's used in Android to amortize the RAM footprint of the large amount of effectively-read-only data (technically
writable but rarely actually written) associated with common library classes across all active VM processes. 1000+
classes get preloaded by the system at boot time, and each class consumes at least a little heap for itself, including
often pointing off to a constellation of other objects. The heap created by the preloading process gets
shared copy-on-write with each spawned VM process (but again doesn't in practice get written much). This saves
hundreds of kB of dirty unpageable RAM per process and also helps speed up process startup.
对于您的 2 个问题:“我们怎么知道 VM 占用的内存大小和 Java 进程占用的大小?”
我不明白您所说的“Java 进程占用的大小” 检查链接是什么意思,以便清楚了解 dalvik 中的内存。
除了 procrank,我们还可以使用adb shell dumpsys meminfo 'your apps package name'
检查此链接 获取有关 dumpsys 的更多信息。