JVM 参数及各部分含义(转)

转自:https://www.jianshu.com/p/1c6b5c2e95f9

JVM参数分类

JVM参数分为标准参数和非标准参数:

  • 标准参数: "-"开头的参数,如-client, -server
  • 非标准参数: "-X""-XX"开头的参数,如-Xmx, -XX:+DisableExplicitGC

或者简单分为三类:

  • "-"开头的参数
  • "-X"开头的参数
  • "-XX"开头的参数

标准参数("-"开头的参数)

-client

选择client模式的VM。客户端常使用

-server ※

选择server模式的VM。服务端常使用

-agentlib:libname[=options]

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

-agentpath:pathname[=options]

Loads a native agent library by full pathname.

-classpath classpath

指定查找class的路径。
可以是文件夹(directories)、jar文件(jar files)或者zip文档(zip archives)。
Linux平台用冒号分隔

如:

java -classpath lib/*:test.jar:test2.jar Main.class

-cp classpath

-classpath,是其简写模式

-Dproperty=value

设置系统参数。在代码中可以通过System.getProperty(property)获取
如:

java -Denv=test Main.class

程序中可以通过System.getProperty("env")获得其值为"test"

-d32

在32位环境中运行程序。如果不支持32位环境,则报错。
如果使用的不是64位系统,则-d32是默认添加的

-d64

在64位环境中运行程序。如果不支持32位环境,则报错。

注:存在-server参数时,-d64是默认添加的

-jar

执行可执行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
-javaagent:jarpath[=options]

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

-verbose

打印虚拟机运行信息

可以在程序启动时,做为参数传入。也可做为命令行使用

做为命令行使用的语法是:

java -verbose:[class|gc|jni] {pid}

verbose的冒号后面支持三个参数,即class, gc, jni

-verbose:class

打印加载的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

在启动参数中加上-verbose:gc当发生gc时,可以打印出gc相关的信息;该信息不够高全面,等同于-XX:+PrintGC。其实只要设置-XX:+PrintGCDetails就会自动带上-verbose:gc-XX:+PrintGC

-verbose:jni

打印native方法或者其他native interface的调用信息

-version

打印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:+[PARAM], 开启该参数的功能,如-XX:+DisableExplicitGC,开启禁止显式GC
  • -XX:-[PARAM], 关闭该参数的功能,如-XX:-DisableExplicitGC,关闭禁止显示GC
  • -XX:PARAM=VALUE, 设置参数的值,如-XX:SurvivorRatio=80,设置eden/survivor的比值

关于"-XX"的常见参数,见下面的按功能分类


按功能分类

标记了的表示常用参数


内存参数

-Xmx[value] ※

设置堆内存最大值

-Xmx1g
或者
-Xmx1024m

-Xms[value] ※

设置堆内存最小值。一般与-Xmx设置一样大

-Xmx1g

-Xmn[value] ※

设置新生代大小

-Xmn256m

-Xss[value] ※

设置栈空间大小

-Xss128k

-XX:SurvivorRatio=[value] ※

新生代Eden和Survivor划分比例

-XX:SurvivorRatio=8

-XX:PermSize=[value] ※

设置永久代初始大小。JDK8中已移除

-XX:PermSize=128m

-XX:MaxPermSize=[value] ※

设置永久代最大值。JDK8中已移除

-XX:MaxPermSize=128m

-XX:MetaspaceSize=[value] ※

设置meta区大小。JDK8增加

-XX:MetaspaceSize=128m

-XX:MaxMetaspaceSize=[value] ※

设置永久代最大值。JDK8中已移除

-XX:MaxMetaspaceSize=128m

-XX:ReservedCodeCacheSize

用于设置Code Cache大小,JIT编译的代码都放在Code Cache中,若Code Cache空间不足则JIT无法继续编译,并且会去优化,比如编译执行改为解释执行,由此,性能会降低

-XX:ReservedCodeCacheSize=128m


行为相关参数

Behavioral Options,即影响VM基本行为的参数

-XX:-DisableExplicitGC ※

GC参数

-XX:-UseSerialGC

使用串行垃圾回收器回收新生代

-XX:+UseParNewGC ※

使用并行垃圾回收器回收新生代

-XX:ParallerGCThreads

当使用-XX:+UseParNewGC时,该参数设定GC的线程数,默认与CPU核数相同

-XX:-UseParallelGC

使用Parallel Scavenge垃圾回收器

-XX:UseParallelGC = “Parallel Scavenge” + “Serial Old”

-XX:-UseParallelOldGC

用并行垃圾回收器进行full gc

-XX:UseParallelOldGC = “Parallel Scavenge” + “Parallel Old”

-XX:-UseConcMarkSweepGC ※

使用CMS做为垃圾回收器

注:当前常见的垃圾回收器组合是下面这种:

-XX:+UseConcMarkSweepGC -XX:+UseParNewGC

-Xloggc:[path] ※

设置gc日志位置

-Xloggc:/opt/logs/mobile/admin.gc.log

-XX:+PrintGC ※

打印GC详情

输出形式:

[GC 118250K->113543K(130112K), 0.0094143 secs]
[Full GC 121376K->10414K(130112K), 0.0650971 secs]

-XX:+PrintGCDetails ※

打印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]


G1参数

Garbage First Garbage Collection Options

-XX:+UseG1GC

使用G1做为垃圾回收器

-XX:MaxGCPauseMillis=n

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.

-XX:InitiatingHeapOccupancyPercent=n

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.

-XX:NewRatio=n

old区/new区的比例,默认为2

-XX:SurvivorRatio=n

eden/survivor的比值,默认为8.

-XX:MaxTenuringThreshold=n

Maximum value for tenuring threshold. The default value is 15.

-XX:ParallelGCThreads=n

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.

-XX:ConcGCThreads=n

Number of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM is running.

-XX:G1ReservePercent=n

Sets the amount of heap that is reserved as a false ceiling to reduce the possibility of promotion failure. The default value is 10.

-XX:G1HeapRegionSize=n

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参数

-XX:-UseConcMarkSweepGC ※

使用CMS做为垃圾回收器

性能相关参数

Performance Options

-XX:+AggressiveOpts

Turn on point performance compiler optimizations that are expected to be default in upcoming releases. (Introduced in 5.0 update 6.)

-XX:+UseLargePages

Use large page memory. (Introduced in 5.0 update 5.) For details, see Java Support for Large Memory Pages.

-XX:+OptimizeStringConcat

Optimize String concatenation operations where possible. (Introduced in Java 6 Update 20)


debug相关参数

Debugging Options

-XX:+PrintFlagsInitial

显示JVM所有可设置的参数及它们的值

-XX:+PrintFlagsFinal

显示所有可设置的参数及它们的值

可以设置的参数默认是不包括diagnostic或experimental系的。要在-XX:+PrintFlagsFinal的输出里看到这两种参数的信息,分别需要显式指定-XX:+UnlockDiagnosticVMOptions-XX:+UnlockExperimentalVMOptions

语法:

java -PrintFlagsFinal -version {id}

添加-version是防止被理解成启动java程序

输出示例:


 
image.png
-XX:ErrorFile=[path] ※

生成error 文件的路径

-XX:ErrorFile=/opt/logs/mobile/admin.error

-XX:+ShowMessageBoxOnError

当jvm crash的时候在linux里会启动gdb 去分析和调式,适合在测试环境中使用

-XX:+HeapDumpOnOutOfMemoryError ※

OOM的时候dump出内存。

-XX:HeapDumpPath

dump文件位置

Q & A

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值