做Dalvik虚拟机进程释放系统内存? [英] Do Dalvik VM Processes Release System RAM?

查看:196
本文介绍了做Dalvik虚拟机进程释放系统内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android开发者文档,项目斯维尔特的一部分(座右铭:你有没有尝试配合Bugdroid成紧身牛仔裤?!),有一个页面上的管理应用程序的内存。它包含:

The Android developer documentation, as part of Project Svelte (motto: "You ever try fitting Bugdroid into skinny jeans?!?"), has a page on Managing Your App's Memory. It contains:

当用户浏览到不同的应用程序,你的UI不再可见,你应该释放所使用的只是你的用户界面的任何资源。此时释放的UI资源可以显著增加系统的容量为缓存过程,这对用户体验的质量有直接影响。

When the user navigates to a different app and your UI is no longer visible, you should release any resources that are used by only your UI. Releasing UI resources at this time can significantly increase the system's capacity for cached processes, which has a direct impact on the quality of the user experience.

TRIM_MEMORY_RUNNING_LOW :您的应用程序正在运行,并且没有考虑killable,但该设备在内存中运行的要低得多,所以你应该释放未使用的资源,以提高系统性能(这直接影响您的应用程序的性能)。

TRIM_MEMORY_RUNNING_LOW: Your app is running and not considered killable, but the device is running much lower on memory so you should release unused resources to improve system performance (which directly impacts your app's performance).

和类似物。

然而,这只会意义,如果释放资源,实际上会影响系统内存不知。

However, these would only make sense if "releasing resources" would actually affect system RAM somehow.

我下的是IM pression的Dalvik虚拟机的表现就像Java虚拟机做(或者是没有,如果他们改变了它,当我没看)。 AFAIK,Java虚拟机分配系统RAM增加堆的大小,但永远不会释放它 - 一次分配,它仍然是只要进程运行的堆空间的一部分

I was under the impression that the Dalvik VM behaved like the Java VM does (or perhaps "did", if they changed it when I wasn't looking). AFAIK, the Java VM allocates system RAM to increase the heap size but never releases it -- once allocated, it remains part of the heap space for as long as the process runs.

如果Dalvik虚拟机进行同样的操作,然后我看不出如何增加我们的过程中未分配的堆空间量会对整个系统的性能产生任何影响。现在,释放堆空间为我们的过程是一件好事,也许这样做会降低我们的需要的可能性的更多的系统RAM的未来......但是那不是文档暗示。该文件指出,在这个时候释放UI资源显著增加系统的容量缓存的过程;它并没有说释放UI资源在这个时候有没有直接的影响,但有利于减少未来的应用程序的系统内存占用。

If the Dalvik VM behaves the same way, then I fail to see how increasing the amount of unallocated heap space in our process would have any impact on the overall system performance. Now, freeing up heap space for our process is a good thing, and perhaps doing so would decrease the likelihood of us needing more system RAM in the future... but that's not what the documentation implies. The documentation states "Releasing UI resources at this time can significantly increase the system's capacity for cached processes"; it does not say "Releasing UI resources at this time has no immediate impact but will help reduce your app's system RAM footprint in the future".

现在,有说明书告诉我们要释放通过NDK分配的内存,这将是有意义的,因为这发生在Dalvik的堆外,并会影响系统内存。但文件并没有得出这样的区分。

Now, had the instructions told us to release memory allocated via the NDK, that would make sense, as that occurs outside the Dalvik heap and would affect system RAM. But the documentation does not draw that distinction.

请问Dalvik虚拟机实际上是释放内存分配回系统,除终止处理?如果是的话,什么时候?并且,在较小程度上,如何实现这完成的,考虑到垃圾收集是非压实和非复制

Does the Dalvik VM actually release allocated RAM back to the system, other than by terminating the process? If so, when? And, to a lesser extent, how is that done, considering that the garbage collector is non-compacting and non-copying?

谢谢!

推荐答案

是的。的基本思想是,如果有什么也没有关于它4K页,页面将被返回给系统。

Yes. The basic idea is that, if there's a 4K page with nothing on it, the page will be returned to the system.

,这是否在虚拟机中的函数调用 trimHeaps()在<一个href="https://android.googlesource.com/platform/dalvik/+/kitkat-release/vm/alloc/HeapSource.cpp">dalvik/vm/alloc/HeapSource.cpp.你可以用它看 mspace_trim(),它使用操作系统调用取消映射那些不再需要的块(见malloc_trim()的<一个绕线1203评论href="https://android.googlesource.com/platform/dalvik/+/kitkat-release/vm/alloc/HeapSource.cpp">malloc.c).然后,它遍历与堆 mspace_inspect_all(),其中要求到 releasePagesInRange()的每一个区域,回调测试看它是否已通过了一个区域,在它没有分配,如果是这样,截断的边界到4K对齐。如果结果不为空的,我们知道该区域跨越一个或多个物理4K页面,它可以返回到系统的madvise(MAD​​V_DONTNEED)

The function that does this in the VM is called trimHeaps(), in dalvik/vm/alloc/HeapSource.cpp. You can see it using mspace_trim(), which uses OS calls to unmap chunks that are no longer needed (see malloc_trim() comments around line 1203 in malloc.c). It then traverses the heap with mspace_inspect_all(), which calls into releasePagesInRange() for every region. The callback tests to see if it was passed a region with no allocations in it, and if so, truncates the boundaries to 4K alignment. If the result isn't empty, we know the region spans one or more physical 4K pages, which can be returned to the system with madvise(MADV_DONTNEED).

trimHeaps()从几个地方被调用时,最显着的 gcDaemonThread(),这将启动微调并发GC后五秒钟。计时器被重置,如果并发GC发生前5秒钟过的想法是,如果我们GCing则VM正忙于分配和这样的空闲时间修整会适得其反。

trimHeaps() is called from a few places, most notably gcDaemonThread(), which will initiate the trim five seconds after a concurrent GC. The timer gets reset if the concurrent GC happens before five seconds elapse, the idea being that if we're GCing then the VM is busily allocating and this sort of idle-time trimming will be counter-productive.

由于在Dalvik GC没有做压缩,这不是很有效,因为它可以。碎片往往随着时间的推移建立起来,这样的情况可能会变得更糟的时间越长一个过程的生活。该应用程序框架可以回收长住服务,以减轻这一点。

Because the Dalvik GC doesn't do compaction, this isn't as effective as it could be. Fragmentation tends to build up over time, so the situation may get worse the longer a process lives. The app framework can "recycle" long-lived services to alleviate this.

这篇关于做Dalvik虚拟机进程释放系统内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆