JavaSE虚拟机GC优化指南--Ergonomics

特别说明: 翻译内容仅供参考,如果有错误的地方,特别是技术相关描述请及时指正。画删除线的表示没翻译,画下划线的表示表述可能有问题。 


Ergonomics is the process by which the Java Virtual Machine (JVM) and garbage collection tuning, such as behavior-based tuning, improve application performance. The JVM provides platform-dependent default selections for the garbage collector, heap size, and runtime compiler. These selections match the needs of different types of applications while requiring less command-line tuning. In addition, behavior-based tuning dynamically tunes the sizes of the heap to meet a specified behavior of the application.

 人机工程学(我真不知道这个应该怎么翻译)是:为了提高应用程序性能,人为的对Java虚拟机(JVM)和GC进行调优(例如基于行为的调优)的过程。JVM根据平台差异,为GC、堆大小和运行时编译器提供不同的默认值。这些默认值符合不同类型应用程序的需求,同时需要较少的命令行调整。 此外,基于行为的调整会动态调整堆的大小,以满足应用程序的指定行为。

 

This section describes these default selections and behavior-based tuning. Use these defaults first before using the more detailed controls described in subsequent sections.

 本节描述一些默认设置和基于行为的调优。在使用后续部分中介绍的更详细的参数控制之前,请首先使用这些默认值。

Garbage Collector, Heap, and Runtime Compiler Default Selections

A class of machine referred to as a server-class machine has been defined as a machine with the following:

  • 2 or more physical processors
  • 2 or more GB of physical memory

On server-class machines, the following are selected by default:

  • Throughput garbage collector
  • Initial heap size of 1/64 of physical memory up to 1 GB
  • Maximum heap size of 1/4 of physical memory up to 1 GB
  • Server runtime compiler

For initial heap and maximum heap sizes for 64-bit systems, see the section Default Heap Size in The Parallel Collector.

GC、堆和运行时编译器的默认设置

一个典型的作为服务器的主机定义如下:

  • 2个或者以上的处理器
  • 2G以上的物理内存

在服务器上,默认值如下:

  • 吞吐量GC(后续会对GC分类)
  • 初始堆大小为1/64的物理内存,最大为1 G
  • 最大堆大小为物理内存的1/4,最大为1 G
  • 服务端运行时编译器

对于64位系统(所以上述的默认选择应该是低于64位的服务器)的初始堆大小和最大堆大小,请参阅Parallel Collector中的默认堆大小部分(这部分连接等翻译后加上)。

The definition of a server-class machine applies to all platforms with the exception of 32-bit platforms running a version of the Windows operating system. Table 2-1, "Default Runtime Compiler", shows the choices made for the runtime compiler for different platforms.

上述服务器主机的定义适用于所有平台,但运行Windows操作系统版本的32位平台除外。 下表显示了针对不同平台默认运行时编译器。 

PlatformOperating System DefaultFoot1 Default if Server-ClassFootref1
i586LinuxClientServer
i586WindowsClientClient(Foot2
SPARC (64-bit)SolarisServerServer(Foot3
AMD (64-bit)LinuxServerServer(Footref3
AMD (64-bit)WindowsServerServer(Footref3

Footnote1:Client means the client runtime compiler is used. Server means the server runtime compiler is used.

Foot1、Footref1:Client 表示选择的是客户端运行时编译器,Server表示选择的是服务端运行时编译器

Footnote2:The policy was chosen to use the client runtime compiler even on a server class machine. This choice was made because historically client applications (for example, interactive applications) were run more often on this combination of platform and operating system.

Foot2:Windows平台不管是服务器还是非服务器都选择客户端运行时编译器,是因为从客户端应用程序(例如,交互式应用程序)的历史经验来看,在这种平台与操作系统结合的系统中使用应用程序比使用服务的频率更高。(真不晓得什么意思,有可能是说像windows这种平台,应用的开启与关闭比服务更频繁)

Footnote3:Only the server runtime compiler is supported.

Foot3、Footref3:只支持服务端运行时编译器


Behavior-Based Tuning

For the parallel collector, Java SE provides two garbage collection tuning parameters that are based on achieving a specified behavior of the application: maximum pause time goal and application throughput goal; see the section The Parallel Collector. (These two options are not available in the other collectors.) Note that these behaviors cannot always be met. The application requires a heap large enough to at least hold all of the live data. In addition, a minimum heap size may preclude reaching these desired goals.

基于行为的调优 

对于并行GC来说,JavaSE平台提供了两个参数针对特定目的的调优:最大停顿时间吞吐量(这两个参数只对并行GC有效),具体参考The Parallel Collector章节。但是,这两个特定的目的不一定总是能达到。应用程序需要至少有足够的堆来存放所有存活数据。另外,最小堆的大小有可能会妨碍达到这些预期目标。(此处搞不明白为什么是最小堆大小而不是最大堆大小)

Maximum Pause Time Goal

The pause time is the duration during which the garbage collector stops the application and recovers space that is no longer in use. The intent of the maximum pause time goal is to limit the longest of these pauses. An average time for pauses and a variance on that average is maintained by the garbage collector. The average is taken from the start of the execution but is weighted so that more recent pauses count more heavily. If the average plus the variance of the pause times is greater than the maximum pause time goal, then the garbage collector considers that the goal is not being met.

The maximum pause time goal is specified with the command-line option -XX:MaxGCPauseMillis=<nnn>. This is interpreted as a hint to the garbage collector that pause times of <nnn> milliseconds or less are desired. The garbage collector will adjust the Java heap size and other parameters related to garbage collection in an attempt to keep garbage collection pauses shorter than <nnn> milliseconds. By default there is no maximum pause time goal. These adjustments may cause garbage collector to occur more frequently, reducing the overall throughput of the application. The garbage collector tries to meet any pause time goal before the throughput goal. In some cases, though, the desired pause time goal cannot be met. 

最大暂停时间目标

暂停时间是GC停止应用程序并回收不再使用的内存空间的持续时间。最大暂停时间的目的就是限制这些暂停的最长时间。GC维护这平均暂停时间和该平均值的方差。平均值是从执行开始时获取的,但是是经过加权了的,暂停越频繁那么权重就越高。如果平均值 * 平均值方差 >  最大暂停时间,那么表示该最大暂停时间的优化目标未达到。

最大暂停时间目标优化通过命令行:-XX:MaxGCPauseMillis=<nnn>,这个行命令告诉GC期望暂时时间在<nnn>毫秒以内,当然越小越好。GC会自动调整堆的大小和其他相关的GC参数来尝试满足暂停时间小于<nnn>毫秒。默认情况下是没有最大暂停时间,这个调整(应该是指最大暂停时间)可能导致频繁的进行GC,从而降低了应用程序的整体吞吐量。以为GC需要先满足最大暂停时间,而不是吞吐量。但是,在某些情况下,可能连最大暂停时间的目标也不能达到。

Throughput Goal

The throughput goal is measured in terms of the time spent collecting garbage and the time spent outside of garbage collection (referred to as application time). The goal is specified by the command-line option -XX:GCTimeRatio=<nnn>. The ratio of garbage collection time to application time is 1 / (1 + <nnn>). For example, -XX:GCTimeRatio=19 sets a goal of 1/20th or 5% of the total time for garbage collection.

The time spent in garbage collection is the total time for both the young generation and old generation collections combined. If the throughput goal is not being met, then the sizes of the generations are increased in an effort to increase the time that the application can run between collections. 

 吞吐量目标

吞吐量目标是通过GC花费的时间和GC之外花费的时间(称为应用程序时间)来衡量的。通过命令行:-XX:GCTimeRatio=<nnn>来设置。该设置表示GC回收使用的时间和应用程序使用的时间比为1:(1+<nnn>)。例如:-XX:GCTimeRatio=19,意思是1/20或者5%的时间用于GC。

这个时间是GC花费在年轻代(请不要再叫新生代了,新生代只是年代中最大的一块老年代的内存回收时间之和(不是分开算)。如果设置的吞吐量优化目标没有达到,那么增加代的大小这里可以理解为堆的大小,为什么增加大小就可以提高系统的吞吐量?因为堆越大,GC次数就越少,但是堆越大每次GC的时间会变长)从而增加应用程序的运行时间。

 Footprint Goal

If the throughput and maximum pause time goals have been met, then the garbage collector reduces the size of the heap until one of the goals (invariably the throughput goal) cannot be met. The goal that is not being met is then addressed.

 Footprint Goal(我不知道这个该怎么翻译)

如果吞吐量和最大暂停时间两个目标都达到了,那么GC会减少对的大小,直到其中一个目标(始终是吞吐量)不满足,然后再解决未实现的目标。(说真的,搞不懂为什么,是不是要做到刚刚好不浪费。)

Tuning Strategy

Do not choose a maximum value for the heap unless you know that you need a heap greater than the default maximum heap size. Choose a throughput goal that is sufficient for your application.

The heap will grow or shrink to a size that will support the chosen throughput goal. A change in the application's behavior can cause the heap to grow or shrink. For example, if the application starts allocating at a higher rate, the heap will grow to maintain the same throughput.

If the heap grows to its maximum size and the throughput goal is not being met, the maximum heap size is too small for the throughput goal. Set the maximum heap size to a value that is close to the total physical memory on the platform but which does not cause swapping of the application. Execute the application again. If the throughput goal is still not met, then the goal for the application time is too high for the available memory on the platform.

If the throughput goal can be met, but there are pauses that are too long, then select a maximum pause time goal. Choosing a maximum pause time goal may mean that your throughput goal will not be met, so choose values that are an acceptable compromise for the application.

It is typical that the size of the heap will oscillate as the garbage collector tries to satisfy competing goals. This is true even if the application has reached a steady state. The pressure to achieve a throughput goal (which may require a larger heap) competes with the goals for a maximum pause time and a minimum footprint (which both may require a small heap). 

调优策略 

不要设置堆的最大值,除非你知道你需要的堆内存大于默认的最大值(这个是可以计算,#GC、堆和运行时编译器的默认设置#有说明)。选择一个满足你应用的吞吐量目标(其实就是GC与应用时间占比问题)

堆的大小会随着选择的吞吐量目标增加或者减少。应用程序的行为的改变也会导致堆的增加和减少,例如:应用程序开始高速率的分类内存,那么堆将变大,以满足吞吐量不变。

如果堆已经增长到了最大值,而且还没有满足吞吐量的目标。那么将最大堆大小设置为接近平台上总物理内存但不会引起应用程序使用交换分区的值(这里需要自行了解一下什么叫交换分区,为什么要有交换分区)。再次执行应用程序,如果还是不能满足吞吐量目标,那么换主机或者加内存。

如果能满足吞吐量目标,但是暂时时间太长,那么不妨选择最大暂停时间目标优化。当然,选择了最大暂停时间目标优化有可能吞吐量就无法满足,那么你可以选择一个折中的值。

通常,GC在满足有竞争的优化目标(比如吞吐量和暂停时间,吞吐量需要堆大,但是堆大了暂停时间定会增加,两者相互矛盾)是对的大小会出现抖动。即使是应用程序,在趋于稳定的状态下也会出现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值