首页 » 编程语言 » Java » Java常用分析工具之jmap

Java常用分析工具之jmap

 

简介

jmap是JDK中提供的一个用来监视进程运行中的jvm物理内存的占用情况的工具。该进程内存内,所有对象的情况,例如产生了哪些对象,对象数量。当系统崩溃时,jmap 可以从core文件或进程中获得内存的具体匹配情况,包括Heap size, Perm size等。
使用jmap会影响线上运行的应用,所以尽量不要在线上执行此命令。如果想dump堆信息,可以使用gcore命令,比jmap -dump快。

使用

jmap的使用很简单,可以通过以下命令查看。

jmap -h
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)
.......

案例

查看堆heap占用情况

jmap -heap 90099
Attaching to process ID 90099, 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)  #垃圾回收的方式

Heap Configuration: #堆配置信息
   MinHeapFreeRatio = 0
   MaxHeapFreeRatio = 100
   MaxHeapSize      = 2147483648 (2048.0MB)
   NewSize          = 1310720 (1.25MB)
   MaxNewSize       = 17592186044415 MB
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)
   G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 34603008 (33.0MB)
   used     = 9773032 (9.320289611816406MB)
   free     = 24829976 (23.679710388183594MB)
   28.24330185398911% used
From Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
To Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
PS Old Generation
   capacity = 89128960 (85.0MB)
   used     = 0 (0.0MB)
   free     = 89128960 (85.0MB)
   0.0% used
PS Perm Generation
   capacity = 22020096 (21.0MB)
   used     = 2666808 (2.5432662963867188MB)
   free     = 19353288 (18.45673370361328MB)
   12.110791887555804% used

677 interned Strings occupying 44048 bytes.

查看对象占用内存情况

$ jmap -histo:live 96176

 num     #instances         #bytes  class name
----------------------------------------------
   1:          5948         766656  <methodKlass>
   2:          5948         679168  <constMethodKlass>
   3:           401         474448  <constantPoolKlass>
   4:           365         289664  <constantPoolCacheKlass>
   5:           401         276112  <instanceKlassKlass>
   6:           562          93176  [B
   7:           900          86256  [C
   8:           463          45328  java.lang.Class
   9:           653          43216  [[I
  10:           608          37144  [S
  11:            45          24480  <objArrayKlassKlass>

输出内容的一些说明:
[C is a char[]
[S is a short[]
[I is a int[]
[B is a byte[]
[[I is a int[][]
上面的输出中[C对象占用Heap这么多,往往跟String有关,String其内部使用final char[]数组来保存数据的。

constMethodKlass/ methodKlass/ constantPoolKlass/ constantPoolCacheKlass/ instanceKlassKlass/ methodDataKlass
与Classloader相关,常驻与Perm区。

导出堆信息

可以使用jmap把堆信息导出,并使用可视化工具mat分析。导出的命令如下:

$ jmap -dump:format=b,file=a.dmp 96185
Dumping heap to /Users/canglong/dev/test/a.dmp ...
Heap dump file created

技术交流

原文链接:Java常用分析工具之jmap,转载请注明来源!

原文链接:Java常用分析工具之jmap,转载请注明来源!

2