ZGC | Using -XX:SoftMaxHeapSize

栏目: IT技术 · 发布时间: 5年前

内容简介:InAs the name implies, this new option sets a

ZGC | Using -XX:SoftMaxHeapSize

In JDK 13 we introduced a new JVM option called -XX:SoftMaxHeapSize=<size> . ZGC is so far the only garbage collector in HotSpot that supports this options, but work to also support it in G1 is ongoing . Since this option is relatively new, and perhaps not yet widely known, I thought I’d write a few words about when and how to use it.

As the name implies, this new option sets a soft limit on how large the Java heap can grow. When set, the GC will strive to not grow heap beyond the soft max heap size. But, the GC is still allowed to grow the heap beyond the specified size if it really needs to, like when the only other alternatives left is to stall an allocation or throw an OutOfMemoryError .

There are different use cases where setting a soft max heap size can be useful. For example:

OutOfMemoryError

Let’s make up an example, to illustrate the first use case listed above. Pretend that your workload under normal conditions needs 2G of heap to run well. But once in a while you see workload spikes (maybe you’re providing a service that attracts a lot more users a certain day of the week/month, or something similar). With this increase in workload your application now needs, say, 5G to run well. To deal with this situation you would normally set the max heap size ( -Xmx ) to 5G to cover for the occasional workload spikes. However, that also means you will be wasting 3G of memory 98% (or something) of the time when it’s not needed, since those unused 3G will still be tied up in the Java heap. This is where a soft max heap size can come in handy, which together with ZGC’s ability to uncommit unused memory allows you to have your cake and eat it too. Set the max heap size to the max amount of heap your application will ever need ( -Xmx5G in this case), and set the soft max heap size to what your application needs under normal conditions ( -XX:SoftMaxHeapSize=2G in this case). You’re now covered to handle those workload spikes nicely, without always wasting 3G. Once the workload spike has passed and the need for memory drops down to normal again, ZGC will shrink the heap and continue to do it’s best to honor the -XX:SoftMaxHeapSize setting. When those extra 3G of heap have been sitting unused for a while, ZGC will uncommit that memory, returning it to the operating system for other processes (or the OS page cache, or something else) to use.

How aggressively ZGC uncommits unused memory can be controlled with -XX:ZUncommitDelay=<seconds> , which defaults to 300 (5 minutes). In other words, by default, memory must have been unused for 5 minutes before it’s eligible for uncommit. Note that committing and uncommitting memory are relatively expensive operations. A too aggressive (short) uncommit delay will cause uncommitted memory to very soon be committed again, which can be a waste of CPU cycles and might in the end also impact the overall performance of your application.

The SoftMaxHeapSize option is also manageable , meaning it can be changed at runtime without having to restart the JVM. You can change a manageable JVM option at runtime using the HotSpotDiagnosticMXBean or jcmd , like this.

jcmd <pid> VM.set_flag SoftMaxHeapSize <size>

The soft max heap size can not be set to a value greater than the max heap size. When not set on the command-line, the soft max heap size will default to the same value as the max heap size.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法

数据结构与算法

许卓群、杨冬青、唐世渭、张铭 / 高等教育出版社 / 2004-1 / 29.50元

《数据结构与算法》把数据结构的原理和算法分析技术有机地结合在一起,系统地介绍了各种类型的数据结构和排序、检索的各种算法,还引入了一些比较高级的数据结构及相关的算法分析技术。.《数据结构与算法》分为基本数据结构、排序和检索、高级数据结构三部分。借助抽象数据类型,从逻辑结构的角度系统地介绍了线性表、字符串、二叉树、树和图等各种基本数据结构;从算法的角度讨论排序、检索和索引算法;从应用的角度介绍了一些复......一起来看看 《数据结构与算法》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具