Three running parameters of JVM

Three parameter types of JVM

1) Standard parameters

The standard parameters of JVM are generally very stable and will not change in future JVM versions. You can use java -help to retrieve all standard parameters.

C:\Users\user>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>
                  使用指定的图像显示启动屏幕
有关详细信息, 请参阅 http://www.oracle.com/technetwork/java/javase/documentation/index.html。

Case 1: Check JVM version

C:\Users\user>java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Note: The -showversion parameter means that the version information is printed first, and then the following commands are executed, which is very useful during debugging

Case 2: Setting system property parameters via -D

insert image description here
insert image description here

package com.studio;
public class Test {
    
    
    public static void main(String[] args) {
    
    
        String str = System.getProperty("str");
        if (str == null) {
    
    
            System.out.println("studio");
        } else {
    
    
            System.out.println(str);
        }
    }
}
D:\JavaEE\JavaEE_2022_0311\JVMTest\src>javac com/studio/Test.java
D:\JavaEE\JavaEE_2022_0311\JVMTest\src>java -Dstr=abcd com.studio.Test
abcd

-server and -client parameters

The initial heap space of the Server VM will be larger, and the parallel garbage collector is used by default, which starts slowly and runs quickly.
The Client VM will be relatively conservative, the initial heap space will be smaller, and the serial garbage collector will be used. Its goal is to make the JVM start faster, but it will run slower than the Server VM mode.

When the JVM starts, it will automatically select the JVM of the Server or Client type according to the hardware and operating system.

32-bit operating system: If it is a Windows system, the JVM of the Client type is used by default regardless of the hardware configuration. If it is another operating system, the machine is configured with more than 2GB of memory and more than 2 CPUs to use server mode by default, otherwise use client mode.

The 64-bit operating system has only the server type and does not support the client type.
insert image description here

java -client -showversion com.studio.Test
java -server -showversion com.studio.Test

2) -X parameter (non-standard parameter)

The -X parameter of jvm is a non-standard parameter. In different versions of jvm, the parameters may be different. You can view the non-standard parameters through java - X.

D:\JavaEE\JavaEE_2022_0311\JVMTest\src>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 选项是非标准选项, 如有更改, 恕不另行通知。

-Xint、-Xcomp、-Xmixed

  • In interpreted mode, the -Xint flag forces the JVM to execute all bytecodes, which of course slows things down, usually by a factor of 10 or more.

  • The -Xcomp parameter is the opposite of -Xint. The JVM will compile all bytecodes into native code when it is first used, resulting in maximum optimization.

    However, many applications will also have some performance penalty when using -Xcomp, which of course is less than using -Xint, because -xcomp does not allow the JVM to enable the full capabilities of the JIT compiler. The JIT compiler can judge whether it needs to be compiled or not. If all the code is compiled, it does not make sense for some code that is only executed once.

  • -Xmixed is a mixed mode, which mixes the interpretation mode and the compilation mode, which is determined by the jvm itself. This is the default mode of the jvm and the recommended mode.
    insert image description here

-Xms、-Xmx、-Xss

-Xms and -Xmx are to set the initial size and maximum size of the jvm's heap memory, respectively.

-Xms512m: Equivalent to -XX:InitialHeapSize, set the JVM initial heap memory to 512M.
-Xmx2048m: Equivalent to -XX:MaxHeapSize, set the maximum heap memory of the JVM to 2048M.
-Xss256k: Equivalent to -XX:ThreadStackSize, set the Java thread stack size.

# 初始化和最大化堆都设置为8m
java -Xms8m -Xmx8m com.studio.Test

3) -XX parameter (the highest usage rate)

The -XX parameter is also a non-standard parameter, mainly used for JVM tuning and debug operations.
There are two ways to use the -XX parameter, one is boolean type, the other is non-boolean type

  • Boolean type format: -XX:[±] For example: -XX:+DisableExplicitGC Indicates that manual invocation of gc operation is disabled, that is, calling System.gc() is invalid .
  • Non-boolean type format: -XX: For example: -XX:NewRatio=1 indicates the ratio of the new generation to the old generation.
java -XX:+DisableExplicitGC -showversion com.studio.Test

View the running parameters of the jvm

print arguments when running java command

Just add -XX:+PrintFlagsFinal when running the java command.

D:\JavaEE\JavaEE_2022_0311\JVMTest\src>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                               = false                               {
    
    C2 product}
     intx AllocateInstancePrefetchLines             = 1                                   {
    
    product}
     ......

It can be seen from the above information that the parameters have boolean type and numeric type, and the value operator is = or :=, which represent the default value and the modified value respectively.
insert image description here
insert image description here

View running jvm parameters

insert image description here
insert image description here

jinfo -flags 7632
jinfo -flag InitialHeapSize 7632

Guess you like

Origin blog.csdn.net/fengsheng5210/article/details/123678695