jdk給我們提供了很多的工具,讓我們檢測運行中的程序,幫助我們更好的優(yōu)化程序和發(fā)現(xiàn)問題。在網(wǎng)站找到了java命令學(xué)習系列這篇文章,很受啟發(fā),在此做一個信息的摘錄,并加上自己的注釋和理解。
- jps
jdk中的jps命令可以顯示當前運行的java進程以及相關(guān)參數(shù),它的實現(xiàn)機制如下:java程序在啟動以后,會在java.io.tmpdir指定的目錄下,就是臨時文件夾里,生成一個類似于hsperfdata_User的文件夾,這個文件夾里(在Linux中為/tmp/hsperfdata_{userName}/),有幾個文件,名字就是java進程的pid,因此列出當前運行的java進程,只是把這個目錄里的文件名列一下而已。 至于系統(tǒng)的參數(shù)什么,就可以解析這幾個文件獲得。
? ~ jps -help
usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]
Definitions:
<hostid>: <hostname>[:<port>]
利用man命令查看一下可以跟的參數(shù)
OPTIONS
The jps command supports a number of options that modify the output of
the command. These options are subject to change or removal in the
future.
-q
Suppresses the output of the class name, JAR file name, and
arguments passed to the main method, producing only a list of
local JVM identifiers.
-m
Displays the arguments passed to the main method. The output may
be null for embedded JVMs.
-l
Displays the full package name for the application's main class
or the full path name to the application's JAR file.
-v
Displays the arguments passed to the JVM.
-V
Suppresses the output of the class name, JAR file name, and
arguments passed to the main method, producing only a list of
local JVM identifiers.
-Joption
Passes option to the JVM, where option is one of the options
described on the reference page for the Java application
launcher. For example, -J-Xms48m sets the startup memory to 48
MB. See java(1).
-q 只顯示pid,不顯示class名稱,jar文件名和傳遞給main 方法的參數(shù)
-m 輸出傳遞給main 方法的參數(shù)
-l 輸出應(yīng)用程序main class的完整package名 或者 應(yīng)用程序的jar文件完整路徑名
-v 輸出傳遞給JVM的參數(shù)
- jstack
jstack是java虛擬機自帶的一種堆棧跟蹤工具。jstack用于生成java虛擬機當前時刻的線程快照。線程快照是當前java虛擬機內(nèi)每一條線程正在執(zhí)行的方法堆棧的集合,生成線程快照的主要目的是定位線程出現(xiàn)長時間停頓的原因,如線程間死鎖、死循環(huán)、請求外部資源導(dǎo)致的長時間等待等。 線程出現(xiàn)停頓的時候通過jstack來查看各個線程的調(diào)用堆棧,就可以知道沒有響應(yīng)的線程到底在后臺做什么事情,或者等待什么資源。 如果java程序崩潰生成core文件,jstack工具可以用來獲得core文件的java stack和native stack的信息,從而可以輕松地知道java程序是如何崩潰和在程序何處發(fā)生問題。另外,jstack工具還可以附屬到正在運行的java程序中,看到當時運行的java程序的java stack和native stack的信息, 如果現(xiàn)在運行的java程序呈現(xiàn)hung的狀態(tài),jstack是非常有用的。
線程狀態(tài):想要通過jstack命令來分析線程的情況的話,首先要知道線程都有哪些狀態(tài),下面這些狀態(tài)是我們使用jstack命令查看線程堆棧信息時可能會看到的線程的幾種狀態(tài):
NEW,未啟動的。不會出現(xiàn)在Dump中。
RUNNABLE,在虛擬機內(nèi)執(zhí)行的。
BLOCKED,受阻塞并等待監(jiān)視器鎖。
WATING,無限期等待另一個線程執(zhí)行特定操作。
TIMED_WATING,有時限的等待另一個線程的特定操作。
TERMINATED,已退出的。
Monitor
在多線程的 JAVA程序中,實現(xiàn)線程之間的同步,就要說說 Monitor。 Monitor是 Java中用以實現(xiàn)線程之間的互斥與協(xié)作的主要手段,它可以看成是對象或者 Class的鎖。每一個對象都有,也僅有一個 monitor。下 面這個圖,描述了線程和 Monitor之間關(guān)系,以 及線程的狀態(tài)轉(zhuǎn)換圖:

解釋說:
進入?yún)^(qū)(Entrt Set):表示線程通過synchronized要求獲取對象的鎖。如果對象未被鎖住,則迚入擁有者;否則則在進入?yún)^(qū)等待。一旦對象鎖被其他線程釋放,立即參與競爭。
擁有者(The Owner):表示某一線程成功競爭到對象鎖。
等待區(qū)(Wait Set):表示線程通過對象的wait方法,釋放對象的鎖,并在等待區(qū)等待被喚醒。
從圖中可以看出,一個 Monitor在某個時刻,只能被一個線程擁有,該線程就是 “Active Thread”,而其它線程都是 “Waiting Thread”,分別在兩個隊列 “ Entry Set”和 “Wait Set”里面等候。在 “Entry Set”中等待的線程狀態(tài)是 “Waiting for monitor entry”,而在 “Wait Set”中等待的線程狀態(tài)是 “in Object.wait()”。 先看 “Entry Set”里面的線程。我們稱被 synchronized保護起來的代碼段為臨界區(qū)。當一個線程申請進入臨界區(qū)時,它就進入了 “Entry Set”隊列。
線程狀態(tài)產(chǎn)生的原因
runnable:狀態(tài)一般為RUNNABLE。
in Object.wait():等待區(qū)等待,狀態(tài)為WAITING或TIMED_WAITING。
waiting for monitor entry:進入?yún)^(qū)等待,狀態(tài)為BLOCKED。
waiting on condition:等待區(qū)等待、被park。
sleeping:休眠的線程,調(diào)用了Thread.sleep()。
看一下命令吧:
? ~ jstack
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
-F 當’jstack [-l] pid’沒有相應(yīng)的時候強制打印棧信息
-l 長列表. 打印關(guān)于鎖的附加信息,例如屬于java.util.concurrent的ownable synchronizers列表.
-m 打印java和native c/c++框架的所有棧信息.
-h 打印幫助信息
pid 需要被打印配置信息的java進程id,可以用jps查詢.
- jstat
jstat(JVM Statistics Monitoring Tool)是用于監(jiān)控虛擬機各種運行狀態(tài)信息的命令行工具。他可以顯示本地或遠程虛擬機進程中的類裝載、內(nèi)存、垃圾收集、JIT編譯等運行數(shù)據(jù),在沒有GUI圖形的服務(wù)器上,它是運行期定位虛擬機性能問題的首選工具。
? ~ jstat -help
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.
option — 選項,下面會介紹可以跟什么選項
vmid — VM的進程號,即當前運行的java進程號
interval– 間隔時間,單位為秒或者毫秒
count — 打印次數(shù),如果缺省則打印無數(shù)次
對于命令格式中的VMID與LVMID需要特別說明下:如果是本地虛擬機進程,VMID(Virtual Machine IDentifier,虛機標識符)和LVMID(Local Virtual Machine IDentifier,虛機標識符)是一致的,如果是遠程虛擬機進程,那VMID的格式應(yīng)當是:[protocol:][//] lvmid [@hostname[:port]/servername]
利用man命令查看一下具體的參數(shù)
OPTIONS
The jstat command supports two types of options, general options and
output options. General options cause the jstat command to display
simple usage and version information. Output options determine the
content and format of the statistical output.
All options and their functionality are subject to change or removal in
future releases.
General Options
If you specify one of the general options, then you cannot specify any
other option or parameter.
-help
Displays a help message.
-options
Displays a list of static options. See Output Options.
Output Options
If you do not specify a general option, then you can specify output
options. Output options determine the content and format of the jstat
command's output, and consist of a single statOption, plus any of the
other output options (-h, -t, and -J). The statOption must come first.
Output is formatted as a table, with columns that are separated by
spaces. A header row with titles describes the columns. Use the -h
option to set the frequency at which the header is displayed. Column
header names are consistent among the different options. In general, if
two options provide a column with the same name, then the data source
for the two columns is the same.
Use the -t option to display a time stamp column, labeled Timestamp as
the first column of output. The Timestamp column contains the elapsed
time, in seconds, since the target JVM started. The resolution of the
time stamp is dependent on various factors and is subject to variation
due to delayed thread scheduling on heavily loaded systems.
Use the interval and count parameters to determine how frequently and
how many times, respectively, the jstat command displays its output.
Note: Do not to write scripts to parse the jstat command's output
because the format might change in future releases. If you write
scripts that parse jstat command output, then expect to modify them for
future releases of this tool.
-statOption
Determines the statistics information the jstat command displays.
The following lists the available options. Use the -options general
option to display the list of options for a particular platform
installation. See Stat Options and Output.
class: Displays statistics about the behavior of the class loader.
compiler: Displays statistics about the behavior of the Java
HotSpot VM Just-in-Time compiler.
gc: Displays statistics about the behavior of the garbage collected
heap.
gccapacity: Displays statistics about the capacities of the
generations and their corresponding spaces.
gccause: Displays a summary about garbage collection statistics
(same as -gcutil), with the cause of the last and current (when
applicable) garbage collection events.
gcnew: Displays statistics of the behavior of the new generation.
gcnewcapacity: Displays statistics about the sizes of the new
generations and its corresponding spaces.
gcold: Displays statistics about the behavior of the old generation
and metaspace statistics.
gcoldcapacity: Displays statistics about the sizes of the old
generation.
gcmetacapacity: Displays statistics about the sizes of the
metaspace.
gcutil: Displays a summary about garbage collection statistics.
printcompilation: Displays Java HotSpot VM compilation method
statistics.
-h n
Displays a column header every n samples (output rows), where n is
a positive integer. Default value is 0, which displays the column
header the first row of data.
-t
Displays a timestamp column as the first column of output. The time
stamp is the time since the start time of the target JVM.
-JjavaOption
Passes javaOption to the Java application launcher. For example,
-J-Xms48m sets the startup memory to 48 MB. For a complete list of
options, see java(1).
Stat Options and Output
The following information summarizes the columns that the jstat command
outputs for each statOption.
-class option
Class loader statistics.
Loaded: Number of classes loaded.
Bytes: Number of kBs loaded.
Unloaded: Number of classes unloaded.
Bytes: Number of Kbytes unloaded.
Time: Time spent performing class loading and unloading operations.
-compiler option
Java HotSpot VM Just-in-Time compiler statistics.
Compiled: Number of compilation tasks performed.
Failed: Number of compilations tasks failed.
Invalid: Number of compilation tasks that were invalidated.
Time: Time spent performing compilation tasks.
FailedType: Compile type of the last failed compilation.
FailedMethod: Class name and method of the last failed compilation.
-gc option
Garbage-collected heap statistics.
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
S0U: Survivor space 0 utilization (kB).
S1U: Survivor space 1 utilization (kB).
EC: Current eden space capacity (kB).
EU: Eden space utilization (kB).
OC: Current old space capacity (kB).
OU: Old space utilization (kB).
MC: Metaspace capacity (kB).
MU: Metacspace utilization (kB).
CCSC: Compressed class space capacity (kB).
CCSU: Compressed class space used (kB).
YGC: Number of young generation garbage collection events.
YGCT: Young generation garbage collection time.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.
-gccapacity option
Memory pool generation and space capacities.
NGCMN: Minimum new generation capacity (kB).
NGCMX: Maximum new generation capacity (kB).
NGC: Current new generation capacity (kB).
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
EC: Current eden space capacity (kB).
OGCMN: Minimum old generation capacity (kB).
OGCMX: Maximum old generation capacity (kB).
OGC: Current old generation capacity (kB).
OC: Current old space capacity (kB).
MCMN: Minimum metaspace capacity (kB).
MCMX: Maximum metaspace capacity (kB).
MC: Metaspace capacity (kB).
CCSMN: Compressed class space minimum capacity (kB).
CCSMX: Compressed class space maximum capacity (kB).
CCSC: Compressed class space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
-gccause option
This option displays the same summary of garbage collection
statistics as the -gcutil option, but includes the causes of the
last garbage collection event and (when applicable) the current
garbage collection event. In addition to the columns listed for
-gcutil, this option adds the following columns.
LGCC: Cause of last garbage collection
GCC: Cause of current garbage collection
-gcnew option
New generation statistics.
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
S0U: Survivor space 0 utilization (kB).
S1U: Survivor space 1 utilization (kB).
TT: Tenuring threshold.
MTT: Maximum tenuring threshold.
DSS: Desired survivor size (kB).
EC: Current eden space capacity (kB).
EU: Eden space utilization (kB).
YGC: Number of young generation GC events.
YGCT: Young generation garbage collection time.
-gcnewcapacity option
New generation space size statistics.
NGCMN: Minimum new generation capacity (kB).
NGCMX: Maximum new generation capacity (kB).
NGC: Current new generation capacity (kB).
S0CMX: Maximum survivor space 0 capacity (kB).
S0C: Current survivor space 0 capacity (kB).
S1CMX: Maximum survivor space 1 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
ECMX: Maximum eden space capacity (kB).
EC: Current eden space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
-gcold option
Old generation and metaspace behavior statistics.
MC: Metaspace capacity (kB).
MU: Metaspace utilization (kB).
CCSC: Compressed class space capacity (kB).
CCSU: Compressed class space used (kB).
OC: Current old space capacity (kB).
OU: Old space utilization (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.
-gcoldcapacity option
Old generation size statistics.
OGCMN: Minimum old generation capacity (kB).
OGCMX: Maximum old generation capacity (kB).
OGC: Current old generation capacity (kB).
OC: Current old space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.
-gcmetacapacity option
Metaspace size statistics.
MCMN: Minimum metaspace capacity (kB).
MCMX: Maximum metaspace capacity (kB).
MC: Metaspace capacity (kB).
CCSMN: Compressed class space minimum capacity (kB).
CCSMX: Compressed class space maximum capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.
-gcutil option
Summary of garbage collection statistics.
S0: Survivor space 0 utilization as a percentage of the space's
current capacity.
S1: Survivor space 1 utilization as a percentage of the space's
current capacity.
E: Eden space utilization as a percentage of the space's current
capacity.
O: Old space utilization as a percentage of the space's current
capacity.
M: Metaspace utilization as a percentage of the space's current
capacity.
CCS: Compressed class space utilization as a percentage.
YGC: Number of young generation GC events.
YGCT: Young generation garbage collection time.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.
-printcompilation option
Java HotSpot VM compiler method statistics.
Compiled: Number of compilation tasks performed by the most
recently compiled method.
Size: Number of bytes of byte code of the most recently compiled
method.
Type: Compilation type of the most recently compiled method.
Method: Class name and method name identifying the most recently
compiled method. Class name uses slash (/) instead of dot (.) as a
name space separator. Method name is the method within the
specified class. The format for these two fields is consistent with
the HotSpot -XX:+PrintCompilation option.
option
選項option代表這用戶希望查詢的虛擬機信息,主要分為3類:類裝載、垃圾收集和運行期編譯狀況,具體選項及作用如下:
–class 監(jiān)視類裝載、卸載數(shù)量、總空間及類裝載所耗費的時間
–gc 監(jiān)視Java堆狀況,包括Eden區(qū)、2個Survivor區(qū)、老年代、永久代等的容量
–gccapacity 監(jiān)視內(nèi)容與-gc基本相同,但輸出主要關(guān)注Java堆各個區(qū)域使用到的最大和最小空間
–gcutil 監(jiān)視內(nèi)容與-gc基本相同,但輸出主要關(guān)注已使用空間占總空間的百分比
–gccause 與-gcutil功能一樣,但是會額外輸出導(dǎo)致上一次GC產(chǎn)生的原因 –gcnew 監(jiān)視新生代GC的狀況
–gcnewcapacity 監(jiān)視內(nèi)容與-gcnew基本相同,輸出主要關(guān)注使用到的最大和最小空間
–gcold 監(jiān)視老年代GC的狀況
–gcoldcapacity 監(jiān)視內(nèi)容與——gcold基本相同,輸出主要關(guān)注使用到的最大和最小空間
–gcpermcapacity 輸出永久代使用到的最大和最小空間
–compiler 輸出JIT編譯器編譯過的方法、耗時等信息
–printcompilation 輸出已經(jīng)被JIT編譯的方法
常見術(shù)語
1、jstat –class<pid> : 顯示加載class的數(shù)量,及所占空間等信息。
Loaded 裝載的類的數(shù)量
Bytes 裝載類所占用的字節(jié)數(shù)
Unloaded 卸載類的數(shù)量
Bytes 卸載類的字節(jié)數(shù)
Time 裝載和卸載類所花費的時間
2、jstat -compiler <pid>顯示VM實時編譯的數(shù)量等信息。
Compiled 編譯任務(wù)執(zhí)行數(shù)量
Failed 編譯任務(wù)執(zhí)行失敗數(shù)量
Invalid 編譯任務(wù)執(zhí)行失效數(shù)量
Time 編譯任務(wù)消耗時間
FailedType 最后一個編譯失敗任務(wù)的類型
FailedMethod 最后一個編譯失敗任務(wù)所在的類及方法
3、jstat -gc <pid>: 可以顯示gc的信息,查看gc的次數(shù),及時間。
S0C 年輕代中第一個survivor(幸存區(qū))的容量 (字節(jié))
S1C 年輕代中第二個survivor(幸存區(qū))的容量 (字節(jié))
S0U 年輕代中第一個survivor(幸存區(qū))目前已使用空間 (字節(jié))
S1U 年輕代中第二個survivor(幸存區(qū))目前已使用空間 (字節(jié))
EC 年輕代中Eden(伊甸園)的容量 (字節(jié))
EU 年輕代中Eden(伊甸園)目前已使用空間 (字節(jié))
OC Old代的容量 (字節(jié))
OU Old代目前已使用空間 (字節(jié))
PC Perm(持久代)的容量 (字節(jié))
PU Perm(持久代)目前已使用空間 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
YGCT 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c所用時間(s)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
FGCT 從應(yīng)用程序啟動到采樣時old代(全gc)gc所用時間(s)
GCT 從應(yīng)用程序啟動到采樣時gc用的總時間(s)
4、jstat -gccapacity <pid>:可以顯示,VM內(nèi)存中三代(young,old,perm)對象的使用和占用大小
NGCMN 年輕代(young)中初始化(最小)的大小(字節(jié))
NGCMX 年輕代(young)的最大容量 (字節(jié))
NGC 年輕代(young)中當前的容量 (字節(jié))
S0C 年輕代中第一個survivor(幸存區(qū))的容量 (字節(jié))
S1C 年輕代中第二個survivor(幸存區(qū))的容量 (字節(jié))
EC 年輕代中Eden(伊甸園)的容量 (字節(jié))
OGCMN old代中初始化(最小)的大小 (字節(jié))
OGCMX old代的最大容量(字節(jié))
OGC old代當前新生成的容量 (字節(jié))
OC Old代的容量 (字節(jié))
PGCMN perm代中初始化(最小)的大小 (字節(jié))
PGCMX perm代的最大容量 (字節(jié))
PGC perm代當前新生成的容量 (字節(jié))
PC Perm(持久代)的容量 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
5、jstat -gcutil <pid>:統(tǒng)計gc信息
S0 年輕代中第一個survivor(幸存區(qū))已使用的占當前容量百分比 S1 年輕代中第二個survivor(幸存區(qū))已使用的占當前容量百分比
E 年輕代中Eden(伊甸園)已使用的占當前容量百分比
O old代已使用的占當前容量百分比
P perm代已使用的占當前容量百分比
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
YGCT 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c所用時間(s)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
FGCT 從應(yīng)用程序啟動到采樣時old代(全gc)gc所用時間(s)
GCT 從應(yīng)用程序啟動到采樣時gc用的總時間(s)
6、jstat -gcnew <pid>:年輕代對象的信息。
S0C 年輕代中第一個survivor(幸存區(qū))的容量 (字節(jié))
S1C 年輕代中第二個survivor(幸存區(qū))的容量 (字節(jié))
S0U 年輕代中第一個survivor(幸存區(qū))目前已使用空間 (字節(jié))
S1U 年輕代中第二個survivor(幸存區(qū))目前已使用空間 (字節(jié))
TT 持有次數(shù)限制
MTT 最大持有次數(shù)限制
EC 年輕代中Eden(伊甸園)的容量 (字節(jié))
EU 年輕代中Eden(伊甸園)目前已使用空間 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
YGCT 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c所用時間(s)
7、jstat -gcnewcapacity<pid>: 年輕代對象的信息及其占用量。
NGCMN 年輕代(young)中初始化(最小)的大小(字節(jié))
NGCMX 年輕代(young)的最大容量 (字節(jié))
NGC 年輕代(young)中當前的容量 (字節(jié))
S0CMX 年輕代中第一個survivor(幸存區(qū))的最大容量 (字節(jié))
S0C 年輕代中第一個survivor(幸存區(qū))的容量 (字節(jié))
S1CMX 年輕代中第二個survivor(幸存區(qū))的最大容量 (字節(jié))
S1C 年輕代中第二個survivor(幸存區(qū))的容量 (字節(jié))
ECMX 年輕代中Eden(伊甸園)的最大容量 (字節(jié))
EC 年輕代中Eden(伊甸園)的容量 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
8、jstat -gcold <pid>:old代對象的信息。
PC Perm(持久代)的容量 (字節(jié))
PU Perm(持久代)目前已使用空間 (字節(jié))
OC Old代的容量 (字節(jié))
OU Old代目前已使用空間 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
FGCT 從應(yīng)用程序啟動到采樣時old代(全gc)gc所用時間(s)
GCT 從應(yīng)用程序啟動到采樣時gc用的總時間(s)
9、stat -gcoldcapacity <pid>: old代對象的信息及其占用量。
OGCMN old代中初始化(最小)的大小 (字節(jié))
OGCMX old代的最大容量(字節(jié))
OGC old代當前新生成的容量 (字節(jié))
OC Old代的容量 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
FGCT 從應(yīng)用程序啟動到采樣時old代(全gc)gc所用時間(s)
GCT 從應(yīng)用程序啟動到采樣時gc用的總時間(s)
10、jstat -gcpermcapacity<pid>: perm對象的信息及其占用量。
PGCMN perm代中初始化(最小)的大小 (字節(jié))
PGCMX perm代的最大容量 (字節(jié))
PGC perm代當前新生成的容量 (字節(jié))
PC Perm(持久代)的容量 (字節(jié))
YGC 從應(yīng)用程序啟動到采樣時年輕代中g(shù)c次數(shù)
FGC 從應(yīng)用程序啟動到采樣時old代(全gc)gc次數(shù)
FGCT 從應(yīng)用程序啟動到采樣時old代(全gc)gc所用時間(s)
GCT 從應(yīng)用程序啟動到采樣時gc用的總時間(s)
11、jstat -printcompilation <pid>:當前VM執(zhí)行的信息。
Compiled 編譯任務(wù)的數(shù)目
Size 方法生成的字節(jié)碼的大小
Type 編譯類型
Method 類名和方法名用來標識編譯的方法。類名使用/做為一個命名空間分隔符。方法名是給定類中的方法。
上述格式是由-XX:+PrintComplation選項進行設(shè)置的
- jmap
jmap是JDK自帶的工具軟件,主要用于打印指定Java進程(或核心文件、遠程調(diào)試服務(wù)器)的共享對象內(nèi)存映射或堆內(nèi)存細節(jié)。堆Dump是反應(yīng)Java堆使用情況的內(nèi)存鏡像,其中主要包括系統(tǒng)信息、虛擬機屬性、完整的線程Dump、所有類和對象的狀態(tài)等。 一般,在內(nèi)存不足、GC異常等情況下,我們就會懷疑有內(nèi)存泄露。這個時候我們就可以制作堆Dump來查看具體情況.常見內(nèi)存錯誤:
outOfMemoryError 年老代內(nèi)存不足。
outOfMemoryError:PermGen Space 永久代內(nèi)存不足。
outOfMemoryError:GC overhead limit exceed 垃圾回收時間占用系統(tǒng)運行時間的98%或以上。
用法摘要
? ~ jmap
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
參數(shù)
option 選項參數(shù)是互斥的(不可同時使用)。想要使用選項參數(shù),直接跟在命令名稱后即可。
pid 需要打印配置信息的進程ID。該進程必須是一個Java進程。想要獲取運行的Java進程列表,你可以使用jps。
executable 產(chǎn)生核心dump的Java可執(zhí)行文件。
core 需要打印配置信息的核心文件。
remote-hostname-or-IP 遠程調(diào)試服務(wù)器的(請查看jsadebugd)主機名或IP地址。
server-id 可選的唯一id,如果相同的遠程主機上運行了多臺調(diào)試服務(wù)器,用此選項參數(shù)標識服務(wù)器。
選項解釋
<no option> 如果使用不帶選項參數(shù)的jmap打印共享對象映射,將會打印目標虛擬機中加載的每個共享對象的起始地址、映射大小以及共享對象文件的路徑全稱。這與Solaris的pmap工具比較相似。
-dump:[live,]format=b,file=<filename> 以hprof二進制格式轉(zhuǎn)儲Java堆到指定filename的文件中。live子選項是可選的。如果指定了live子選項,堆中只有活動的對象會被轉(zhuǎn)儲。想要瀏覽heap dump,你可以使用jhat(Java堆分析工具)讀取生成的文件。
-finalizerinfo 打印等待終結(jié)的對象信息。
-heap 打印一個堆的摘要信息,包括使用的GC算法、堆配置信息和generation wise heap usage。
-histo[:live] 打印堆的柱狀圖。其中包括每個Java類、對象數(shù)量、內(nèi)存大小(單位:字節(jié))、完全限定的類名。打印的虛擬機內(nèi)部的類名稱將會帶有一個’*’前綴。如果指定了live子選項,則只計算活動的對象。
-permstat 打印Java堆內(nèi)存的永久保存區(qū)域的類加載器的智能統(tǒng)計信息。對于每個類加載器而言,它的名稱、活躍度、地址、父類加載器、它所加載的類的數(shù)量和大小都會被打印。此外,包含的字符串數(shù)量和大小也會被打印。
-F 強制模式。如果指定的pid沒有響應(yīng),請使用jmap -dump或jmap -histo選項。此模式下,不支持live子選項。
-h 打印幫助信息。
-help 打印幫助信息。
-J<flag> 指定傳遞給運行jmap的JVM的參數(shù)。
查看java 堆(heap)使用情況,執(zhí)行命令:
jmap -heap 31846
Attaching to process ID 31846, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.71-b01
using thread-local object allocation.
Parallel GC with 4 thread(s)//GC 方式
Heap Configuration: //堆內(nèi)存初始化配置
MinHeapFreeRatio = 0 //對應(yīng)jvm啟動參數(shù)-XX:MinHeapFreeRatio設(shè)置JVM堆最小空閑比率(default 40)
MaxHeapFreeRatio = 100 //對應(yīng)jvm啟動參數(shù) -XX:MaxHeapFreeRatio設(shè)置JVM堆最大空閑比率(default 70)
MaxHeapSize = 2082471936 (1986.0MB) //對應(yīng)jvm啟動參數(shù)-XX:MaxHeapSize=設(shè)置JVM堆的最大大小
NewSize = 1310720 (1.25MB)//對應(yīng)jvm啟動參數(shù)-XX:NewSize=設(shè)置JVM堆的‘新生代’的默認大小
MaxNewSize = 17592186044415 MB//對應(yīng)jvm啟動參數(shù)-XX:MaxNewSize=設(shè)置JVM堆的‘新生代’的最大大小
OldSize = 5439488 (5.1875MB)//對應(yīng)jvm啟動參數(shù)-XX:OldSize=<value>:設(shè)置JVM堆的‘老生代’的大小
NewRatio = 2 //對應(yīng)jvm啟動參數(shù)-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
SurvivorRatio = 8 //對應(yīng)jvm啟動參數(shù)-XX:SurvivorRatio=設(shè)置年輕代中Eden區(qū)與Survivor區(qū)的大小比值
PermSize = 21757952 (20.75MB) //對應(yīng)jvm啟動參數(shù)-XX:PermSize=<value>:設(shè)置JVM堆的‘永生代’的初始大小
MaxPermSize = 85983232 (82.0MB)//對應(yīng)jvm啟動參數(shù)-XX:MaxPermSize=<value>:設(shè)置JVM堆的‘永生代’的最大大小
G1HeapRegionSize = 0 (0.0MB)
Heap Usage://堆內(nèi)存使用情況
PS Young Generation
Eden Space://Eden區(qū)內(nèi)存分布
capacity = 33030144 (31.5MB)//Eden區(qū)總?cè)萘? used = 1524040 (1.4534378051757812MB) //Eden區(qū)已使用
free = 31506104 (30.04656219482422MB) //Eden區(qū)剩余容量
4.614088270399305% used //Eden區(qū)使用比率
From Space: //其中一個Survivor區(qū)的內(nèi)存分布
capacity = 5242880 (5.0MB)
used = 0 (0.0MB)
free = 5242880 (5.0MB)
0.0% used
To Space: //另一個Survivor區(qū)的內(nèi)存分布
capacity = 5242880 (5.0MB)
used = 0 (0.0MB)
free = 5242880 (5.0MB)
0.0% used
PS Old Generation //當前的Old區(qū)內(nèi)存分布
capacity = 86507520 (82.5MB)
used = 0 (0.0MB)
free = 86507520 (82.5MB)
0.0% used
PS Perm Generation//當前的 “永生代” 內(nèi)存分布
capacity = 22020096 (21.0MB)
used = 2496528 (2.3808746337890625MB)
free = 19523568 (18.619125366210938MB)
11.337498256138392% used
670 interned Strings occupying 43720 bytes.
查看堆內(nèi)存(histogram)中的對象數(shù)量及大小,執(zhí)行命令:
jmap -histo 3331
num #instances #bytes class name
編號 個數(shù) 字節(jié) 類名
----------------------------------------------
1: 7 1322080 [I
2: 5603 722368 <methodKlass>
3: 5603 641944 <constMethodKlass>
4: 34022 544352 java.lang.Integer
5: 371 437208 <constantPoolKlass>
6: 336 270624 <constantPoolCacheKlass>
7: 371 253816 <instanceKlassKlass>
jmap -histo:live 這個命令執(zhí)行,JVM會先觸發(fā)gc,然后再統(tǒng)計信息。
將內(nèi)存使用的詳細情況輸出到文件,執(zhí)行命令:
jmap -dump:format=b,file=heapDump 6900
然后用jhat命令可以參看 jhat -port 5000 heapDump 在瀏覽器中訪問:http://localhost:5000/ 查看詳細信息,這個命令執(zhí)行,JVM會將整個heap的信息dump寫入到一個文件,heap如果比較大的話,就會導(dǎo)致這個過程比較耗時,并且執(zhí)行的過程中為了保證dump的信息是可靠的,所以會暫停應(yīng)用。
5.jhat
jhat(Java Heap Analysis Tool),是一個用來分析java的堆情況的命令。之前的文章講到過,使用可以生成Java堆的Dump文件。生成dump文件之后就可以用jhat命令,將dump文件轉(zhuǎn)成html的形式,然后通過http訪問可以查看堆情況。看一下命令
? ~ jhat -help
Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>
-J<flag> Pass <flag> directly to the runtime system. For
example, -J-mx512m to use a maximum heap size of 512MB
-stack false: Turn off tracking object allocation call stack.
-refs false: Turn off tracking of references to objects
-port <port>: Set the port for the HTTP server. Defaults to 7000
-exclude <file>: Specify a file that lists data members that should
be excluded from the reachableFrom query.
-baseline <file>: Specify a baseline object dump. Objects in
both heap dumps with the same ID and same class will
be marked as not being "new".
-debug <int>: Set debug level.
0: No debug output
1: Debug hprof file parsing
2: Debug hprof file parsing, no server
-version Report version number
-h|-help Print this help and exit
<file> The file to read
For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".
All boolean options default to "true"
說明:
-stack false|true
關(guān)閉對象分配調(diào)用棧跟蹤(tracking object allocation call stack)。 如果分配位置信息在堆轉(zhuǎn)儲中不可用. 則必須將此標志設(shè)置為 false. 默認值為 true.
-refs false|true
關(guān)閉對象引用跟蹤(tracking of references to objects)。 默認值為 true. 默認情況下, 返回的指針是指向其他特定對象的對象,如反向鏈接或輸入引用(referrers or incoming references), 會統(tǒng)計/計算堆中的所有對象。
-port port-number
設(shè)置 jhat HTTP server 的端口號. 默認值 7000.
-exclude exclude-file
指定對象查詢時需要排除的數(shù)據(jù)成員列表文件(a file that lists data members that should be excluded from the reachable objects query)。 例如, 如果文件列列出了 java.lang.String.value , 那么當從某個特定對象 Object o 計算可達的對象列表時, 引用路徑涉及 java.lang.String.value 的都會被排除。
-baseline exclude-file
指定一個基準堆轉(zhuǎn)儲(baseline heap dump)。 在兩個 heap dumps 中有相同 object ID 的對象會被標記為不是新的(marked as not being new). 其他對象被標記為新的(new). 在比較兩個不同的堆轉(zhuǎn)儲時很有用.
-debug int
設(shè)置 debug 級別. 0 表示不輸出調(diào)試信息。 值越大則表示輸出更詳細的 debug 信息.
-version
啟動后只顯示版本信息就退出
-J< flag >
因為 jhat 命令實際上會啟動一個JVM來執(zhí)行, 通過 -J 可以在啟動JVM時傳入一些啟動參數(shù). 例如, -J-Xmx512m 則指定運行 jhat 的Java虛擬機使用的最大堆內(nèi)存為 512 MB. 如果需要使用多個JVM啟動參數(shù),則傳入多個 -Jxxxxxx.
如何生成dump文件
1. jmap : jmap -dump:format=b,file=heapDump 62247
2. 使用 jconsole 選項通過 HotSpotDiagnosticMXBean 從運行時獲得堆轉(zhuǎn)儲(生成dump文件)
3. 虛擬機啟動時如果指定了 -XX:+HeapDumpOnOutOfMemoryError 選項, 則在拋出 OutOfMemoryError 時, 會自動執(zhí)行堆轉(zhuǎn)儲。
4. 使用 hprof 命令
針對生成的堆dump文件,除了jhat處理外,還可以通過eclipse的插件mat來操作。
本文更多的是摘錄,也是自己的記錄,共勉。