JVM参数分为标准参数和非标准参数:
"-"开头的参数,如-client, -server等"-X"和"-XX"开头的参数,如-Xmx, -XX:+DisableExplicitGC或者简单分为三类:
"-"开头的参数"-X"开头的参数"-XX"开头的参数"-"开头的参数)选择client模式的VM。客户端常使用
选择server模式的VM。服务端常使用
Loads native agent library libname, for example:
-agentlib:hprof
-agentlib:jdwp=help
-agentlib:hprof=help
See JVMTI Agent Command-Line Options at
http://docs.oracle.com/javase/7/docs/platform/jvmti/jvmti.html#starting
Loads a native agent library by full pathname.
指定查找class的路径。
可以是文件夹(directories)、jar文件(jar files)或者zip文档(zip archives)。
Linux平台用冒号分隔
如:
java -classpath lib/*:test.jar:test2.jar Main.class
同-classpath,是其简写模式
设置系统参数。在代码中可以通过System.getProperty(property)获取
如:
java -Denv=test Main.class
程序中可以通过System.getProperty("env")获得其值为"test"
在32位环境中运行程序。如果不支持32位环境,则报错。
如果使用的不是64位系统,则-d32是默认添加的
在64位环境中运行程序。如果不支持32位环境,则报错。
注:存在-server参数时,-d64是默认添加的
执行可执行jar包。
第一个参数是jar包名字,而非Main Class名字。
要使这个参数生效,要有一个前提,即manifest文件中需要有下面一行:
Main-Class: classname
一个manifest文件示例:
Manifest-Version: 1.0
Implementation-Title: spring-boot-web-base
Implementation-Version: 1.0-SNAPSHOT
Built-By: michealyang
Implementation-Vendor-Id: com.michealyang
Spring-Boot-Version: 2.0.1.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.michealyang.webbase.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_162-ea
Implementation-URL: http://maven.apache.org
Loads a Java programming language agent. For more information about instrumenting Java applications, see the java.lang.instrument package description in the Java API documentation at http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html
打印虚拟机运行信息
可以在程序启动时,做为参数传入。也可做为命令行使用
做为命令行使用的语法是:
java -verbose:[class|gc|jni] {pid}
verbose的冒号后面支持三个参数,即class, gc, jni
打印加载的class信息
如:
java -verbose:class 12093
数据结果大致为:
[Opened /usr/local/jdk1.8.0_45/jre/lib/rt.jar]
[Loaded java.lang.Object from /usr/local/jdk1.8.0_45/jre/lib/rt.jar]
[Loaded java.io.Serializable from /usr/local/jdk1.8.0_45/jre/lib/rt.jar]
[Loaded java.lang.Comparable from /usr/local/jdk1.8.0_45/jre/lib/rt.jar]
[Loaded java.lang.CharSequence from /usr/local/jdk1.8.0_45/jre/lib/rt.jar]
[Loaded java.lang.String from /usr/local/jdk1.8.0_45/jre/lib/rt.jar]
在启动参数中加上-verbose:gc当发生gc时,可以打印出gc相关的信息;该信息不够高全面,等同于-XX:+PrintGC。其实只要设置-XX:+PrintGCDetails就会自动带上-verbose:gc和-XX:+PrintGC
打印native方法或者其他native interface的调用信息
打印java版本
java -version
结果为:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
"-X"开头的参数"-X"开头的参数是非标准参数,也就是只能被部分VM识别,而不能被全部VM识别的参数。
官网解释:
Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK
关于"-X"的常见参数,见下面的按功能分类
"-XX"开头的参数"-XX"开头的参数是非稳定参数,随时可能被修改或者移除。
官网解释:
Options that are specified with -XX are not stable and are subject to change without notice.
-XX参数的常见语法有:
-XX:+DisableExplicitGC,开启禁止显式GC-XX:-DisableExplicitGC,关闭禁止显示GC-XX:SurvivorRatio=80,设置eden/survivor的比值关于"-XX"的常见参数,见下面的按功能分类
标记了※的表示常用参数
设置堆内存最大值
-Xmx1g
或者
-Xmx1024m
设置堆内存最小值。一般与-Xmx设置一样大
-Xmx1g
设置新生代大小
-Xmn256m
设置栈空间大小
-Xss128k
新生代Eden和Survivor划分比例
-XX:SurvivorRatio=8
设置永久代初始大小。JDK8中已移除
-XX:PermSize=128m
设置永久代最大值。JDK8中已移除
-XX:MaxPermSize=128m
设置meta区大小。JDK8增加
-XX:MetaspaceSize=128m
设置永久代最大值。JDK8中已移除
-XX:MaxMetaspaceSize=128m
用于设置Code Cache大小,JIT编译的代码都放在Code Cache中,若Code Cache空间不足则JIT无法继续编译,并且会去优化,比如编译执行改为解释执行,由此,性能会降低
-XX:ReservedCodeCacheSize=128m
Behavioral Options,即影响VM基本行为的参数
使用串行垃圾回收器回收新生代
使用并行垃圾回收器回收新生代
当使用-XX:+UseParNewGC时,该参数设定GC的线程数,默认与CPU核数相同
使用Parallel Scavenge垃圾回收器
-XX:UseParallelGC = “Parallel Scavenge” + “Serial Old”
用并行垃圾回收器进行full gc
-XX:UseParallelOldGC = “Parallel Scavenge” + “Parallel Old”
使用CMS做为垃圾回收器
注:当前常见的垃圾回收器组合是下面这种:
-XX:+UseConcMarkSweepGC -XX:+UseParNewGC
设置gc日志位置
-Xloggc:/opt/logs/mobile/admin.gc.log
打印GC详情
输出形式:
[GC 118250K->113543K(130112K), 0.0094143 secs]
[Full GC 121376K->10414K(130112K), 0.0650971 secs]
打印GC更详细的信息
输出形式:
[GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs]
[GC [DefNew: 8614K->8614K(9088K), 0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs]
Garbage First Garbage Collection Options
使用G1做为垃圾回收器
Sets a target for the maximum GC pause time. This is a soft goal, and the JVM will make its best effort to achieve it.
Percentage of the (entire) heap occupancy to start a concurrent GC cycle. It is used by GCs that trigger a concurrent GC cycle based on the occupancy of the entire heap, not just one of the generations (e.g., G1). A value of 0 denotes ‘do constant GC cycles‘. The default value is 45.
old区/new区的比例,默认为2
eden/survivor的比值,默认为8.
Maximum value for tenuring threshold. The default value is 15.
Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running.
Number of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM is running.
Sets the amount of heap that is reserved as a false ceiling to reduce the possibility of promotion failure. The default value is 10.
With G1 the Java heap is subdivided into uniformly sized regions. This sets the size of the individual sub-divisions. The default value of this parameter is determined ergonomically based upon heap size. The minimum value is 1Mb and the maximum value is 32Mb.
使用CMS做为垃圾回收器
Performance Options
Turn on point performance compiler optimizations that are expected to be default in upcoming releases. (Introduced in 5.0 update 6.)
Use large page memory. (Introduced in 5.0 update 5.) For details, see Java Support for Large Memory Pages.
Optimize String concatenation operations where possible. (Introduced in Java 6 Update 20)
Debugging Options
显示JVM所有可设置的参数及它们的值
显示所有可设置的参数及它们的值
可以设置的参数默认是不包括diagnostic或experimental系的。要在-XX:+PrintFlagsFinal的输出里看到这两种参数的信息,分别需要显式指定-XX:+UnlockDiagnosticVMOptions和-XX:+UnlockExperimentalVMOptions。
语法:
java -PrintFlagsFinal -version {id}
添加
-version是防止被理解成启动java程序
输出示例:

生成error 文件的路径
-XX:ErrorFile=/opt/logs/mobile/admin.error
当jvm crash的时候在linux里会启动gdb 去分析和调式,适合在测试环境中使用
OOM的时候dump出内存。
dump文件位置
Q: -Xss和-XX:ThreadStackSize有啥区别?
A: 本质上是没有区别的。-Xss是在HotSpot出现之前就存在的,而-XX:ThreadStackSize是HotSpot的内置参数。也就是说-Xss适用于很多VM,包括HotSpot,而-XX:ThreadStackSize只适用于HotSpot。
另外,使用jinfo的时候,应该使用ThreadStackSize:
jinfo -float ThreadStackSize [pid]
作者:齐晋
链接:https://www.jianshu.com/p/1c6b5c2e95f9
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
原文:https://www.cnblogs.com/xuningchuanblogs/p/11441554.html