JVM命令行参数解析

时间:2023-03-08 19:13:27

1. java命令行参数

  • 先看java命令行的参数
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$ java
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
-zero to select the "zero" VM
-dcevm to select the "dcevm" VM
The default VM is server,
because you are running on a server-class machine. -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.
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$ java -version
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-1~deb9u1-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$
  • 看一个实际运行的java命令:
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$ ps aux | grep java
solr 1 0.2 13.3 3060500 272904 ? Ssl Oct08 44:03 /docker-java-home/jre/bin/java -server -Xms512m -Xmx512m -XX:NewRatio=3 -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ConcGCThreads=4 -XX:ParallelGCThreads=4 -XX:+CMSScavengeBeforeRemark -XX:PretenureSizeThreshold=64m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=50 -XX:CMSMaxAbortablePrecleanTime=6000 -XX:+CMSParallelRemarkEnabled -XX:+ParallelRefProcEnabled -XX:-OmitStackTraceInFastThrow -verbose:gc -XX:+PrintHeapAtGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:/opt/solr/server/logs/solr_gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=9 -XX:GCLogFileSize=20M -Dsolr.log.dir=/opt/solr/server/logs -Djetty.port=8983 -DSTOP.PORT=7983 -DSTOP.KEY=solrrocks -Duser.timezone=UTC -Djetty.home=/opt/solr/server -Dsolr.solr.home=/opt/solr/server/solr -Dsolr.install.dir=/opt/solr -Dsun.net.inetaddr.ttl=60 -Dsun.net.inetaddr.negative.ttl=60 -Xss256k -jar start.jar --module=http
  • -Xms512m -Xmx512m

  • -XX:NewRatio=3

    • 用来来指定新生代和整个堆的大小比例,或者直接用–XX:NewSize来指定所需的新生代空间。如果设置了NewRatio,那么整个堆空间的1/(NewRatio +1)就是新生代空间的大小。
    • 使用CMS垃圾回收时,需要设置一个充足的新生代空间。然而,当新生代空间的大小超过一个特定的水平,程序的响应能力会被降低。
  • -XX:SurvivorRatio=4

    • Eden区与Survivor区的大小比值。设置为4,则两个Survivor区与一个Eden区的比值为2:4,一个Survivor区占整个新生代的1/6。
    • 参考:JVM系列三:JVM参数设置、分析
  • -XX:TargetSurvivorRatio=90

  • -XX:MaxTenuringThreshold=8

    • 设置对象在新生代中最大的存活次数,最大值15,并行回收机制默认为15,CMS默认为4。每经过一次YGC,年龄加1,当survivor区的对象年龄达到TenuringThreshold时,表示该对象是长存活对象,就会直接晋升到老年代。
    • 如果设置为0的话,则年轻代对象不经过Survivor区,直接进入年老代. 对于年老代比较多的应用,可以提高效率.如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象再年轻代的存活时间,增加在年轻代即被回收的概率。该参数只有在串行GC时才有效.
  • -XX:+UseConcMarkSweepGC

    • 使用CMS内存收集算法
    • 启用CMS低停顿垃圾收集器,减少FGC的暂停时间
  • -XX:+UseParNewGC

    • 设置新生代为并行收集。可与CMS收集同时使用JDK5.0以上,JVM会根据系统配置自行设置,所以无需再设置此值
  • -XX:ConcGCThreads=4

  • -XX:ParallelGCThreads=4

    • 并行收集器的线程数。
    • 此值最好配置与处理器数目相等,同样适用于CMS
  • -XX:+CMSScavengeBeforeRemark

    • 参考:JVM GC算法 CMS 详解(转)
    • 在CMS GC前启动一次ygc,目的在于减少old gen对ygc gen的引用,降低remark时的开销-----一般CMS的GC耗时 80%都在remark阶段
    • 开启-XX:+CMSScavengeBeforeRemark选项,强制remark之前开始一次minor gc,减少remark的暂停时间,但是在remark之后也将立即开始又一次minor gc。
  • -XX:PretenureSizeThreshold=64m

    • 对象超过多大是直接在旧生代分配
    • 单位字节 新生代采用Parallel Scavenge GC时无效另一种直接在旧生代分配的情况是大的数组对象,且数组中无外部引用对象.
  • -XX:+UseCMSInitiatingOccupancyOnly

    • 命令JVM不基于运行时收集的数据来启动CMS垃圾收集周期,禁止hostspot自行触发CMS GC。
    • 只有当我们充足的理由(比如测试)并且对应用程序产生的对象的生命周期有深刻的认知时,才应该使用该标志。
  • -XX:CMSInitiatingOccupancyFraction=50

    • 设定CMS在对内存占用率达到50%的时候开始GC(因为CMS会有浮动垃圾,所以一般都较早启动GC);
    • 这两个设置一般配合使用,一般用于『降低CMS GC频率或者增加频率、减少GC时长』的需求;
      • -XX:CMSInitiatingOccupancyFraction=70 是指设定CMS在对内存占用率达到70%的时候开始GC(因为CMS会有浮动垃圾,所以一般都较早启动GC);
      • -XX:+UseCMSInitiatingOccupancyOnly 只是用设定的回收阈值(上面指定的70%),如果不指定,JVM仅在第一次使用设定值,后续则自动调整.
  • -XX:CMSMaxAbortablePrecleanTime=6000

    - 参考:Tenured 区并发垃圾回收器CMS介绍

  • -XX:+CMSParallelRemarkEnabled

    • 降低标记停顿
  • -XX:+ParallelRefProcEnabled

    • 参考:一步步优化JVM五:优化延迟或者响应时间(3)
    • 这个选项可以用HotSpot VM的任何一种垃圾回收器上,他会是用多个的引用处理线程,而不是单个线程。这个选项不会启用多线程运行方法的finalizer。他会使用很多线程去发现需要排队通知的finalizable对象。
  • -XX:-OmitStackTraceInFastThrow

  • -verbose:gc

  • -XX:+PrintHeapAtGC

    • 打印GC前后的详细堆栈信息
  • -XX:+PrintGCDetails

    • 需要在生产环境或者压测环境中测量这些参数下系统的表现,这时候需要打开GC日志查看具体的信息,因此加上参数:-verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/home/test/logs/gc.log
  • -XX:+PrintGCDateStamps

  • -XX:+PrintGCTimeStamps

  • -XX:+PrintTenuringDistribution

    • 查看每次minor GC后新的存活周期的阈值
  • -XX:+PrintGCApplicationStoppedTime

    • 打印垃圾回收期间程序暂停的时间.可与上面混合使用
  • -Xloggc:/opt/solr/server/logs/solr_gc.log

    • 把相关日志信息记录到文件以便分析.与上面几个配合使用
  • -XX:+UseGCLogFileRotation

    • 启用GC日志文件的自动转储
  • -XX:NumberOfGCLogFiles=9

    • GC日志文件的循环数目
  • -XX:GCLogFileSize=20M

    • 控制GC日志文件的大小
    • 参考:-xx:+usegclogfilerotation
      • Built-in support for GC log rotation has been added to the HotSpot JVM. It is described in the RFE 6941923 and is available in: Java 6 Update 34 Java 7 Update 2 (but there is no reference to it in these release notes)。
      • There are three new JVM flags that can be used to enable and configure it:
      • -XX:+UseGCLogFileRotation

        must be used with -Xloggc:;

        -XX:NumberOfGCLogFiles=

        must be >=1, default is one;

        -XX:GCLogFileSize=M (or K)

        default will be set to 512K.
  • -Xss256k

    • 每个线程的堆栈大小
    • JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K.更具应用的线程所需内存大小进行 调整.在相同物理内存下,减小这个值能生成更多的线程.但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左右。

1.2 javap反编译工具

  • 将class文件反编译为字节码
  • 使用-XX:+PrintAssembly参数来输出反汇编

1.3 java命令行参数使用

  • -XX:+PrintGCDetails,在发生垃圾收集行为的时候打印内存回收日志。