1. 程式人生 > 實用技巧 >【JVM】jmap命令詳解----檢視JVM記憶體使用詳情

【JVM】jmap命令詳解----檢視JVM記憶體使用詳情

linux獲取java程序PID:

https://www.cnblogs.com/sxdcgaq8080/p/10734752.html

如果命令使用過程中報錯,可能解決你問題的方案:

https://www.cnblogs.com/sxdcgaq8080/p/11089179.html

https://www.cnblogs.com/sxdcgaq8080/p/10675966.html

==========================================

1、jmap命令基本概述

  jmap命令是一個可以輸出所有記憶體中物件的工具,甚至可以將VM 中的heap,以二進位制輸出成文字。

  打印出某個java程序(使用pid)記憶體內的,所有‘物件’的情況(如:產生那些物件,及其數量)。

64位機上使用需要使用如下方式:

jmap -J-d64 -heap pid

2、命令格式

    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) 連線到遠端除錯服務

3.引數說明

1) options:

>    pid:    目標程序的PID,程序編號,可以採用ps -ef | grep java 檢視java程序的PID;
>    executable:     產生core dump的java可執行程式;
>    core:     將被列印資訊的core dump檔案;
>    remote-hostname-or-IP:     遠端debug服務的主機名或ip;
>    server-id:     唯一id,假如一臺主機上多個遠端debug服務;

2)基本引數:

[就是替換[option]位置的引數]

1> -dump:[live,]format=b,file=<filename>使用hprof二進位制形式,輸出jvm的heap內容到檔案=. live子選項是可選的,假如指定live選項,那麼只輸出活的物件到檔案.

命令:

jmap -dump:live,format=b,file=myjmapfile.txt 19570

結果:

即可在/root目錄開啟myjmapfile.txt檔案。

當然,file=後面也可以指定檔案存放的目錄,就可以在指定目錄檢視檔案了。

2> -finalizerinfo列印正等候回收的物件的資訊

命令:

jmap -finalizerinfo 3772

結果:

Attaching to process ID 19570, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.80-b11
Number of objects pending for finalization: 0 (等候回收的物件為0個)

3> -heap列印heap的概要資訊,GC使用的演算法,heap(堆)的配置及JVM堆記憶體的使用情況.

命令:

jmap -heap 19570

結果:

解析:

using parallel threads in the new generation.  ##新生代採用的是並行執行緒處理方式

using thread-local object allocation.   

Concurrent Mark-Sweep GC   ##同步並行垃圾回收

 

Heap Configuration:  ##堆配置情況,也就是JVM引數配置的結果[平常說的tomcat配置JVM引數,就是在配置這些]

   MinHeapFreeRatio = 40 ##最小堆使用比例

   MaxHeapFreeRatio = 70 ##最大堆可用比例

   MaxHeapSize      = 2147483648 (2048.0MB) ##最大堆空間大小

   NewSize          = 268435456 (256.0MB) ##新生代分配大小

   MaxNewSize       = 268435456 (256.0MB) ##最大可新生代分配大小

   OldSize          = 5439488 (5.1875MB) ##老年代大小

   NewRatio         = 2  ##新生代比例

   SurvivorRatio    = 8 ##新生代與suvivor的比例

   PermSize         = 134217728 (128.0MB) ##perm區 永久代大小

   MaxPermSize      = 134217728 (128.0MB) ##最大可分配perm區 也就是永久代大小

 

Heap Usage: ##堆使用情況【堆記憶體實際的使用情況】

New Generation (Eden + 1 Survivor Space):  ##新生代(伊甸區Eden區 + 倖存區survior(1+2)空間)

   capacity = 241631232 (230.4375MB)  ##伊甸區容量

   used     = 77776272 (74.17323303222656MB) ##已經使用大小

   free     = 163854960 (156.26426696777344MB) ##剩餘容量

   32.188004570534986% used ##使用比例

Eden Space:  ##伊甸區

   capacity = 214827008 (204.875MB) ##伊甸區容量

   used     = 74442288 (70.99369812011719MB) ##伊甸區使用

   free     = 140384720 (133.8813018798828MB) ##伊甸區當前剩餘容量

   34.65220164496263% used ##伊甸區使用情況

From Space: ##survior1區

   capacity = 26804224 (25.5625MB) ##survior1區容量

   used     = 3333984 (3.179534912109375MB) ##surviror1區已使用情況

   free     = 23470240 (22.382965087890625MB) ##surviror1區剩餘容量

   12.43827838477995% used ##survior1區使用比例

To Space: ##survior2 區

   capacity = 26804224 (25.5625MB) ##survior2區容量

   used     = 0 (0.0MB) ##survior2區已使用情況

   free     = 26804224 (25.5625MB) ##survior2區剩餘容量

   0.0% used ## survior2區使用比例

PS Old  Generation: ##老年代使用情況

   capacity = 1879048192 (1792.0MB) ##老年代容量

   used     = 30847928 (29.41887664794922MB) ##老年代已使用容量

   free     = 1848200264 (1762.5811233520508MB) ##老年代剩餘容量

   1.6416783843721663% used ##老年代使用比例

Perm Generation: ##永久代使用情況

   capacity = 134217728 (128.0MB) ##perm區容量

   used     = 47303016 (45.111671447753906MB) ##perm區已使用容量

   free     = 86914712 (82.8883285522461MB) ##perm區剩餘容量

   35.24349331855774% used ##perm區使用比例



4> -histo[:live]列印每個class的例項數目,記憶體佔用,類全名資訊.VM的內部類名字開頭會加上字首”*”.如果live子引數加上後,只統計活的物件數量.

命令:

jmap -histo:live 19570

結果:

num     #instances(例項)   #bytes(位元組大小)  class name(類名)

----------------------------------------------

   1:         65220        9755240      <constMethodKlass>
   2:         65220        8880384      <methodKlass>
   3:         11721        8252112      [B
   4:          6300        6784040      <constantPoolKlass>
   5:         75224        6218208      [C
   6:         93969        5163280      <symbolKlass>
   7:          6300        4854440      <instanceKlassKlass>
   8:          5482        4203152      <constantPoolCacheKlass>
   9:         72097        2307104      java.lang.String
  10:         15102        2289912      [I
  11:          4089        2227728      <methodDataKlass>
  12:         28887        1386576      org.apache.velocity.runtime.parser.Token
  13:          6792         706368          java.lang.Class
  14:          7445         638312          [Ljava.util.HashMap$Entry;

4380:             1             16          com.sun.proxy.$Proxy208
4381:             1             16          sun.reflect.GeneratedMethodAccessor198
4382:             1             16          com.sun.proxy.$Proxy46
4383:             1             16          org.apache.ibatis.ognl.SetPropertyAccessor
4384:             1             16          oracle.jdbc.driver.OracleDriver
4385:             1             16          com.sun.proxy.$Proxy181
4386:             1             16          com.sun.proxy.$Proxy82
4387:             1             16          java.util.jar.JavaUtilJarAccessImpl
4388:             1             16          com.sun.proxy.$Proxy171
4389:             1             16          sun.reflect.GeneratedMethodAccessor136
4390:             1             16            sun.reflect.GeneratedConstructorAccessor22
4391:             1             16          org.elasticsearch.action.search.SearchAction
4392:             1             16          org.springframework.core.annotation.AnnotationAwareOrderComparator
Total       1756265      162523736

  採用jmap-histopid>a.log日誌將其儲存,在一段時間後,使用文字對比工具,可以對比出GC回收了哪些物件。

  jmap-dump:format=b,file=outfile3024可以將3024程序的記憶體heap輸出出來到outfile檔案裡,再配合MAT(記憶體分析工具)。


5> -permstat列印classload和jvmheap長久層的資訊.包含每個classloader的名字,活潑性,地址,父classloader和載入的class數量.另外,內部String的數量和佔用記憶體數也會打印出來.

命令:

jmap -permstat 19570

結果:

解析:

Attaching to process ID 19570, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.80-b11
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness.liveness analysis may be inaccurate ...
class_loader    classes    bytes    parent_loader    alive?    type

<bootstrap>    2538    14654264      null      live    <internal>
0x000000070af968c8    63    399160    0x0000000707db1788    dead    org/apache/catalina/loader/WebappClassLoader@0x000000070367d2a8
0x000000070cba7b08    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba6a38    28    221344    0x0000000707e709a8    dead    org/apache/jasperrvlet/JasperLoader@0x0000000705b11878
0x000000070baed8b8    32    297296    0x0000000707e709a8    dead    org/apache/jasperrvlet/JasperLoader@0x0000000705b11878
0x000000070919a610    1    3056    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070bdd1e18    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x0000000707f1d480    1    3072      null      dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba7f48    1    1912    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba8508    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba6c40    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070bd4c6a0    1    3056      null      dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba62b0    28    235472    0x0000000707e709a8    dead    org/apache/jasperrvlet/JasperLoader@0x0000000705b11878
0x000000070cba77c8    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba7388    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba8148    1    3064    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070afd8b60    1    6704    0x0000000707db1788    dead    org/apache/catalina/loader/WebappClassLoader@0x000000070367d2a8
0x000000070919a410    1    1888      null      dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070bdd05b0    1    1912      null      dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070bc848b8    1    3088    0x0000000707db1788    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba64e8    1    1888    0x0000000707e709a8    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x0000000707f1d2c0    1    3064    0x0000000707db1788    dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070be82e38    1    1912      null      dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
0x000000070cba7908    1    3208      null      dead    sun/reflect/DelegatingClassLoader@0x0000000702a50b98
.........
total = 273 12995 87547304 N/A alive=1, dead=272 N/A


6> -F強迫.在pid沒有相應的時候使用-dump或者-histo引數.在這個模式下,live子引數無效.

7> -h|-help列印輔助資訊

8> -J傳遞引數給jmap啟動的jvm.