JAVA自帶監控工具的介紹

 

轉:http://www.alidw.com/?p=326java


相信部分同窗可能仍是不太瞭解或者不多使用,這些監控工具是jdk5.0以上纔會有的,有部分是liunx特有的。
瞭解這些工具再作壓力測試和調優方面是特別有用的哦!用好下面介紹的工具徹底能夠不用jprofile了(它仍是收費的),並且速度還快。
下面我就給你們介紹介紹吧。
JPS
用來顯示本地的java進程,以及進程號。
咱們能夠經過它來查看咱們到底啓動了幾個java進程.
jps
jps也能夠列出遠程服務器的java進程(遠程服務需提供jstatd服務,採用rmi協議,默認連接端口1099),一般沒人這麼幹。

JINFO
能夠輸出並修改運行時的java進程的參數(JVM參數和Java System屬性)
jinfo  pid 會打印出詳細的jvm運行參數和Java System屬性。
此命令內容較多。並且輸出的也稍慢。不過能夠用如下命令來打印出你所關注的參數。
jinfo -flag MaxPermSize pid 該命令查看某個進程的MaxPermSize(MaxPermSize 能夠換任意JVM參數 好比PermSize)
 
[xxxxxxxx ~]$ jinfo -flag MaxPermSize 12191
-XX:MaxPermSize=134217728
 
[xxxxxxxx ~]$ jinfo -flag PermSize 12191
-XX:PermSize=134217728
 
[admin@dw_web4 ~]$ jinfo -flag LargePageSizeInBytes 12191
-XX:LargePageSizeInBytes=134217728
 
用main jinfo查看更多使用方法介紹

JSTAT(我最喜歡用的) 另外還有一個jvmstat 可視化的,我這裏不作介紹 有興趣http://java.sun.com/performance/jvmstat/ 下載玩玩
監視VM的內存工具,能夠用來監視vm內存內的各類堆和非堆的大小及其內存使用量,能夠觀察到classloader,compiler,gc相關信息。(興趣高起來了 等下我在提供一個JVM內存管理的圖,上一篇博客關於java內存模型主要是介紹寫併發程序)
jstat -gcutil:統計gc時,heap狀況
[xxxxxxxx ~]$ jstat -gcutil 12191 250 7
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT  
100.00   0.00  70.12   2.33  43.55      4    0.243     0    0.000    0.243
100.00   0.00  70.12   2.33  43.55      4    0.243     0    0.000    0.243
100.00   0.00  70.79   2.33  43.56      4    0.243     0    0.000    0.243
100.00   0.00  75.15   2.33  43.79      4    0.243     0    0.000    0.243
100.00   0.00  75.15   2.33  43.79      4    0.243     0    0.000    0.243
100.00   0.00  81.15   2.33  44.51      4    0.243     0    0.000    0.243
100.00   0.00  83.15   2.33  44.63      4    0.243     0    0.000    0.243
間隔250毫秒 打印7行
 
jstat -gccause 監控內存使用狀況 參數 (查看內存溢出相對有用)
[xxxxxxxx ~]$ jstat -gccause 12191 3000 (每隔3秒監控一次)
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                
100.00   0.00  93.97   2.35  44.71      4    0.259     0    0.000    0.259 unknown GCCause      No GC              
100.00   0.00  93.97   2.35  44.72      4    0.259     0    0.000    0.259 unknown GCCause      No GC
….
….
下面copy一份介紹:
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.
P Permanent space utilization as a percentage of the space’s current capacity.
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.
LGCC  Cause of last Garbage Collection.
GCC Cause of current Garbage Collection.
 
jstat -class pid:顯示加載class的數量,及所佔空間等信息。
 
[xxxxxxxx web-deploy]$ jstat -class 12191
Loaded  Bytes  Unloaded  Bytes     Time  
  8209 17577.9        0     0.0       1.68
 
jstat -compiler pid:顯示VM實時編譯的數量等信息。
[xxxxxxxx web-deploy]$ jstat -compiler 12191
Compiled Failed Invalid   Time   FailedType FailedMethod
    1029      0       0     9.42          0
 
jstat -gc pid:能夠顯示gc的信息,查看gc的次數,及時間。其中最後五項,分別是young gc的次數,young gc的時間,full gc的次數,full gc的時間,gc的總時間。
[xxxxxxxx ~]$ jstat -gc 12191
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT  
26176.0 26176.0  0.0   26176.0 209792.0 75798.8  1835008.0   72266.8   131072.0 60144.0      5    0.338   0      0.000    0.338
jstat -gccapacity:能夠顯示,VM內存中三代(包括新生區,老年區,permanent區)對象的使用和佔用大小,如:PGCMN顯示的是最小 perm的內存使用量,PGCMX顯示的是perm的內存最大使用量,PGC是當前新生成的perm內存佔用量,PC是但前perm內存佔用量。其餘的可 以根據這個類推, OC是old內純的佔用量。
[xxxxxxxx ~]$ jstat -gccapacity 12191
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC      PGCMN    PGCMX     PGC       PC     YGC    FGC
262144.0 262144.0 262144.0 26176.0 26176.0 209792.0  1835008.0  1835008.0  1835008.0  1835008.0 131072.0 131072.0 131072.0 131072.0      5     0
jstat -gcnew pid:統計gc時,new新生代的狀況
[xxxxxxxx ~]$ jstat -gcnew 12191
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT 
26176.0 26176.0    0.0 26176.0  1   4 13088.0 209792.0  76512.8      5    0.338
jstat -gcnewcapacity pid:new對象的信息及其佔用量。
 
jstat -gcold pid:old對象的信息。
 
jstat -gcoldcapacity pid:old對象的信息及其佔用量。
 
jstat -gcpermcapacity pid: perm對象的信息及其佔用量。
 
jstat -printcompilation pid:當前VM執行的信息。除了以上一個參數外,還能夠同時加上 兩個數字,如:jstat -printcompilation 3024 250 6是每250毫秒打印一次,一共打印6次,還能夠加上-h3每三行顯示一下標題。
 
man jstat查看更詳細的吧

JSTACK
能夠觀察到jvm中當前全部線程的運行狀況和線程當前狀態
jstack pid 運行看看,內容不少只列出部分
[xxxxxxxx ~]$ jstack 12191
2009-12-24 18:45:19
Full thread dump Java HotSpot(TM) 64-Bit Server VM (11.0-b16 mixed mode):
 
「Attach Listener」 daemon prio=10 tid=0×0000000044a11c00 nid=0×4430 waiting on condition [0x0000000000000000..0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
 
「http-0.0.0.0-7001-2″ daemon prio=10 tid=0×000000004546b000 nid=0×2fe5 in Object.wait() [0x00000000423af000..0x00000000423afc90]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        – waiting on <0×00002aaabc8700d8> (a org.apache.tomcat.util.net.MasterSlaveWorkerThread)
        at java.lang.Object.wait(Object.java:485)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.await(MasterSlaveWorkerThread.java:81)
        – locked <0×00002aaabc8700d8> (a org.apache.tomcat.util.net.MasterSlaveWorkerThread)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:107)
        at java.lang.Thread.run(Thread.java:619)
 
「http-0.0.0.0-7001-1″ daemon prio=10 tid=0×0000000045469c00 nid=0×2fe4 in Object.wait() [0x000000004236e000..0x000000004236ec10]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        – waiting on <0×00002aaabcaef5d0> (a org.apache.tomcat.util.net.MasterSlaveWorkerThread)
        at java.lang.Object.wait(Object.java:485)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.await(MasterSlaveWorkerThread.java:81)
        – locked <0×00002aaabcaef5d0> (a org.apache.tomcat.util.net.MasterSlaveWorkerThread)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:107)
        at java.lang.Thread.run(Thread.java:619)
 
「TP-Monitor」 daemon prio=10 tid=0×00002aab406b0000 nid=0×2fe3 in Object.wait() [0x000000004232d000..0x000000004232db90]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable.run(ThreadPool.java:559)
        – locked <0×00002aaabcd6ea98> (a org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable)
        at java.lang.Thread.run(Thread.java:619)
 
「TP-Processor4″ daemon prio=10 tid=0×00002aab414f0800 nid=0×2fe2 runnable [0x00000000422ec000..0x00000000422ecb10]
   java.lang.Thread.State: RUNNABLE
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384)
        – locked <0×00002aaabcfedfd8> (a java.net.SocksSocketImpl)
        at java.net.ServerSocket.implAccept(ServerSocket.java:453)
        at java.net.ServerSocket.accept(ServerSocket.java:421)
        at org.apache.jk.common.ChannelSocket.accept(ChannelSocket.java:306)
        at org.apache.jk.common.ChannelSocket.acceptConnections(ChannelSocket.java:660)
        at org.apache.jk.common.ChannelSocket$SocketAcceptor.runIt(ChannelSocket.java:870)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:619)
………………………………………….
……………………………………………
等等。。。

JMAP
打印出某個java進程內存內全部對象的狀況(產生的對象以及數量)
jmap -histo pid 打印jvm heap的直方圖。其輸出信息包括類名,對象數量,對象佔用大小。 內容會比較多 不方便查看能夠將其保存到文本中去。(jmap -histo pid>histo.log)
該命令檢查內存泄漏 是頗有用哦。
[xxxxxxxx ~]$ more histo.log
 
 num     #instances         #bytes  class name
———————————————-
   1:        251510       39395232  [C
   2:         53304       37733944  [I
   3:         90608       12763752  <constMethodKlass>
   4:         90608       10883664  <methodKlass>
   5:        256090       10243600  java.lang.String
   6:          8209        9233040  <constantPoolKlass>
   7:         74133        9109848  [B
   8:        129787        6779512  <symbolKlass>
   9:          8209        6552912  <instanceKlassKlass>
  10:        111943        6268808  java.util.HashMap$ValueIterator
  11:         94562        6051968  java.util.TreeMap$Entry
  12:          7083        5493440  <constantPoolCacheKlass>
  13:        109800        5270400  org.apache.catalina.LifecycleEvent
  14:         28087        5116800  [Ljava.lang.Object;
  15:         34532        4681168  [Ljava.util.HashMap$Entry;
  16:         27458        4173616  java.lang.reflect.Method
  17:        109800        3498960  [Lorg.apache.catalina.Container;
  18:         66949        3213552  java.util.HashMap$Entry
  19:        109970        2771112  [Lorg.apache.catalina.LifecycleListener;
  20:         32831        2101184  java.util.HashMap
  21:         34327        1828320  [Ljava.lang.String;
  22:         21356        1708480  java.util.TreeMap
  23:          8785        1616440  java.lang.Class
..................
..................
..................
jmap -dump:format=b,file=heap.log 12191能夠將12191進程的內存heap輸出出來到heap.log 文件裏。
jmap -heap pid(該命令我用的比較多) 打印出heap狀況,能夠觀察到New Generation(Eden Space,From Space,To Space),tenured generation,Perm Generation的內存使用狀況。
[xxxxxxxx ~]$ jmap -heap 12191
Attaching to process ID 12191, please wait…
Debugger attached successfully.
Server compiler detected.
JVM version is 11.0-b16
 
using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC
 
Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 2147483648 (2048.0MB)
   NewSize          = 268435456 (256.0MB)
   MaxNewSize       = 268435456 (256.0MB)
   OldSize          = 805306368 (768.0MB)
   NewRatio         = 7
   SurvivorRatio    = 8
   PermSize         = 134217728 (128.0MB)
   MaxPermSize      = 134217728 (128.0MB)
 
Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 241631232 (230.4375MB)
   used     = 94646792 (90.26221466064453MB)
   free     = 146984440 (140.17528533935547MB)
   39.16993313182296% used
Eden Space:
   capacity = 214827008 (204.875MB)
   used     = 67842568 (64.69971466064453MB)
   free     = 146984440 (140.17528533935547MB)
   31.580092573835035% used
From Space:
   capacity = 26804224 (25.5625MB)
   used     = 26804224 (25.5625MB)
   free     = 0 (0.0MB)
   100.0% used
To Space:
   capacity = 26804224 (25.5625MB)
   used     = 0 (0.0MB)
   free     = 26804224 (25.5625MB)
   0.0% used
concurrent mark-sweep generation:
   capacity = 1879048192 (1792.0MB)
   used     = 74001208 (70.57305145263672MB)
   free     = 1805046984 (1721.4269485473633MB)
   3.9382283176694597% used
Perm Generation:
   capacity = 134217728 (128.0MB)
   used     = 61586560 (58.7335205078125MB)
   free     = 72631168 (69.2664794921875MB)
   45.885562896728516% used
 
jmap -permstat pid 打印permanent generation heap狀況
 
更多信息查看man jmap
   OPTIONS
          <no option>
             When  no  option  is used jmap prints shared object mappings. For each shared object loaded in the target
             VM, start address, the size of the mapping, and the full path of the shared object file are printed. This
             is similar to the Solaris pmap utility.
 
          -dump:[live,]format=b,file=<filename>
             Dumps  the  Java  heap  in hprof binary format to filename. The live suboption is optional. If specified,
             only the live objects in the heap are dumped. To browse the heap dump, you can  use  jhat(1)  (Java  Heap
             Analysis Tool) to read the generated file.
 
          -finalizerinfo
             Prints information on objects awaiting finalization.
 
          -heap
             Prints  a heap summary. GC algorithm used, heap configuration and generation wise heap usage are printed.
 
          -histo[:live]
             Prints a histogram of the heap. For each Java class, number of objects, memory size in bytes,  and  fully
             qualified  class names are printed. VM internal class names are printed with ‘*’ prefix. If the live sub-
             option is specified, only live objects are counted.
 
          -permstat
             Prints class loader wise statistics of permanent generation of Java heap.  For  each  class  loader,  its
             name,  liveness,  address,  parent  class  loader,  and  the number and size of classes it has loaded are
             printed. In addition, the number and size of interned Strings are printed.
 
          -F Force. Use with jmap -dump or jmap -histo option if the pid does not respond. The live suboption  is  not
             supported in this mode.
 
          -h Prints a help message.
 
          -help
             Prints a help message.
 
          -J<flag>
             Passes <flag> to the Java virtual machine on which jmap is run.

JCONSOLE
一個java GUI監視工具,能夠以圖表化的形式顯示各類數據,並可經過遠程鏈接監視遠程的服務器VM。
我的喜歡稍微簡單易用jstat來查看。
相關文章
相關標籤/搜索