1. Overview
1.概述
In this quick tutorial, we’ll explore the most well-known options which can be used to configure the Java Virtual Machine.
在这个快速教程中,我们将探讨最著名的选项,这些选项可用于配置Java虚拟机。
2. Explicit Heap Memory – Xms and Xmx Options
2.显式堆内存–Xms和Xmx选项
One of the most common performance-related practices is to initialize the heap memory as per the application requirements.
最常见的与性能有关的做法之一是根据应用要求初始化堆内存。
That’s why we should specify minimal and maximal heap size. Below parameters can be used for achieving it:
这就是为什么我们应该指定最小和最大的堆尺寸。以下参数可用于实现这一目标。
-Xms<heap size>[unit]
-Xmx<heap size>[unit]
Here, unit denotes the unit in which the memory (indicated by heap size) is to be initialized. Units can be marked as ‘g’ for GB, ‘m’ for MB and ‘k’ for KB.
这里,unit表示要初始化的内存(由heap size表示)的单位。单位可以标记为‘g’代表GB,‘m’代表MB,‘k’代表KB。
For example, if we want to assign minimum 2 GB and maximum 5 GB to JVM, we need to write:
例如,如果我们想给JVM分配最小2GB和最大5GB的空间,我们需要写。
-Xms2G -Xmx5G
Starting with Java 8, the size of Metaspace is not defined. Once it reaches the global limit, JVM automatically increases it, However, to overcome any unnecessary instability, we can set Metaspace size with:
从Java 8开始,Metaspace的大小没有定义。一旦达到全局限制,JVM就会自动增加。然而,为了克服任何不必要的不稳定性,我们可以用以下方法设置Metaspace的大小。
-XX:MaxMetaspaceSize=<metaspace size>[unit]
Here, metaspace size denotes the amount of memory we want to assign to Metaspace.
这里,metaspace size表示我们要分配给Metaspace的内存量。
As per Oracle guidelines, after total available memory, the second most influential factor is the proportion of the heap reserved for the Young Generation. By default, the minimum size of the YG is 1310 MB, and maximum size is unlimited.
根据Oracle指南,在总的可用内存之后,第二大影响因素是为年轻一代保留的堆的比例。默认情况下,YG的最小大小为1310MB,最大大小为unlimited。
We can assign them explicitly:
我们可以明确地分配它们。
-XX:NewSize=<young size>[unit]
-XX:MaxNewSize=<young size>[unit]
3. Garbage Collection
3.垃圾收集
For better stability of the application, choosing of right Garbage Collection algorithm is critical.
为了提高应用程序的稳定性,选择正确的垃圾收集算法是至关重要的。
JVM has four types of GC implementations:
JVM有四种类型的GC实现。
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
These implementations can be declared with the below parameters:
这些实现可以用以下参数声明。
-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+USeParNewGC
-XX:+UseG1GC
More details on Garbage Collection implementations can be found here.
关于垃圾收集实现的更多细节可以在这里找到。
4. GC Logging
4.GC日志
To strictly monitor the application health, we should always check the JVM’s Garbage Collection performance. The easiest way to do this is to log the GC activity in human readable format.
为了严格监控应用程序的健康状况,我们应该经常检查JVM的Garbage Collection性能。最简单的方法是以人类可读的格式记录GC活动。
Using the following parameters, we can log the GC activity:
使用以下参数,我们可以记录GC活动。
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=< number of log files >
-XX:GCLogFileSize=< file size >[ unit ]
-Xloggc:/path/to/gc.log
UseGCLogFileRotation specifies the log file rolling policy, much like log4j, s4lj, etc. NumberOfGCLogFiles denotes the max number of log files that can be written for a single application life cycle. GCLogFileSize specifies the max size of the file. Finally, loggc denotes its location.
UseGCLogFileRotation指定了日志文件滚动策略,就像log4j、s4lj等。NumberOfGCLogFiles表示在一个应用程序的生命周期中可以写入的最大日志文件数。GCLogFileSize规定了文件的最大尺寸。最后,loggc表示其位置。
Point to note here is that, there are two more JVM parameters available (-XX:+PrintGCTimeStamps and -XX:+PrintGCDateStamps) which can be used to print date-wise timestamp in the GC log.
这里需要注意的是,还有两个JVM参数可用(-XX:+PrintGCTimeStamps和-XX:+PrintGCDateStamps),可用于在GC日志中打印按日期划分的时间戳。
For example, if we want to assign a maximum of 100 GC log files, each having a maximum size of 50 MB and want to store them in ‘/home/user/log/’ location, we can use below syntax:
例如,如果我们想分配最多100个GC日志文件,每个文件最大尺寸为50MB,并想将它们存储在’/home/user/log/’ 位置,我们可以使用以下语法。
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=10
-XX:GCLogFileSize=50M
-Xloggc:/home/user/log/gc.log
However, the problem is that one additional daemon thread is always used for monitoring system time in the background. This behavior may create some performance bottleneck; that’s why it’s always better not to play with this parameter in production.
然而,问题是,一个额外的守护进程总是被用来在后台监控系统时间。这种行为可能会造成一些性能瓶颈;这就是为什么最好不要在生产中使用这个参数。
5. Handling out of Memory
5.处理内存不足的问题
It’s very common for a large application to face out of memory error which, in turn, results in the application crash. It’s a very critical scenario and very hard to replicate to troubleshoot the issue.
大型应用程序面临内存不足错误的情况非常普遍,这反过来又会导致应用程序崩溃。这是一个非常关键的情况,而且非常难以复制以排除问题。
That’s why JVM comes with some parameters which dump heap memory into a physical file which can be used later for finding out leaks:
这就是为什么JVM带有一些参数,可以将堆内存转储到一个物理文件中,以后可以用来查找泄漏。
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./java_pid<pid>.hprof
-XX:OnOutOfMemoryError="< cmd args >;< cmd args >"
-XX:+UseGCOverheadLimit
A couple of points to note here:
这里有几点需要注意。
- HeapDumpOnOutOfMemoryError instructs the JVM to dump heap into physical file in case of OutOfMemoryError
- HeapDumpPath denotes the path where the file is to be written; any filename can be given; however, if JVM finds a <pid> tag in the name, the process id of the current process causing the out of memory error will be appended to the file name with .hprof format
- OnOutOfMemoryError is used to issue emergency commands to be executed in case of out of memory error; proper command should be used in the space of cmd args. For example, if we want to restart the server as soon as out of memory occur, we can set the parameter:
-XX:OnOutOfMemoryError="shutdown -r"
- UseGCOverheadLimit is a policy that limits the proportion of the VM’s time that is spent in GC before an OutOfMemory error is thrown
6. 32/64 Bit
6. 32/64位
In the OS environment where both 32 and 64-bit packages are installed, the JVM automatically chooses 32-bit environmental packages.
在同时安装了32位和64位软件包的操作系统环境中,JVM会自动选择32位环境软件包。
If we want to set the environment to 64 bit manually, we can do so using below parameter:
如果我们想手动设置环境为64位,我们可以用下面的参数来做。
-d<OS bit>
OS bit can be either 32 or 64. More information about this can be found here.
操作系统位可以是32或64。有关这方面的更多信息可以在这里找到。
7. Misc
7.杂项
- -server: enables “Server Hotspot VM”; this parameter is used by default in 64 bit JVM
- -XX:+UseStringDeduplication: Java 8u20 has introduced this JVM parameter for reducing the unnecessary use of memory by creating too many instances of the same String; this optimizes the heap memory by reducing duplicate String values to a single global char[] array
- -XX:+UseLWPSynchronization: sets LWP (Light Weight Process) – based synchronization policy instead of thread-based synchronization
- -XX:LargePageSizeInBytes: sets the large page size used for the Java heap; it takes the argument in GB/MB/KB; with larger page sizes we can make better use of virtual memory hardware resources; however, this may cause larger space sizes for the PermGen, which in turn can force to reduce the size of Java heap space
- -XX:MaxHeapFreeRatio: sets the maximum percentage of heap free after GC to avoid shrinking.
- -XX:MinHeapFreeRatio: sets the minimum percentage of heap free after GC to avoid expansion; to monitor the heap usage you can use VisualVM shipped with JDK.
- -XX:SurvivorRatio: Ratio of eden/survivor space size – for example, -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden space to be 1:6,
- -XX:+UseLargePages: use large page memory if it is supported by the system; please note that OpenJDK 7 tends to crash if using this JVM parameter
- -XX:+UseStringCache: enables caching of commonly allocated strings available in the String pool
- -XX:+UseCompressedStrings: use a byte[] type for String objects which can be represented in pure ASCII format
- -XX:+OptimizeStringConcat: it optimizes String concatenation operations where possible
8. Conclusion
8.结论
In this quick article, we learned about some important JVM parameters – which can be used to tune and improve general application performance.
在这篇快速文章中,我们了解了一些重要的JVM参数–它们可以用来调整和提高一般的应用程序性能。
Some of these can also be used for debugging purposes.
其中一些也可用于调试目的。
If you want to explore the reference parameters in more detail, you can get started here.
如果你想更详细地探索参考参数,你可以从这里开始。