常用的JVM参数与命令你知道吗?


常用的jvm参数

  • -version 查看版本号

java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
  • -help 帮助说明

java -help
4f10ba7e73258ac53db0f8d8a1b4ab46.png
  • -server 指定使用服务器类

也可以选择客户端类型 -client

java -server [other options] MainClass [args...]
  • -cp 类搜索路径和jar,zip文件

java -cp  <class_path> MainClass [args...]
  • -classpath 和-cp相似

java -classpath <class_path> MainClass [args...]
  • -javaagent 加载代理相关

java -javaagent:<jarpath>[=<option>]load Java programming language agent, see java.lang.instrument

非标准参数

-X print help on non-standard options

java -X -help
-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

常用的有以下几个

# 设置初始化堆大小
-Xms<size>        set initial Java heap size
# 设置堆的最大值
-Xmx<size>        set maximum Java heap size
# 设置java栈的大小
-Xss<size>        set java thread stack size

还有常用的几个参数,发生GC或者OOM问题排查

  • 启动堆内存溢出打印

当JVM堆内存发生溢出时,也就是OOM,自动生成dump文件

-XX:+HeapDumpOnOutOfMemoryError
  • 指定堆内存溢出打印目录

表示在当前目录生成一个heap.hprof文件

-XX:HeapDumpPath=heap.hprof

日常开发中会经常启动服务参数都会加内存溢出自动导出堆转储文件

nohup java  -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=heap.hprof  -jar SpringBootDebugService-0.0.1-SNAPSHOT.jar &
1d3bebb2c37d88e567f915b4acf98c3c.png
  • 打印出GC日志

可以使用不同的垃圾收集器,对比查看GC情况

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:g1-gc.log

常用的JVM命令

jps 查看java的进程

jps
4d705900c10f1685b2a361894b717c46.png

jinfo 查看或者调整jvm的参数

[root@node02 opt] jinfo -h
Usage:
    jinfo [option] <pid>
        (to connect to running process)
    jinfo [option] <executable <core>
        (to connect to a core file)
    jinfo [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    -flag <name>         to print the value of the named VM flag
    -flag [+|-]<name>    to enable or disable the named VM flag
    -flag <name>=<value> to set the named VM flag to the given value
    -flags               to print VM flags
    -sysprops            to print Java system properties
    <no option>          to print both of the above
    -h | -help           to print this help message
  • 查看当前虚拟机的参数情况

jinfo -flags 16973
2dff1988959d9599ce1f254ecea010ed.png
  • 查看单个参数

jinfo -flag MaxHeapSize 16973
# -XX:MaxHeapSize=255852544

其他的参数就不一一列举了。

jstack 查看线程堆栈信息

jstack -l pid
5513b1bff28f335c4adc4a7f3fea33b9.png

jmap 生成虚拟机的内存转储快照

jmap -dump:file=heap.bin 8221
778fba4e4dfde9b4aed21978d8bad65b.png

jhat 分析堆转储快照

jhat -port 8899 heap.bin
374aa8af97f555ff8e0289d77a6d49b4.png

还有很多工具jconsole,jmc,jcmd,jvisualvm...此篇到此。

往期推荐

推荐两款程序员高效工作神器

Git命令基操必须要会!

非常nice! IDEA远程Debug调试程序

DataX二次开发之达梦数据库插件

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘同学要加油呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值