在 android 应用程序中设置最小堆大小的目的是什么? [英] What is the objective of setting the minimum heap size in an android app?

查看:29
本文介绍了在 android 应用程序中设置最小堆大小的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在适用于 Android 操作系统的 google 日历应用程序中,您将在 CalendarActivity.

In google's Calendar app for Android OS, you will encounter this line in the onCreate method of CalendarActivity.

// Eliminate extra GCs during startup by setting the initial heap size to 4MB.
VMRuntime.getRuntime().setMinimumHeapSize(INITIAL_HEAP_SIZE)

谁能解释为什么将其设置为 4MB 会消除 GC?

Can someone explain why setting it to 4MB will eliminate GCs?

推荐答案

JVM 通常从分配一个相对较小的堆开始.然后在每次 GC 运行后,它会检查有多少空闲堆内存.如果空闲堆与总堆的比率太小,JVM 将向堆添加更多内存(最多为配置的最大堆大小).

A JVM typically starts by allocating a relatively small heap. Then after each GC run it checks to see how much free heap memory there is. If the ratio of free heap to total heap is too small, the JVM will then add more memory to the heap (up to the maximum configured heap size).

第二个相关事实是,当有大量内存需要回收时,GC 的运行效率最高.假设您没有遇到整体系统资源限制(例如触发分页或交换),您可以通过使用大堆而不是小堆来获得更好的应用程序性能.

A second relevant fact is that GC runs most efficiently when there is lots of memory to reclaim. Provided that you don't run into overall system resource limits (e.g. triggering paging or swapping), you get better application performance by running with a large heap than a small one.

假设应用程序编写者知道应用程序很可能需要给定数量的堆(例如 4Mb)才能舒适地运行.通过将该大小设置为最小堆大小意味着当堆填满(例如)1Mb、2​​Mb 和 3Mb 时,JVM 不需要运行 GC.因此,JVM 在应用启动和正常运行期间运行垃圾收集器的次数更少,应用启动速度更快,用户看到的 GC 暂停更少.

Suppose that the application writer knows that the app most likely needs a given amount of heap (e.g. 4Mb) to run comfortably. By setting that size as the minimum heap size means that the JVM does not need to run the GC when the heap fills at (say) 1Mb, 2Mb and 3Mb. As a result, the JVM runs the garbage collector fewer times during application startup and normal running, the app starts up faster and the user sees fewer GC pauses.

这篇关于在 android 应用程序中设置最小堆大小的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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