1.JVM的组成
- 类加载器(ClassLoader)
- 运行时数据区(Runtime Data Area) (堆、栈)
- 执行引擎(Execution Engine) c++/C
- 本地库接口(Native Interface)
2. jvm的运行参数
2.1 三种参数类型
jvm的参数类型分为三类,分别是:
- 标准参数
-help
-version
- -X参数 (非标准参数)
-Xint
-Xcomp
- -XX参数(非stable参数)
-XX:newSize
-XX:+UseSerialGC
2.2 标准参数
jvm的标准参数,一般都是很稳定的,在未来的JVM版本中不会改变,可以使用java -help
检索出所有的标准参数。
[liuyh@VMcentos jdk1.8.0_231]$ java -helpUsage: 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 imageSee http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
2.2.1 -d32和-d64
指定程序是运行在32位还是64位环境。java -version
命令可以查看到默认的运行环境(64-Bit):
[liuyh@VMcentos jdk1.8.0_231]$ java -versionjava 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)
2.2.2 -client和-server
可以通过-server或-client设置jvm的运行参数。java -version
命令可以查看到默认的运行环境(Server VM):
[liuyh@VMcentos jdk1.8.0_231]$ java -versionjava 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 VM的初始堆空间会大一些,默认使用的是并行垃圾回收器,启动慢运行快。
- Client VM相对来讲会保守一些,初始堆空间会小一些,使用串行的垃圾回收器,它的目标是为了让JVM的启动速度更快,但运行速度会比Serverm模式慢些。
- JVM在启动的时候会根据硬件和操作系统自动选择使用Server还是Client类型的JVM。
2.2.3 -D设置系统属性参数
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
2.3 非标准参数
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 continueThe -X options are non-standard and subject to change without notice.
2.3.1 -Xint、-Xcomp、-Xmixed
- 在解释模式(interpreted mode)下,-Xint标记会强制JVM执行所有的字节码,当然这会降低运行速度,通常低10倍或更多。
- -Xcomp纯编译模式(如果方法无法编译,则回退到解释模式执行无法编译的方法)
- -Xmixed是混合模式,将解释模式与编译模式进行混合使用,由jvm自己决定,这是jvm默认的模式,也是推荐使用的模式。
2.3.2 -Xms与-Xmx参数
-Xms与-Xmx分别是设置jvm的堆内存的初始大小和最大大小。
-Xmx2048m:等价于-XX:MaxHeapSize,设置JVM最大堆内存为2048M。
-Xms512m:等价于-XX:InitialHeapSize,设置JVM初始堆内存为1024M。
[liuyh@VMcentos jvm]$ java -Xms1024m -Xmx2048m JVMTest系统属性test为空
2.4 非Stable参数(-XX)
这些参数可以被松散的聚合成三类:
- 行为参数(Behavioral Options):用于改变jvm的一些基础行为;
- 性能调优(Performance Tuning):用于jvm的性能调优;
- 调试参数(Debugging Options):一般用于打开跟踪、打印、输出等jvm参数,用于显示jvm更加详细的信息。
-XX参数的使用有2种方式,一种是boolean类型,一种是非boolean类型:
- boolean类型
格式:-XX:[±]
如:-XX:+DisableExplicitGC 表示禁用手动调用gc操作,也就是说调用System.gc()无效
- 非boolean类型
格式:-XX:
如:-XX:NewRatio=1 表示新生代和老年代的比值
2.5 查看jvm的运行参数
2.5.1 运行java命令时打印参数
使用java -XX:+PrintFlagsFinal -version打印运行参数
结果中参数有boolean类型和数字类型,值的操作符是=或:=,分别代
表默认值和被修改的值。
2.5.2查看正在运行的jvm参数
- 使用jps -l查看java进程
- 然后查看某一参数的值,用法:jinfo ‐flag <参数名> <进程id>
[liuyh@VMcentos jvm]$ jps -l6882 sun.tools.jps.Jps1612 /home/liuyh/apache-activemq-5.15.11//bin/activemq.jar[liuyh@VMcentos jvm]$ jinfo -flag MaxHeapSize 1612-XX:MaxHeapSize=1073741824