JVM的参数讲解

在jvm中有很多的参数可以进行设置,这样可以让jvm在各种环境中都能够高效的运行。绝大部分的参数保持默认 即可

一、 3种参数类型

jvm的参数类型分为三类,分别是:

  • 标准参数 -help -version -X参数
  • (非标准参数) -Xint -Xcomp -XX参数
  • (使用率较高) -XX:newSize -XX:+UseSerialGC

二 标准参数

jvm的标准参数,一般都是很稳定的,在未来的JVM版本中不会改变,可以使用java -help检索出所有的标准参数:

[root@VM_0_8_centos xu]# java -help
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32	  use a 32-bit data model if available
    -d64	  use a 64-bit data model if available
    -server	  to select the "server" VM
                  The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.

2.1 实战1 :通过-D设置系统属性参数

public class TestJVM {

	public static void main(String[] args) {
		String str = System.getProperty("str");
		if (str == null) {
			System.out.println("beishan");
		} else {
			System.out.println(str);
		}
	}
}
[root@VM_0_8_centos xu]# javac TestJVM.java 
[root@VM_0_8_centos xu]# java TestJVM
beishan
[root@VM_0_8_centos xu]# java -Dstr=123 TestJVM
123
[root@VM_0_8_centos xu]# 

2.2-server与-client参数

可以通过-server或-client设置jvm的运行参数。

  • 它们的区别是Server VM的初始堆空间会大一些,默认使用的是并行垃圾回收器,启动慢运行快。
  • Client VM相对来讲会保守一些,初始堆空间会小一些,使用串行的垃圾回收器,它的目标是为了让JVM的启 动速度更快,但运行速度会比Serverm模式慢些。
  • JVM在启动的时候会根据硬件和操作系统自动选择使用Server还是Client类型的JVM。
  • 32位操作系统
    如果是Windows系统,不论硬件配置如何,都默认使用Client类型的JVM。
    如果是其他操作系统上,机器配置有2GB以上的内存同时有2个以上CPU的话默认使用server模式,否则 使用client模式。
  • 64位操作系统 只有server类型,不支持client类型
[root@VM_0_8_centos xu]# java -client -showversion TestJVM
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

beishan
[root@VM_0_8_centos xu]# java -server -showversion TestJVM
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

beishan

#由于机器是64位系统,所以不支持client模式

三 -X参数

jvm的-X参数是非标准参数,在不同版本的jvm中,参数可能会有所不同,可以通过java -X查看非标准参数。

[root@VM_0_8_centos xu]# java -x
Unrecognized option: -x
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
[root@VM_0_8_centos xu]# java -X
    -Xmixed           mixed mode execution (default)
    -Xint             interpreted mode execution only
    -Xbootclasspath:<directories and zip/jar files separated by :>
                      set search path for bootstrap classes and resources
    -Xbootclasspath/a:<directories and zip/jar files separated by :>
                      append to end of bootstrap class path
    -Xbootclasspath/p:<directories and zip/jar files separated by :>
                      prepend in front of bootstrap class path
    -Xdiag            show additional diagnostic messages
    -Xnoclassgc       disable class garbage collection
    -Xincgc           enable incremental garbage collection
    -Xloggc:<file>    log GC status to a file with time stamps
    -Xbatch           disable background compilation
    -Xms<size>        set initial Java heap size
    -Xmx<size>        set maximum Java heap size
    -Xss<size>        set java thread stack size
    -Xprof            output cpu profiling data
    -Xfuture          enable strictest checks, anticipating future default
    -Xrs              reduce use of OS signals by Java/VM (see documentation)
    -Xcheck:jni       perform additional checks for JNI functions
    -Xshare:off       do not attempt to use shared class data
    -Xshare:auto      use shared class data if possible (default)
    -Xshare:on        require using shared class data, otherwise fail.
    -XshowSettings    show all settings and continue
    -XshowSettings:all
                      show all settings and continue
    -XshowSettings:vm show all vm related settings and continue
    -XshowSettings:properties
                      show all property settings and continue
    -XshowSettings:locale
                      show all locale related settings and continue

The -X options are non-standard and subject to change without notice.

3.1 -Xint、-Xcomp、-Xmixed

解释模式:
编译模式:
两者混合模式:

解释编译混合!

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

beishan
[root@VM_0_8_centos xu]# java  -showversion -Xcomp TestJVM 
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, compiled mode)

beishan
[root@VM_0_8_centos xu]# java  -showversion  TestJVM 
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

beishan
[root@VM_0_8_centos xu]# 

四 -XX参数

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

  • boolean类型 格式:-XX:[±] 表示启用或禁用属性
    如:-XX:+DisableExplicitGC 表示禁用手动调用gc操作,也就是说调用System.gc()无效
  • 非boolean类型
    格式:-XX:= 表示属性的值为
    如:-XX:NewRatio=1 表示新生代和老年代的比值
[root@VM_0_8_centos xu]# java -showversion -XX:+DisableExplicitGC TestJVM
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

beishan

五 、-Xms与-Xmx参数

-Xms与-Xmx分别是设置jvm的堆内存的初始大小和大大小。
-Xmx2048m:等价于-XX:MaxHeapSize,设置JVM大堆内存为2048M。
-Xms512m:等价于-XX:InitialHeapSize,设置JVM初始堆内存为512M。
适当的调整jvm的内存大小,可以充分利用服务器资源,让程序跑的更快。

[root@node01 test]# java -Xms512m -Xmx2048m TestJVM

六 查看jvm的运行参数

有些时候我们需要查看jvm的运行参数,这个需求可能会存在2种情况:
第一,运行java命令时打印出运行参数;
第二,查看正在运行的java进程的参数

6.1 运行java命令时打印参数

java -XX:+PrintFlagsFinal -version 
[root@VM_0_8_centos xu]# java -XX:+PrintFlagsFinal -version 
[Global flags]
     intx ActiveProcessorCount                      = -1                                  {product}
    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 AggressiveHeap                            = false                               {product}
     bool AggressiveOpts                            = false                               {product}
     intx AliasLevel                                = 3                                   {C2 product}
     bool AlignVector                               = true                                {C2 product}
     intx AllocateInstancePrefetchLines             = 1                                   {product}

6.2 查看正在运行的jvm参数

如果想要查看正在运行的jvm就需要借助于jinfo命令查看

资料:
链接:https://pan.baidu.com/s/1nS0viB6EyAKZNNeKyIZcWA
提取码:kltv

rz 上传  
tar -zxvf apache-tomcat-7.0.57.tar.gz   
cd apache-tomcat-7.0.57  
cd bin/  ./startup.sh
[root@VM_0_8_centos bin]# jps
28519 Bootstrap
28607 Jps
[root@VM_0_8_centos bin]# jps -l
28519 org.apache.catalina.startup.Bootstrap
28617 sun.tools.jps.Jps
[root@VM_0_8_centos bin]# jinfo -flags 28519
Attaching to process ID 28519, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.221-b11
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=31457280 -XX:MaxHeapSize=482344960 -XX:MaxNewSize=160759808 -XX:MinHeapDeltaBytes=196608 -XX:NewSize=10485760 -XX:OldSize=20971520 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops 
Command line:  -Djava.util.logging.config.file=/opt/xu/soft/apache-tomcat-7.0.57/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/xu/soft/apache-tomcat-7.0.57/endorsed -Dcatalina.base=/opt/xu/soft/apache-tomcat-7.0.57 -Dcatalina.home=/opt/xu/soft/apache-tomcat-7.0.57 -Djava.io.tmpdir=/opt/xu/soft/apache-tomcat-7.0.57/temp
[root@VM_0_8_centos bin]# 

#查看所有的参数,用法:jinfo -flags <进程id>
#通过jps 或者 jps -l 查看java进程
#查看某一参数的值,用法:jinfo -flag <参数名> <进程id>

发布了107 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/beishanyingluo/article/details/104966782
今日推荐