JDK8中java命令相关选项JVM参数、GC参数

地址链接https://docs.oracle.com/javase/8/docs/technotes/tools/unix/index.html

java命令的参数

Standard Options

这些是JVM的所有实现所支持的最常用选项。

主要参数
-jar filename Executes a program encapsulated in a JAR file
-javaagent:jarpath[=options] Loads the specified Java programming language agent.
-verbose:gc Displays information about each garbage collection (GC) event.

Non-Standard Options

常用比较重要参数
-Xnoclassgc

关闭CLASS类的垃圾回收功能,就是虚拟机加载的类,即便是不使用,没有实例也不会回收。

注意:这里说的是class类,而不是堆内存中new出来的类的实例。

当您在启动时指定-Xnoclassgc时,应用程序中的类对象在GC期间将保持不变,并且始终被认为是活动的。 这可能导致更多的内存被永久占用,如果不谨慎使用,将抛出内存不足异常。

-Xms set initial Java heap size
-Xmx

set maximum Java heap size.

-Xmx 相当于 -XX:MaxHeapSize.

-Xss set java thread stack size
-Xmn

The initial size of the heap for the young generation.

 -Xmn  相当于 -XX:NewSize

 -Xloggc:<file>

log GC status to a file with time stamps

应将详细的GC事件信息输出到日志记录的文件。写入此文件的信息与-verbose:gc的输出类似,从第一个GC事件以来经过的每个事件信息。 如果两者都使用相同的Java命令给出,则-Xloggc选项将覆盖-verbose:gc。

如:-Xloggc:/test/gc.log

通过设置-XX:+PrintGCDetails、-Xloggc:/test/gc.log参数后,发生了GC之后,看到的GC信息如下图所示

Advanced Serviceability Options

这些选项提供了收集系统信息和执行广泛调试的能力。

部分参数

-XX:+HeapDumpOnOutOfMemoryError

Enables the dumping of the Java heap to a file in the current directory by using the heap profiler (HPROF) when a java.lang.OutOfMemoryError exception is thrown. You can explicitly set the heap dump file path and name using the -XX:HeapDumpPath option. By default, this option is disabled and the heap is not dumped when an OutOfMemoryError exception is thrown.

-XX:HeapDumpPath=path

Sets the path and file name for writing the heap dump provided by the heap profiler (HPROF) when the -XX:+HeapDumpOnOutOfMemoryError option is set. By default, the file is created in the current working directory, and it is named java_pidpid.hprof where pid is the identifier of the process that caused the error. The following example shows how to set the default file explicitly (%p represents the current process identificator):

-XX:HeapDumpPath=./java_pid%p.hprof

The following example shows how to set the heap dump file to /var/log/java/java_heapdump.hprof:

-XX:HeapDumpPath=/var/log/java/java_heapdump.hprof

-XX:LogFile=path

Sets the path and file name where log data is written. By default, the file is created in the current working directory, and it is named hotspot.log.

The following example shows how to set the log file to /var/log/java/hotspot.log:

-XX:LogFile=/var/log/java/hotspot.log

Advanced Garbage Collection Options

这些选项控制Java HotSpot VM如何执行垃圾回收(GC)。

部分参数

-XX:ConcGCThreads=threads

设置用于并发GC的线程个数,默认值依赖于JVM可使用的CPU的个数。

For example, to set the number of threads for concurrent GC to 2, specify the following option:

-XX:ConcGCThreads=2

-XX:+DisableExplicitGC

Enables the option that disables processing of calls to System.gc(). This option is disabled by default, meaning that calls to System.gc() are processed. If processing of calls to System.gc() is disabled, the JVM still performs GC when necessary.

关闭通过System.gc()触发的垃圾回收,对于java程序员基本用不到调用System.gc()来主动触发垃圾回收,因此建议关闭掉,否则有C++开发习惯的程序员会因着习惯而调用,造成问题。

-XX:+PrintGC Enables printing of messages at every GC. By default, this option is disabled.
-XX:+PrintGCDetails

Enables printing of detailed messages at every GC. By default, this option is disabled.

在发生垃圾收集行为时打印内存回收日志,并且在进程退出的时候输出当前的内存各区域分配情况。 

-XX:+PrintGCTimeStamps Enables printing of time stamps at every GC. By default, this option is disabled.
-XX:+UseSerialGC
-XX:+UseParNewGC

Enables the use of parallel threads for collection in the young generation. 

当使用ParNewGC时必须显式使用-XX:+UseConcMarkSweepGC来作为老年代垃圾收集器

-XX:+UseConcMarkSweepGC -XX:-UseParNewGC

-XX:+UseConcMarkSweepGC

当吞吐量垃圾收集器(-XX:+ UseParallelGC)无法满足应用程序延迟要求时,Oracle建议您使用CMS垃圾收集器。

使用G1垃圾收集器(-XX:+UseG1GC)也是一个选择

-XX:+UseParallelGC If it is enabled, then the -XX:+UseParallelOldGC option is automatically enabled, unless you explicitly disable it.
-XX:+UseParallelOldGC  
-XX:+UseG1GC  The G1 collector is recommended for applications requiring large heaps (sizes of around 6 GB or larger) with limited GC latency requirements (stable and predictable pause time below 0.5 seconds)
-XX:MaxTenuringThreshold=threshold

设置新生代需要经历多少次GC晋升到老年代中的最大阈值。

最大值为15。 parallel收集器的默认值为15,而CMS收集器的默认值为6。

-XX:+UseTLAB Enables the use of thread-local allocation blocks (TLABs) in the young generation space. This option is enabled by default. To disable the use of TLABs, specify -XX:-UseTLAB.
-XX:SurvivorRatio=ratio

设置Eden区和Survivor(两个Survivor区的合计)区的比例,默认值是8

-XX:+UseAdaptiveSizePolicy

自适应各代大小比例,这个参数默认是启动的。通过 -XX:-UseAdaptiveSizePolicy参数来禁止,并且要通过-XX:SurvivorRatio显式设置内存分配。

-XX:InitialSurvivorRatio=ratio

通过-XX:+UseParallelGC -XX:+UseParallelOldGC这两个参数使用PS+PO的垃圾收集器时,默认情况下-XX:-UseAdaptiveSizePolicy这个参数是启用的,survivor区随着应用的运行从初始值不断的调整着空间的大小。

这个参数必须与-XX:+UseAdaptiveSizePolicy配合使用,是使用自适应各区大小比例的初始值。

-XX:PretenureSizeThreshold=threshold 大于这个值的参数直接在老年代分配
-XX:+PrintFlagsInitial 表示打印出所有XX选项的默认值
-XX:+PrintFlagsFinal

表示打印出XX选项在运行程序时生效的值,如java -XX:+PrintFlagsFinal -version

我们可以结合linux的grep命令进行查找,如

java -XX:+PrintFlagsFinal -version | grep PretenureSizeThreshold

查询PretenureSizeThreshold参数

-XX:+PrintCommandLineFlags 这个参数让JVM打印出那些已经被用户或者JVM设置过的详细的XX参数的名称和值。

GC日志理解: 

[GC [Full GC表示这次垃圾收集的停顿类型,而不是用来分新生代GC还是老年代GC的。

[PSYoungGen [ParOldGen [PSPermGen 表示GC发生的区域,因为这里我使用的垃圾收集器是Parallel Scavenge + ParallelOld,因此新生代是PSYoungGen、老年代是ParOldGen,永久代是PSPermGen。

接下来的方括号

[GC停顿类型 [GC发生区域:GC前该区域已使用容量->GC后该区域已使用容量(该内存区域总量)] GC前Java堆已使用容量->GC后Java堆已使用容量(Java堆总量) GC所占用时间]

[Full GC [PSYoungGen: 53399K->0K(1223680K)] [ParOldGen: 72K->51911K(2796544K)] 53471K->51911K(4020224K) [PSPermGen: 24193K->23999K(1048576K)], 0.2055930 secs]

[Times: user=0.14 sys=0.03, real=0.11 secs]

user:用户态消耗的CPU时间、sys:内核态消耗的CPU时间、real:操作从开始到结束所经过的墙钟时间(WallClickTime)

目前JDK中有的GC及他们之间的搭配

参考内存分配与回收策略 

发布了243 篇原创文章 · 获赞 138 · 访问量 138万+

猜你喜欢

转载自blog.csdn.net/ystyaoshengting/article/details/104233176