jvm的参数类型分为三类,分别是:
-help
-version
-Xint
-Xcomp
-XX:newSize
-XX:+UseSerialGC
jvm的标准参数,一般都是很稳定的,在未来的JVM版本中不会改变,可以使用java -help
检索出所有的标准参数。
[liuyh@VMcentos jdk1.8.0_231]$ 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.
指定程序是运行在32位还是64位环境。 java -version
命令可以查看到默认的运行环境(64-Bit):
[liuyh@VMcentos jdk1.8.0_231]$ java -version java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
可以通过-server或-client设置jvm的运行参数。 java -version
命令可以查看到默认的运行环境(Server VM):
[liuyh@VMcentos jdk1.8.0_231]$ java -version java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
public class JVMTest { public static void main(String[] args) { String test = System.getProperty("test"); if (test == null) { System.out.println("系统属性test为空"); } else { System.out.println("系统属性test= "+test); } } }
启动参数VM option增加 -Dtest=20200406
,运行后结果为
系统属性test= 20200406
jvm的-X参数是非标准参数,在不同版本的jvm中,参数可能会有所不同,可以通过java -
X查看非标准参数。
[liuyh@VMcentos jdk1.8.0_231]$ 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.
-Xms与-Xmx分别是设置jvm的堆内存的初始大小和最大大小。
-Xmx2048m:等价于-XX:MaxHeapSize,设置JVM最大堆内存为2048M。
-Xms512m:等价于-XX:InitialHeapSize,设置JVM初始堆内存为1024M。
[liuyh@VMcentos jvm]$ java -Xms1024m -Xmx2048m JVMTest 系统属性test为空
这些参数可以被松散的聚合成三类:
-XX参数的使用有2种方式,一种是boolean类型,一种是非boolean类型:
格式:-XX:[±]
如:-XX:+DisableExplicitGC 表示禁用手动调用gc操作,也就是说调用System.gc()无效
格式:-XX:
如:-XX:NewRatio=1 表示新生代和老年代的比值
使用java -XX:+PrintFlagsFinal -version打印运行参数
结果中参数有boolean类型和数字类型,值的操作符是=或:=,分别代
表默认值和被修改的值。
[liuyh@VMcentos jvm]$ jps -l 6882 sun.tools.jps.Jps 1612 /home/liuyh/apache-activemq-5.15.11//bin/activemq.jar [liuyh@VMcentos jvm]$ jinfo -flag MaxHeapSize 1612 -XX:MaxHeapSize=1073741824