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

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

问题描述

在为Android OS谷歌的日历应用程序,你会遇到这一行的onCreate方法<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/2.3.7_r1/com/android/calendar/CalendarActivity.java#CalendarActivity"相对=nofollow> 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将消除选区?

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)的一个给定的量,以舒适地运行。通过设置大小为最小堆大小意味着JVM并不需要运行的GC当堆填充在(比方说)的1Mb,的2Mb和3MB。其结果是,在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天全站免登陆