ART在什么条件下会压缩堆? [英] Under What Conditions Does ART Compact the Heap?

查看:131
本文介绍了ART在什么条件下会压缩堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 5.0及更高版本中ART运行时的吹捧功能之一是堆压缩,以减少堆碎片.碎片化的堆可以很容易地获得 OutOfMemoryErrors ,因为即使堆整体上有足够的可用空间,也可能没有足够的单个连续可用内存块满足您的需求.

One of the touted features of the ART runtime in Android 5.0+ is heap compaction, to reduce heap fragmentation. A fragmented heap can get OutOfMemoryErrors a lot easier, as there may not be a single contiguous free block of memory big enough for your needs, even if the heap overall has enough free space.

我了解到,基于Google会议演示等,当应用程序移至后台时会发生这种情况.但是,我可以在文档说:

I understand that this occurs when the app moves to the background, based on Google conference presentations and the like. However, the only statement that I can find on it in the documentation says:

同构空间压缩是指自由列表空间到自由列表空间的压缩,通常在应用程序移动到无法察觉的暂停过程状态时发生.这样做的主要原因是减少RAM使用率和对堆进行碎片整理.

Homogeneous space compaction is free-list space to free-list space compaction which usually occurs when an app is moved to a pause imperceptible process state. The main reasons for doing this are reducing RAM usage and defragmenting the heap.

从技术上说,尚不清楚暂停不可觉察的过程状态"的含义是什么.

It's unclear exactly what a "pause imperceptible process state" means, technically.

假设某个应用当前没有任何前台活动.开发人员是否可能做过任何事情来阻止该应用程序进程的堆压缩?例如,具有前台服务会阻止堆压缩吗?

Suppose an app does not have any foreground activities at the moment. Is there anything that the developer might have done that might prevent heap compaction for that app's process? For example, does having a foreground service block heap compaction?

推荐答案

将难题的各个部分放在一起.

Putting the pieces of the puzzle together.

根据我的判断,ART将压缩暂停2-3秒的所有内容,暂停将意味着当前未在后台运行,因此活动处于活动状态,但未运行服务.它还将即时压缩,或在应用程序处于前台时同时压缩.

From what I can determine, ART will compact anything that is paused for 2-3 seconds and by paused it means not currently running in background, so activities, but not running services. It will also compact on the fly, or concurrently while the app is in the foreground.

当前,触发堆压缩的事件是ActivityManager进程状态更改.当应用程序进入后台时,它会通知ART进程状态不再是可感知".这样一来,ART就可以执行导致应用程序线程长时间停顿的操作,例如压缩和监视器放气.

Currently, the event that triggers heap compaction is ActivityManager process-state changes. When an app goes to background, it notifies ART the process state is no longer jank "perceptible." This enables ART do things that cause long application thread pauses, such as compaction and monitor deflation.

切斯特·哈斯指出:

垃圾回收
ART带来了改进的垃圾收集动态.一方面,ART是一个动人的收藏家.当应用程序中的长时间暂停不会影响用户体验时(例如,应用程序在后台且未播放音频时),它可以压缩堆.另外,还有一个单独的堆,用于存放大对象(例如位图),从而更快地找到了这些大对象的内存,而不必花在潜在的零散的常规堆上.ART中的暂停通常在2到3毫秒的范围内.

Garbage Collection
ART brought improved garbage collection dynamics. For one thing, ART is a moving collector; it is able to compact the heap when a long pause in the application won’t impact user experience (for example, when the app is in the background and is not playing audio). Also, there is a separate heap for large objects like bitmaps, making it faster to find memory for these large objects without wading through the potentially fragmented regular heap. Pauses in ART are regularly in the realm of 2–3ms.

从我可以看出,应用程序中的任何暂停对于ART GC来说都是公平的游戏.

From what I can see any pause in the app is fair game for the ART GC.

我怀疑该应用程序需要完全暂停所有服务,以使压缩发生,因为它正在重新分配堆的内存地址,并且这种情况无法更改.由于在应用程序暂停期间(而不是在运行中)获得的更大的压缩是堆的动态重新排列.在较小的暂停中只能进行的更改是在不再使用的进程上重新路由某些地址.

I suspect the app needs to be paused completely of all services, etc for the compact to occur, as it's reallocating the memory addresses of the heap, and for this to occur, it cannot be changing. As this larger compact that is taken during the app pause and not on the fly is a dynamic rearrangement of the heap. The only changes that can be made in the smaller pauses is to re-route some addresses on processes no longer being used.

尽管这是有根据的猜测,但不是确定的,我将努力获取更多信息.

Though this is an educated guess, not definitive and I will endeavour to get more info.

此处的源代码应该具有答案.他们正在使用诸如 InJankPerceptibleProcessState()之类的命名,并试图通过这些方法进行尝试,因为您可能已经拥有了自己.

The source code here should have the answer. They're using naming like InJankPerceptibleProcessState() and trying to wade through this, as you probably already have yourself.

阅读它会在我找到确定答案的情况下更新答案.

这篇关于ART在什么条件下会压缩堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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