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

本文探讨Java Platform, Standard Edition (Java SE)下不同垃圾回收器(GC)的特性与选择策略,针对多样化的应用需求,如大型数据处理、多线程及高性能场景,介绍GC如何影响应用性能,以及如何根据特定需求选择与调优。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


A wide variety of applications use Java Platform, Standard Edition (Java SE), from small applets on desktops to web services on large servers. In support of this diverse range of deployments, the Java HotSpot virtual machine implementation (Java HotSpot VM) provides multiple garbage collectors, each designed to satisfy different requirements. This is an important part of meeting the demands of both large and small applications. Java SE selects the most appropriate garbage collector based on the class of the computer on which the application is run. However, this selection may not be optimal for every application. Users, developers, and administrators with strict performance goals or other requirements may need to explicitly select the garbage collector and tune certain parameters to achieve the desired level of performance. This document provides information to help with these tasks. First, general features of a garbage collector and basic tuning options are described in the context of the serial, stop-the-world collector. Then specific features of the other collectors are presented along with factors to consider when selecting a collector.

使用java平台的应用种类繁多,从桌面小型应用到大型web服务。为了支持不同部署环境,JVM为此提供了多种GC来满足不同的需求。JavaSE会根据应用运行的环境(平台类型/计算机类型)自动选择最合适的GC。但是,这个选择不一定是最优的。用户、开发人员、管理员可以根据严格的性能目标或者其他需求自主选择GC和设置各种参数来达到预期目标。首先,GC的一般特性和基本的调优选项将在serial GC中描述,stop the world 收集器(意思是serial GC工作时STW)。然后,其他GC具体特性以及选择GC时要考虑的因素将在选择GC时讨论。

 

A garbage collector (GC) is a memory management tool. It achieves automatic memory management through the following operations:

  • Allocating objects to a young generation and promoting aged objects into an old generation.
  • Finding live objects in the old generation through a concurrent (parallel) marking phase. The Java HotSpot VM triggers the marking phase when the total Java heap occupancy exceeds the default threshold. See the sections Concurrent Mark Sweep (CMS) Collector and Garbage-First Garbage Collector.
  • Recovering free memory by compacting live objects through parallel copying. See the sections The Parallel Collector and Garbage-First Garbage Collector

GC其实就是一个内存管理工具,通过以下操作实现自动内存管理:

  • 年轻代为对象分配内存,提升老对象老年代
  • 在老年代并发(并行)标记阶段查找存活对象。当Java堆总占用率超过默认阈值时JVM将触发标记阶段。参考CMS和G1章节(目前还未翻译,后续翻译加上连接)
  • 通过并行复制压缩存活对象来回收可用内存。参考Parallel GC和 G1章节(目前还未翻译,后续翻译加上连接)

When does the choice of a garbage collector matter? For some applications, the answer is never. That is, the application can perform well in the presence of garbage collection with pauses of modest frequency and duration. However, this is not the case for a large class of applications, particularly those with large amounts of data (multiple gigabytes), many threads, and high transaction rates.

 什么时候需要考虑选择GC?对于在当前GC下那些运行良好、停顿适中的这些应用来说永远不需要。但是对于一些大型应用程序来说,尤其是有大量应用数据(几G)、大量线程、高事物的应用程序来说就不一定了。

Amdahl's law (parallel speedup in a given problem is limited by the sequential portion of the problem) implies that most workloads cannot be perfectly parallelized; some portion is always sequential and does not benefit from parallelism. This is also true for the Java platform. In particular, virtual machines from Oracle for the Java platform prior to Java SE 1.4 do not support parallel garbage collection, so the effect of garbage collection on a multiprocessor system grows relative to an otherwise parallel application.

 Amdahl定律(给定问题的并行加速受该问题的顺序/串行部分的限制)表明大多数负载不能完全并行化执行。总有部分是顺序/串行执行的,并不能从并行执行受益。对于java平台来说也是如此。特别是在Java SE 1.4之前的Oracle Java平台中的虚拟机不支持并行GC,因此与并行应用程序相比,在多处理器系统中GC的影响更明显。

The graph in Figure 1-1, "Comparing Percentage of Time Spent in Garbage Collection" models an ideal system that is perfectly scalable with the exception of garbage collection (GC). The red line is an application spending only 1% of the time in garbage collection on a uniprocessor system. This translates to more than a 20% loss in throughput on systems with 32 processors. The magenta line shows that for an application at 10% of the time in garbage collection (not considered an outrageous amount of time in garbage collection in uniprocessor applications), more than 75% of throughput is lost when scaling up to 32 processors.

下图为一个理想系统(出了GC外其他资源可以自由扩展)的GC耗时占比对比图。红色线表示应用在单处理器系统上GC耗时1%,在32核时吞吐量丢失超20%。洋红色线显示了一个应用程序用10%的时间内进行GC(在单处理器应用程序中,GC的时间并不算多),当扩展到32个处理器时,超过75%的吞吐量丢失。(该结果图Oracle官网并没有给出任何解释,应该是测试结果)

Figure 1-1 Comparing Percentage of Time Spent in Garbage Collection

X轴为处理器数量Y轴为吞吐量


This shows that negligible speed issues when developing on small systems may become principal bottlenecks when scaling up to large systems. However, small improvements in reducing such a bottleneck can produce large gains in performance. For a sufficiently large system, it becomes worthwhile to select the right garbage collector and to tune it if necessary.

 上述表面,在小系统里面一个微不足道的速度问题,在大系统里面将会变成主要瓶颈,所以,瓶颈问题的细微改进可以在性能上产生巨大的收益。对于足够大的系统,选择正确的GC并在必要时进行调优是值得的。

The serial collector is usually adequate for most "small" applications (those requiring heaps of up to approximately 100 megabytes (MB (on modern processors). The other collectors have additional overhead or complexity, which is the price for specialized behavior. If the application does not need the specialized behavior of an alternate collector, use the serial collector. One situation where the serial collector is not expected to be the best choice is a large, heavily threaded application that runs on a machine with a large amount of memory and two or more processors. When applications are run on such server-class machines, the parallel collector is selected by default. See the section Ergonomics.

 serial GC 通常用在小型应用中(比如堆的使用在不超过100M),其他GC为了达到特定的行为 都会以额外的开销或者复杂性作为代价。如果一个应用不需要特殊的GC行为,那么请使用serial GC。在一个大内存(上G)的多处理上运行一个大型程序,serial GC是错误的选择(这一段是意译)当应用程序运行在上述机器上时,JVM会默认选择parallel GC。Ergonomics章节详解

This document was developed using Java SE 8 on the Solaris operating system (SPARC Platform Edition) as the reference. However, the concepts and recommendations presented here apply to all supported platforms, including Linux, Microsoft Windows, the Solaris operating system (x64 Platform Edition), and OS X. In addition, the command line options mentioned are available on all supported platforms, although the default values of some options may be different on each platform.

该文档是基于 Solaris operating system的JDK1.8编写的,但是这里提出的理念和建议适用于所有受支持的平台,包括linux 、window、Solaris 和OS X。另外,文档中所有的命令行 选项对于所有的平台都是支持的,但是再不同的平台可能默认值不一样。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值