JVM_Java应用问题排查_远程debug

JVM_Java应用问题排查_远程debug

  • 常用命令
  • JVisualVM使用
  • 拓展-远程debug

常用命令

Java平台,标准版工具参考 https://docs.oracle.com/javase/8/docs/technotes/tools/unix/index.html
(1)jinfo
可以查看运行中jvm的全部参数,还可以设置部分参数等

E:\workspace-sts-3>jinfo -flags 2608
Attaching to process ID 2608, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.31-b07
Non-default VM flags: -XX:-BytecodeVerificationLocal -XX:-BytecodeVerificationRemote -XX:CICompilerCount=4 -XX:InitialHeapSize=266338304 -XX:+ManagementServer -XX:MaxHeapSize=4261412864 -XX:MaxNewSize=1420296192 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=88604672 -XX:OldSize=177733632 -XX:TieredStopAtLevel=1 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation 
-XX:+UseParallelGC
Command line:  -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=59984 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Xverify:none -XX:TieredStopAtLevel=1 -Dfile.encoding=UTF-8

(2)jps
查看所有Java进程

E:\workspace-sts-3>jps -l
2608 com.example.demo.DemoApplication
9952 org.jetbrains.idea.maven.server.RemoteMavenServer
12600
16072
11980 sun.tools.jps.Jps

(3)jstat
查看堆内存各部分的使用量,以及加载类的数量等
类加载统计示例如下:

E:\workspace-sts-3>jstat -class 2608
Loaded  Bytes  Unloaded  Bytes     Time
加载class数量  所占用空间大小  未加载数量  未加载占用空间   花费时间
  6090 11047.5        2     1.9       2.89

(4)jmap
查看内存使用情况

C:\Users\vander>jmap -heap 3760
Attaching to process ID 3760, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.31-b07

using thread-local object allocation.
Parallel GC with 10 thread(s)

Heap Configuration:   #堆内存配置信息 
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 4261412864 (4064.0MB)
   NewSize                  = 88604672 (84.5MB)
   MaxNewSize               = 1420296192 (1354.5MB)
   OldSize                  = 177733632 (169.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:  # 堆内存的使用情况
PS Young Generation  #年轻代
Eden Space:
   capacity = 66584576 (63.5MB)
   used     = 28971920 (27.629776000976562MB)
   free     = 37612656 (35.87022399902344MB)
   43.511458269254426% used
From Space:
   capacity = 11010048 (10.5MB)
   used     = 10993344 (10.48406982421875MB)
   free     = 16704 (0.01593017578125MB)
   99.84828404017857% used
To Space:
   capacity = 11010048 (10.5MB)
   used     = 0 (0.0MB)
   free     = 11010048 (10.5MB)
   0.0% used
PS Old Generation   #年老代
   capacity = 177733632 (169.5MB)
   used     = 2203432 (2.1013565063476562MB)
   free     = 175530200 (167.39864349365234MB)
   1.2397383518275258% used

11586 interned Strings occupying 1033728 bytes.

查看所有对象,包括活跃以及非活跃的

jmap -histo 3760 | more

查看活跃对象

jmap -histo:live 3760 | more

使用命令生成heap.hprof文件

jmap -dump:format=b,file=heap.hprof 进程号

方式一jhat命令方式

jhat ‐port 9999 /tmp/heap.hprof    #浏览器访问

方式二使用MAT工具打开heap.hprof文件进行分析
Eclipse MAT工具下载地址 https://www.eclipse.org/mat/downloads.php
JVM_Java应用问题排查_远程debug_第1张图片
备注:使用jmap和MAT工具可排查内存溢出等问题
(5)jstack
用于生成java虚拟机当前时刻的线程快照,线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合

jstack 进程id > info.txt
top -p 进程id -H  #显示十进制的线程id
printf "%x" 线程id    

备注:使用jstack可排查死循环或死锁等问题

JVisualVM使用

(1)远程监控
监控远程tomcat:
1、进入Tomcat/bin目录,修改catalina.sh
2、添加一下配置

CATALINA_OPTS=”$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.164.120
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false”

监控远程jar:

nohup java -Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.164.120
-Dcom.sun.management.jmxremote.port=9998
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false -jar demo.jar &

拓展-远程debug

(1)Tomcat
添加配置:catalina.sh

CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8899" 

(2)Java

java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8899 demo.jar 

你可能感兴趣的:(JVM,Java)