jcmd、jstack、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|-options
jstat -<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 target
Java virtual machine, typically a process id; <hostname> is
the name of the host running the target Java virtual machine;
and <port> is the port number for the rmiregistry on the
target host. See the jvmstat documentation for a more complete
description 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 format
dump-options:
live dump only live objects; if not specified,
all objects in the heap are dumped.
format=b binary format
file=<file> dump heap to <file>
Example: jmap -dump:live,format=b,file=heap.bin <pid>
-F force. Use with -dump:<dump-options> <pid> or -histo
to force a heap dump or histogram when <pid> does not
respond. The "live" suboption is not supported
in this mode.
-h | -help to print this help message
-J<flag> to pass <flag> directly to the runtime system
堆棧溢出的可能性
堆
- poi非流式導出大量數據
- 全局容器存儲對象的引用導致對象無法Gc
- new 對象太多
- 做業務計算時使用的對象太多 select * from table;
棧 程序所要求的棧深度過大
- 遞歸導致
- 死循環