Performance optimization of operating parameters -jvm

2, jvm operating parameters

There are many parameters can be set, which would allow jvm can run efficiently in a variety of environments in the jvm. Most of the parameters remain default.

2.1 three parameter types

jvm parameter types are divided into three categories, namely:

  • Standard parameters
    • -help
    • -version
  • -X parameter (non-standard parameters)
    • -Xint
    • -Xcomp
  • -XX parameters (higher utilization)
    • -XX:newSize
    • -XX:+UseSerialGC

2.2, the standard parameters

jvm standard parameters, are generally very stable and will not change in future versions of the JVM, you can use java -help
retrieve all the standard parameters.

[root@node01 ~]# java ‐help
用法: java [‐options] class [args...]
(执行类)
或	java [‐options] ‐jar jarfile [args...] (执行 jar 文件)
其中选项包括:
‐d32	使用 32 位数据模型 (如果可用)
‐d64	使用 64 位数据模型 (如果可用)
‐server	选择 "server" VM
默认 VM 是 server,
因为您是在服务器类计算机上运行。



‐cp <目录和 zip/jar 文件的类搜索路径>
‐classpath <目录和 zip/jar 文件的类搜索路径>
用 : 分隔的目录, JAR 档案
和 ZIP 档案列表, 用于搜索类文件。

‐D<名称>=<值>


设置系统属性

‐verbose:[class|gc|jni]
启用详细输出
‐version	输出产品版本并退出
‐version:<值>
警告: 此功能已过时, 将在未来发行版中删除。
需要指定的版本才能运行
‐showversion	输出产品版本并继续
‐jre‐restrict‐search | ‐no‐jre‐restrict‐search
警告: 此功能已过时, 将在未来发行版中删除。
在版本搜索中包括/排除用户专用 JRE
‐? ‐help	输出此帮助消息
‐X	输出非标准选项的帮助
‐ea[:<packagename>...|:<classname>]
‐enableassertions[:<packagename>...|:<classname>]
按指定的粒度启用断言
‐da[:<packagename>...|:<classname>]
‐disableassertions[:<packagename>...|:<classname>]
禁用具有指定粒度的断言

‐esa | ‐enablesystemassertions
启用系统断言
‐dsa | ‐disablesystemassertions
禁用系统断言
‐agentlib:<libname>[=<选项>]
加载本机代理库 <libname>, 例如 ‐agentlib:hprof
另请参阅 ‐agentlib:jdwp=help 和 ‐agentlib:hprof=help
‐agentpath:<pathname>[=<选项>]
按完整路径名加载本机代理库
‐javaagent:<jarpath>[=<选项>]
加载 Java 编程语言代理, 请参阅 java.lang.instrument
‐splash:<imagepath>
使用指定的图像显示启动屏幕

2.2.1, combat

Real 1: View jvm version

[root@node01 ~]# java ‐version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141‐b15)
Java HotSpot(TM) 64‐Bit Server VM (build 25.141‐b15, mixed mode)

# ‐showversion参数是表示,先打印版本信息,再执行后面的命令,在调试时非常有用,
后面会使用到

Actual 2: -D attribute parameter setting system

Here Insert Picture Description
Compile, test:
Here Insert Picture Description

2.2.2, -server and -client parameters

Jvm operating parameters may be provided by -server or -client.

  • The difference between them is the initial heap Server VM will be larger, the default is to use parallel garbage collector, a slow start to run fast.
  • Client VM will be relatively conservative, the initial heap space will be smaller, use the serial garbage collector, its goal is to
  • JVM startup faster, but the speed will be slower than some Serverm mode.
    JVM uses the Server or Client JVM at startup type of hardware and operating system automatically selection.
  • 32-bit operating system
    • If it is a Windows system, regardless of hardware configuration, use the default JVM Client type.
    • If you are on other operating systems, the machine configured with more than 2GB of memory at the same time there are two or more CPU server mode, then use the default, or use client mode.
  • 64-bit operating system
    • Only server type, client type is not supported.

test:
Here Insert Picture Description

2.3, -X parameters

-X parameters jvm non-standard parameters, different versions of jvm, the parameters may vary by java - see non-standard parameters X

[root@node01 test]# java ‐X
‐Xmixed	混合模式执行 (默认)
‐Xint	仅解释模式执行
‐Xbootclasspath:<用 : 分隔的目录和 zip/jar 文件>
设置搜索路径以引导类和资源
‐Xbootclasspath/a:<用 : 分隔的目录和 zip/jar 文件>
附加在引导类路径末尾
‐Xbootclasspath/p:<用 : 分隔的目录和 zip/jar 文件>
置于引导类路径之前
‐Xdiag	显示附加诊断消息
‐Xnoclassgc	禁用类垃圾收集
‐Xincgc	启用增量垃圾收集
‐Xloggc:<file>	将 GC 状态记录在文件中 (带时间戳)
‐Xbatch	禁用后台编译
‐Xms<size>	设置初始 Java 堆大小
‐Xmx<size>	设置最大 Java 堆大小
‐Xss<size>	设置 Java 线程堆栈大小
‐Xprof	输出 cpu 配置文件数据
‐Xfuture	启用最严格的检查, 预期将来的默认值
‐Xrs	减少 Java/VM 对操作系统信号的使用 (请参阅文档)
‐Xcheck:jni	对 JNI 函数执行其他检查
‐Xshare:off	不尝试使用共享类数据
‐Xshare:auto	在可能的情况下使用共享类数据 (默认)
‐Xshare:on	要求使用共享类数据, 否则将失败。
‐XshowSettings	显示所有设置并继续
‐XshowSettings:all
显示所有设置并继续
‐XshowSettings:vm 显示所有与 vm 相关的设置并继续
‐XshowSettings:properties
显示所有属性设置并继续
‐XshowSettings:locale
显示所有与区域设置相关的设置并继续

‐X 选项是非标准选项, 如有更改, 恕不另行通知。

2.3.1、-Xint、-Xcomp、-Xmixed

  • 在解释模式(interpreted mode)下,-Xint标记会强制JVM执行所有的字节码,当然这会降低运行速度,通常低10倍或更多。
  • -Xcomp参数与它(-Xint)正好相反,JVM在第一次使用时会把所有的字节码编译成本地代码,从而带来最大程度的优化。
    • 然而,很多应用在使用-Xcomp也会有一些性能损失,当然这比使用-Xint损失的少,原因是xcomp没有让JVM启用JIT编译器的全部功能。JIT编译器可以对是否需要编译做判断,如果所有代码都进行编译的话,对于一些只执行一次的代码就 没有意义了。
  • -Xmixed是混合模式,将解释模式与编译模式进行混合使用,由jvm自己决定,这是jvm默认的模式,也是推荐使用的模式。

示例:强制设置运行模式

#强制设置为解释模式
[root@node01 test]# java	‐showversion ‐Xint TestJVM java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141‐b15)
Java HotSpot(TM) 64‐Bit Server VM (build 25.141‐b15, interpreted mode) itcast

#强制设置为编译模式
[root@node01 test]# java	‐showversion ‐Xcomp TestJVM java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141‐b15)
Java HotSpot(TM) 64‐Bit Server VM (build 25.141‐b15, compiled mode)

itcast
#注意:编译模式下,第一次执行会比解释模式下执行慢一些,注意观察。


#默认的混合模式
[root@node01 test]# java	‐showversion TestJVM java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141‐b15)
Java HotSpot(TM) 64‐Bit Server VM (build 25.141‐b15, mixed mode) itcast

2.4、-XX参数

-XX参数也是非标准参数,主要用于jvm的调优和debug操作。
-XX参数的使用有2种方式,一种是boolean类型,一种是非boolean类型:

  • boolean类型
    格式:-XX:[±]
    如:-XX:+DisableExplicitGC 表示禁用手动调用gc操作,也就是说调用System.gc()无效
  • 非boolean类型
    格式:-XX:
    如:-XX:NewRatio=1 表示新生代和老年代的比值

用法:

Here Insert Picture Description

2.5、-Xms与-Xmx参数

-Xms与-Xmx分别是设置jvm的堆内存的初始大小和最大大小。
-Xmx2048m:等价于-XX:MaxHeapSize,设置JVM最大堆内存为2048M。
-Xms512m:等价于-XX:InitialHeapSize,设置JVM初始堆内存为512M。

适当的调整jvm的内存大小,可以充分利用服务器资源,让程序跑的更快。

示例:

[root@node01 test]# java ‐Xms512m ‐Xmx2048m TestJVM itcast

2.6、查看jvm的运行参数

Sometimes we need to see jvm operating parameters, and this demand there may be two situations:
First, run the java command to print out the operating parameters;
second, to view java process running parameters

2.6.1 Print parameters when running java command

Print command parameters run java, you need to add -XX: + PrintFlagsFinal parameters.

[root@node01 test]# java ‐XX:+PrintFlagsFinal ‐version [Global flags]
uintx AdaptiveSizeDecrementScaleFactor	= 4
{product}
uintx AdaptiveSizeMajorGCDecayTimeScale	= 10
{product}
uintx AdaptiveSizePausePolicy	= 0
{product}
uintx AdaptiveSizePolicyCollectionCostMargin	= 50
{product}
uintx AdaptiveSizePolicyInitializingSteps	= 20
{product}
uintx AdaptiveSizePolicyOutputInterval	= 0
{product}
uintx AdaptiveSizePolicyWeight	= 10
{product}
uintx AdaptiveSizeThroughPutPolicy	= 0
{product}
uintx AdaptiveTimeWeight	= 25
{product}
bool AdjustConcurrency	= false
{product}
bool AggressiveOpts	= false
{product}
intx AliasLevel	= 3
{C2 product}
bool AlignVector	= true
{C2 product}
intx AllocateInstancePrefetchLines	= 1
{product}
intx AllocatePrefetchDistance	= 256
{product}
intx AllocatePrefetchInstr	= 0
{product}

…………………………略…………………………………………





bool UseXmmI2D	= false
{C1 product}
intx ValueSearchLimit	= 1000
{C2 product}
bool VerifyMergedCPBytecodes	= true
{product}
bool VerifySharedSpaces	= false
{product}
intx WorkAroundNPTLTimedWaitHang	= 1
{product}
uintx YoungGenerationSizeIncrement	= 20
{product}
uintx YoungGenerationSizeSupplement	= 80
{product}
uintx YoungGenerationSizeSupplementDecay	= 8
{product}
uintx YoungPLABSize	= 4096
{product}
bool ZeroTLAB	= false
{product}
intx hashCode	= 5
{product}          java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141‐b15)
Java HotSpot(TM) 64‐Bit Server VM (build 25.141‐b15, mixed mode)

As it can be seen from the above information, a boolean parameters and numeric type, the value of the operator is = or: =, and a value representing the default value is modified.

Example:

java ‐XX:+PrintFlagsFinal ‐XX:+VerifySharedSpaces ‐version

intx ValueMapInitialSize	= 11
{C1 product}
intx ValueMapMaxLoopSize	= 8
{C1 product}
intx ValueSearchLimit	= 1000
{C2 product}
bool VerifyMergedCPBytecodes	= true
{product}
bool VerifySharedSpaces	:= true
{product}
intx WorkAroundNPTLTimedWaitHang	= 1
{product}
uintx YoungGenerationSizeIncrement	= 20
{product}
uintx YoungGenerationSizeSupplement	= 80
{product}
uintx YoungGenerationSizeSupplementDecay	= 8
{product}
uintx YoungPLABSize	= 4096
{product}
bool ZeroTLAB	= false
{product}
intx hashCode	= 5
{product}          java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141‐b15)
Java HotSpot(TM) 64‐Bit Server VM (build 25.141‐b15, mixed mode) #可以看到VerifySharedSpaces这个参数已经被修改了。

2.6.2 Check the running jvm parameters

If you want to see a running jvm will need the help of jinfo command.
First, start a tomcat for testing, to observe the parameters jvm running under.
Here Insert Picture Description
Successful visit:
Here Insert Picture Description

#查看所有的参数,用法:jinfo ‐flags <进程id>

#通过jps 或者	jps ‐l 查看java进程
[root@node01 bin]# jps 6346 Jps
6219 Bootstrap [root@node01 bin]# jps ‐l 6358 sun.tools.jps.Jps
6219 org.apache.catalina.startup.Bootstrap [root@node01 bin]#

[root@node01 bin]# jinfo ‐flags 6219 Attaching to process ID 6219, please wait... Debugger attached successfully.
Server compiler detected. JVM version is 25.141‐b15
Non‐default VM flags: ‐XX:CICompilerCount=2 ‐XX:InitialHeapSize=31457280
‐XX:MaxHeapSize=488636416 ‐XX:MaxNewSize=162529280 ‐ XX:MinHeapDeltaBytes=524288 ‐XX:NewSize=10485760 ‐XX:OldSize=20971520 ‐ XX:+UseCompressedClassPointers ‐XX:+UseCompressedOops ‐ XX:+UseFastUnorderedTimeStamps ‐XX:+UseParallelGC
Command line:	‐Djava.util.logging.config.file=/tmp/apache‐tomcat‐ 7.0.57/conf/logging.properties ‐ Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager ‐ Djava.endorsed.dirs=/tmp/apache‐tomcat‐7.0.57/endorsed ‐ Dcatalina.base=/tmp/apache‐tomcat‐7.0.57 ‐Dcatalina.home=/tmp/apache‐ tomcat‐7.0.57 ‐Djava.io.tmpdir=/tmp/apache‐tomcat‐7.0.57/temp

#查看某一参数的值,用法:jinfo ‐flag <参数名> <进程id> [root@node01 bin]# jinfo ‐flag MaxHeapSize 6219
‐XX:MaxHeapSize=488636416
Released 1050 original articles · won praise 888 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/103972776