tags: java, troubleshooting, monitor
Summarized in one sentence: simple java start command, so many secrets hidden in the original article for you announced.
1 Introduction
Just beginning to learn java students, I will never forget After installing the jdk, will use the java -version
command to test to see if the installation was successful, and that there are no other parameters can use it? Usually when developing and running java applications, often see some -D
parameters (such as when using maven, package
when use -Dmaven.test.skip
), these parameters are used to do what? There often comes to tuning, will be involved -Xms
and -Xmx
the setting, which is what does it mean? These are basically using the java
command to start the parameters used by the application, it has a lot of parameters, in particular, often used when it comes to application tuning and problem diagnosis, learning java students should find out. This paper will start the java command parameters are described in detail that focused on common settings and to set the debug monitoring.
2 java application launch
Start using a java application java
(class file), or java -jar
(jar or war package) command, java command actually creates an instance of the JVM, the java application running on this JVM instance, JVM class loader responsible, stack allocation operation or other region work, when the application exits, JVM instance will be shut down. Launch multiple java applications, will launch multiple JVM instances, they do not affect each other (but they all share the same system resources), which is why the use of a JDK, you can run multiple java logic behind applications. Use the java command to start the parameters used by the application, basically for the JVM, JVM instance by calling some initial class main () method to run a Java program, this method as a starting point the initial thread of the program, any other this thread is the initial thread started. There are two threads within the JVM: daemon threads (such as garbage collection thread) and non-daemon threads ( main
Method threads and user Thread
threads created), when the program in all non-daemon threads are terminated, JVM instances will automatically exit.
3 java application startup Parameter Description
java command exactly which parameters can be used, what role these parameters are simple without parameters java
or java -help
or java -?
, to see the use of the command and parameters described as follows:
java
The implementation of the class file, java -jar
execute jar
or war
file. The above summary of the parameters just listed out, a more detailed description of the parameters, refer to the official website of java
the command instructions ( https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html
). Start parameters used by the application using the java command, basically for the JVM, to some extent, also known as JVM arguments. Overall, java startup parameters is divided into three categories, namely:
- Standard parameters (-): the relative stability of parameters, each version of the JVM are available.
- Non-standard X parameter (-X): default jvm realize the function of these parameters, but does not guarantee that all jvm implementations to meet, not to ensure backward compatibility.
- XX parameter (-XX): jvm implement each of these parameters will vary, the future may be canceled at any time.
Below these parameters will be explained.
3.1 standard parameters (-)
Use from the front java -?
you can see, with -
parameters at the beginning, all belong to the standard parameters, we used the -help
, -version
, -classpath
, -Dproperty=value
are all part of the standard parameters. The parameters are described below:
-d32及-d64 分别表示应用运行在32位或64位的环境中,使用Java HotSpot Server VM的默认使用的是server模式,而server模式默认使用的是-d64,因此在没有使用此参数时,默认就是-d64。
-server 选择 "server" VM,默认 VM 是 server,表示是在服务器类计算机上运行。
-cp或-classpath <目录和 zip/jar 文件的类搜索路径>linux用":",windows用";"来分隔目录, JAR 档案和 ZIP 档案列表, 用于搜索类文件。
使用-classpath后jvm将不再使用CLASSPATH中的类搜索路径,如果-classpath和CLASSPATH都没有设置,则jvm使用当前路径(.)作为类搜索路径。
-D<名称>=<值> 设置系统属性,运行在此jvm之上的应用程序可用System.getProperty(“property”)得到value的值。
如果value中有空格,则需要用双引号将该值括起来,如-Dfoo=”foo bar”。该参数通常用于设置系统级全局变量值,如配置文件路径,以便该属性在程序中任何地方都可访问。
-verbose:[class|gc|jni] 启用详细输出,一般在调试和诊断时,都会把gc的详细信息输出
-version 输出产品版本并退出
-version:<值> 需要指定的版本才能运行
-showversion 输出产品版本并继续,即输出版本后,继续按java执行,这是跟-version的区别
-jre-restrict-search | -no-jre-restrict-search 在版本搜索中包括/排除用户专用 JRE
-? -help 输出此帮助消息
-X 输出非标准选项的帮助
-ea或-enableassertions [:<packagename>...|:<classname>] 按指定的粒度启用断言,默认jvm关闭断言机制
-da或-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> 使用指定的图像显示启动屏幕,一般用于图形编程。
复制代码
From the above description, we know commonly used -version
, -classpath
, -Dproperty=value
it is what to do for the. Special mention -classpath
(previously encountered due to this cause operational problems), jvm when loading the class, the search path is the path, and its use in linux and windows delimiter is not the same, with linux :
, windows used ;
to separate.
3.2 Non-standard parameters X (-X)
Use the command java -X
, to the non-standard output parameters, usually used, we used more is -Xloggc
, -Xms<size>
, -Xmx<size>
, -Xss<size>
, -Xmn<size>
, and described in detail as follows:
-Xmixed 默认是mixed,使用它们来设置JVM的本地代码编译模式
-Xint 表示解释执行,所有的字节码将被直接执行,而不会编译成本地码
-Xcomp 表示第一次使用就编译成本地代码。
-Xbatch 禁止后台代码编译,强制在前台编译,编译完成之后才能进行代码执行,默认情况下,jvm在后台进行编译,若没有编译完成,则前台运行代码时以解释模式运行
-Xbootclasspath: 设置搜索路径以引导类和资源,让jvm从指定路径(可以是分号分隔的目录、jar、或者zip)中加载bootclass,用来替换jdk的rt.jar
-Xbootclasspath/a: 附加在引导类路径末尾
-Xbootclasspath/p: 置于引导类路径之前,让jvm优先于bootstrap默认路径加载指定路径的所有文件
-Xcheck:jni 对JNI函数进行附加check;此时jvm将校验传递给JNI函数参数的合法性,在本地代码中遇到非法数据时,jmv将报一个致命错误而终止;使用该参数后将造成性能下降,请慎用。
-Xfuture 让jvm对类文件执行严格的格式检查(默认jvm不进行严格格式检查),以符合类文件格式规范,推荐开发人员使用该参数
-Xincgc 开启增量gc(默认为关闭);这有助于减少长时间GC时应用程序出现的停顿;但由于可能和应用程序并发执行,所以会降低CPU对应用的处理能力
-Xloggc:file 与-verbose:gc功能类似,只是将每次GC事件的相关情况记录到一个文件中,文件的位置最好在本地,以避免网络的潜在问题。若与verbose命令同时出现在命令行中,则以-Xloggc为准。
-Xms 指定jvm堆的初始大小,默认为物理内存的1/64,最小为1M;可以指定单位,比如k、m,若不指定,则默认为字节。
-Xmx 指定jvm堆的最大值,默认为物理内存的1/4或者1G,最小为2M;单位与-Xms一致。
-Xss 设置单个线程栈的大小,一般默认为512k。
-Xmn 设置堆(heap)的年轻代的初始值及最大值,单位与-Xms一致,年轻代是存储新对象的地址,也是GC发生得最频繁的地方,若设置过小,则会容易触发年轻代垃圾回收(minor gc),若设置过大,只触发full gc,则占用时间会很长,oracle建议是把年轻代设置在堆大小的四份之一到一半的。这命令同时设置了初始值和最大值,可以使用-XX:NewSize和-XX:MaxNewSiz来分别设置。
-XshowSettings 显示所有设置并继续
复制代码
The above parameters, -Xms<size>
, -Xmx<size>
, -Xss<size>
, -Xmn<size>
are our very important performance optimization parameters -Xloggc
are troubleshooting without the professional players tracking tool case.
3.3 XX parameter (-XX)
Such parameters are very rich, while including advanced operating parameters, senior JIT compiler parameters, advanced maintenance parameters and advanced GC parameters, you can see it in the official website all the parameters ( https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html
), various versions of jvm realize there may be different. Wherein by format, divided into two categories, one is a boolean
type, mainly for function switch, one is the key-value
type, the main performance, and the like set the debug parameter, include the following main parameters used.
3.3.1 boolean type
Such parameters, formats: -XX:[+-]<name>
as a functional switch indicates attribute enabled or disabled. Here are some examples:
-XX:+PrintFlagsFinal 输出参数的最终值
-XX:+PrintFlagsInitial 输出参数的默认值
-XX:-DisableExplicitGC 禁止调用System.gc();但jvm的gc仍然有效
-XX:+MaxFDLimit 最大化文件描述符的数量限制
-XX:+ScavengeBeforeFullGC 新生代GC优先于Full GC执行
-XX:+UseGCOverheadLimit 在抛出OOM之前限制jvm耗费在GC上的时间比例
-XX:-UseConcMarkSweepGC 对老生代采用并发标记交换算法进行GC
-XX:-UseParallelGC 启用并行GC
-XX:-UseParallelOldGC 对Full GC启用并行,当-XX:-UseParallelGC启用时该项自动启用
-XX:-UseSerialGC 启用串行GC
-XX:+UseThreadPriorities 启用本地线程优先级
-XX:-UseG1GC 启用G1的GC
复制代码
3.3.2 key-value type
Such parameters, formats: -XX:<name>=<value>
indicates the value of name attribute value. While monitoring the performance tuning and debugging, it will be frequently used.
- Performance Tuning
When tuning, tuning mainly JVM memory allocation, comprising heap size, the size of the young generation, like the young ratio of the old generation.
-XX:LargePageSizeInBytes=4m 设置用于Java堆的大页面尺寸
-XX:MaxHeapFreeRatio=70 GC后java堆中空闲量占的最大比例
-XX:MaxNewSize=size 新生成对象能占用内存的最大值
-XX:MaxPermSize=64m 老生代对象能占用内存的最大值
-XX:MinHeapFreeRatio=40 GC后java堆中空闲量占的最小比例
-XX:NewRatio=2 新生代内存容量与老生代内存容量的比例
-XX:NewSize=2.125m 新生代对象生成时占用内存的默认值
-XX:ReservedCodeCacheSize=32m 保留代码占用的内存容量
-XX:ThreadStackSize=512 设置线程栈大小,若为0则使用系统默认值
-XX:+UseLargePages 使用大页面内存
复制代码
- Debug Monitoring
In the application needs to be monitored, in particular, to observe the situation GC, OOM after inspection issues.
-XX:-CITime 打印消耗在JIT编译的时间
-XX:ErrorFile=./hs_err_pid<pid>.log 保存错误日志或者数据到文件中
-XX:-ExtendedDTraceProbes 开启solaris特有的dtrace探针
-XX:HeapDumpPath=./java_pid<pid>.hprof 指定导出堆信息时的路径或文件名
-XX:-HeapDumpOnOutOfMemoryError 当首次遭遇OOM时导出此时堆中相关信息
-XX:OnError="<cmd args>;<cmd args>" 出现致命ERROR之后运行自定义命令
-XX:OnOutOfMemoryError="<cmd args>;<cmd args>" 当首次遭遇OOM时执行自定义命令
-XX:-PrintClassHistogram 遇到Ctrl-Break后打印类实例的柱状信息,与jmap -histo功能相同
-XX:-PrintConcurrentLocks 遇到Ctrl-Break后打印并发锁的相关信息,与jstack -l功能相同
-XX:-PrintCommandLineFlags 打印在命令行中出现过的标记
-XX:-PrintCompilation 当一个方法被编译时打印相关信息
-XX:-PrintGC 每次GC时打印相关信息
-XX:-PrintGC Details 每次GC时打印详细信息
-XX:-PrintGCTimeStamps 打印每次GC的时间戳
-XX:-TraceClassLoading 跟踪类的加载信息
-XX:-TraceClassLoadingPreorder 跟踪被引用到的所有类的加载信息
-XX:-TraceClassResolution 跟踪常量池
-XX:-TraceClassUnloading 跟踪类的卸载信息
-XX:-TraceLoaderConstraints 跟踪类加载器约束的相关信息
复制代码
4 common java application startup parameters
After the introduction of a few chapters earlier, it should have to start the parameters of java (JVM parameters) a certain understanding, but too many parameters, can not put all the parameters have to remember, if necessary, recommend everyone to see -help
or Tell me what network instructions to view it. In many cases, we only need to remember a few commonly used to. The following JVM parameters commonly used to sum up.
4.1 common standard parameters
-version
Scene: I want to see the JDKjava -version
version .-D<名称>=<值>
Scene: maven skip unit testing, usingjava -Dmaven.test.skip=true
,-cp或-classpath
Scene: jar package provided to load positions, usingjava -cp lib/test.jar com.test.TestMain
-verbose:gc
Scene: GC output Details
4.2 Common parameters X
-Xms<size>
And-Xmx<size>
, Scene: oom becomes insufficient due to a memory, the size of the transfer piles of a setting of 1G, canjava -Xms1024m -Xmx1024m
, usually in order to avoid the GC, frequent-Xms
, and-Xmx
is set to the same.-Xss<size>
Scene: Thread local variables and operand, large adjusting the thread stack size, you canjava -Xss1024k
-Xmn<size>
Scene: the young generation size is set to 512m, canjava -Xmn512m
-Xloggc:file
Scene: the case of GC-related events each record in a file for subsequent analysis,java -Xloggc:logs/gc.log
4.3 Common parameters XX
Print GC-related content, including heap case, details of GC, GC time, when OOM occurs, a snapshot, the occurrence of error is the error log and other records, as follows:
-XX:+PrintHeapAtGC
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintGCTimeStamps
-XX:+PrintTenuringDistribution
-XX:+PrintGCApplicationStoppedTime
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=logs/heapdump.hprof
When OOM occurs, dump out the snapshot to fileheapdump.hprof
in.-XX:ErrorFile=logs/java_error_%p.log
, JVM when an error occurs, the log output to thejava_error_%p.log
middle.
The above parameters are a high degree of use parameters, using the java
start application command, these parameters can be combined with, for subsequent tuning and problem diagnosis.
5 summary
Simple java start command, use that so complicated, of course, in general, use only java
or java -jar
to start the application by default value, there will not be a big problem. Just comes to tuning, monitoring, diagnosis, understanding of these parameters, senior programmer is undoubtedly necessary skills. We hope this paper, everyone java
commands and parameters can be aware of.
Reference material
- JDK Tools Reference document :
https://docs.oracle.com/javase/8/docs/technotes/tools/unix
Related Reading
- " The Java application monitoring (1) application monitoring technology -java programmer should know ":
https://juejin.im/post/5d5fde94f265da03d42fae8b