当前位置: 代码迷 >> 综合 >> jcmd、jstack、jstat、jmap的应用
  详细解决方案

jcmd、jstack、jstat、jmap的应用

热度:78   发布时间:2023-10-29 19:34:01.0

jcmd、jstack、jstat、jmap的应用

  • 背景
  • 说明
  • jcmd: Java cmd
  • jstack: Java stack
  • jstat
  • jmap
  • 堆栈溢出的可能性
    • 栈 程序所要求的栈深度过大

背景

说明

java堆栈信息应用

jcmd: Java cmd

功能: 导出堆、查看Java进程、导出线程信息、执行GC

我一般用来查看机器上的jvm进程pid, 和jps是一样的

jcmd
jps

jstack: Java stack

功能:分析java应用的线程堆栈信息

在代码设计层面、code review基本就杜绝了,所以我在实际中没有使用过该工具

Usage:jstack [-l] <pid>(to connect to running process)jstack -F [-m] [-l] <pid>(to connect to a hung process)jstack [-m] [-l] <executable> <core>(to connect to a core file)jstack [-m] [-l] [server_id@]<remote server IP or hostname>(to connect to a remote debug server)Options:-F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)-m  to print both java and native frames (mixed mode)-l  long listing. Prints additional information about locks-h or -help to print this help message

jstat

功能:用于查看堆内存各年代的使用量,加载类的使用量

当发生堆溢出导致项目无法使用的时候,用jstat分析各内存使用情况,分析gc情况,使用为 jstat -gcold pid

Usage: jstat -help|-optionsjstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]Definitions:<option>      An option reported by the -options option<vmid>        Virtual Machine Identifier. A vmid takes the following form:<lvmid>[@<hostname>[:<port>]]Where <lvmid> is the local vm identifier for the targetJava virtual machine, typically a process id; <hostname> isthe name of the host running the target Java virtual machine;and <port> is the port number for the rmiregistry on thetarget host. See the jvmstat documentation for a more completedescription of the Virtual Machine Identifier.<lines>       Number of samples between header lines.<interval>    Sampling interval. The following forms are allowed:<n>["ms"|"s"]Where <n> is an integer and the suffix specifies the units as milliseconds("ms") or seconds("s"). The default units are "ms".<count>       Number of samples to take before terminating.-J<flag>      Pass <flag> directly to the runtime system.--options:-class              类加载器
-compiler           JIT
-gc                 gc堆状态
-gccapacity         各区大小
-gccause            最近一次Gc信息统计
-gcmetacapacity     
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcutil             Gc统计汇总
-printcompilation   HotSpot编译统计

jmap

功能:查看堆内对象的统计信息,ClassLoader 的信息以及 finalizer 队列。可以生成java程序的dump程序

我一般只应用 jmap histo pid 查看对象的统计信息

Usage:jmap [option] <pid>(to connect to running process)jmap [option] <executable <core>(to connect to a core file)jmap [option] [server_id@]<remote server IP or hostname>(to connect to remote debug server)where <option> is one of:<none>               to print same info as Solaris pmap-heap                to print java heap summary-histo[:live]        to print histogram of java object heap; if the "live"suboption is specified, only count live objects-clstats             to print class loader statistics-finalizerinfo       to print information on objects awaiting finalization-dump:<dump-options> to dump java heap in hprof binary formatdump-options:live         dump only live objects; if not specified,all objects in the heap are dumped.format=b     binary formatfile=<file>  dump heap to <file>Example: jmap -dump:live,format=b,file=heap.bin <pid>-F                   force. Use with -dump:<dump-options> <pid> or -histoto force a heap dump or histogram when <pid> does notrespond. The "live" suboption is not supportedin this mode.-h | -help           to print this help message-J<flag>             to pass <flag> directly to the runtime system

堆栈溢出的可能性

  1. poi非流式导出大量数据
  2. 全局容器存储对象的引用导致对象无法Gc
  3. new 对象太多
  4. 做业务计算时使用的对象太多 select * from table;

栈 程序所要求的栈深度过大

  1. 递归导致
  2. 死循环
  相关解决方案