美文网首页
jmap - JVM堆内存分析

jmap - JVM堆内存分析

作者: cf6bfeab5260 | 来源:发表于2019-04-18 14:39 被阅读0次

1 作用

  • 生成 java 程序的 dump 文件
  • 查看堆内对象示例的统计信息
  • 查看 ClassLoader 的信息
  • finalizer 队列

2 使用

2.1 生成 java 程序的 dump 文件

[root@kvm15 zengfiles]# jmap -dump:format=b,file=test 25274
Dumping heap to /root/zengfiles/test ...
Heap dump file created
[root@kvm15 zengfiles]# ls
jdk-6u45-linux-x64.bin  jdk-7u80-linux-x64.tar.gz  jdk-9.0.1_linux-x64_bin.tar.gz  test

这个命令执行,JVM会将整个heap的信息dump写入到一个文件,heap如果比较大的话,就会导致这个过程比较耗时,并且执行的过程中为了保证dump的信息是可靠的,所以会暂停应用, 线上系统慎用。

然后用分析工具分析,比如jhat、eclipse Memory Analyzer、jvisualvm,这里以jhat为例:

[root@kvm15 zengfiles]# jhat -J-Xmx1024M test
Reading from test...
Dump file created Thu Apr 18 14:14:11 CST 2019
Snapshot read, resolving...
Resolving 3706025 objects...
Chasing references, expect 741 dots.....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references.....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

访问7000端口:
可以统计实例总数、占用大小、:


image.png
image.png

2.2 查看进程的内存映像信息

[root@kvm15 ~]# jmap 25274
Attaching to process ID 25274, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
0x0000000000400000  7K  /usr/java/jdk1.8.0_151/bin/java
0x00007f9942dec000  48K /usr/java/jdk1.8.0_151/jre/lib/amd64/libinstrument.so
0x00007f998049a000  90K /usr/java/jdk1.8.0_151/jre/lib/amd64/libnio.so
0x00007f99808ab000  49K /usr/java/jdk1.8.0_151/jre/lib/amd64/libmanagement.so
0x00007f9980eb4000  66K /usr/lib64/libbz2.so.1.0.6
0x00007f99810c4000  153K    /usr/lib64/liblzma.so.5.2.2
0x00007f99812ea000  88K /usr/lib64/libz.so.1.2.7
0x00007f9981500000  97K /usr/lib64/libelf-0.168.so
0x00007f9981718000  19K /usr/lib64/libattr.so.1.1.0
0x00007f998191d000  86K /usr/lib64/libgcc_s-4.8.5-20150702.so.1
0x00007f9981b33000  290K    /usr/lib64/libdw-0.168.so
0x00007f9981d7a000  19K /usr/lib64/libcap.so.2.22
0x00007f9981f7f000  108K    /usr/lib64/libresolv-2.17.so
0x00007f9982199000  27K /usr/lib64/libnss_dns-2.17.so
0x00007f99882e0000  113K    /usr/java/jdk1.8.0_151/jre/lib/amd64/libnet.so
0x00007f999c02b000  64K /usr/lib64/libnss_myhostname.so.2
0x00007f999de5c000  125K    /usr/java/jdk1.8.0_151/jre/lib/amd64/libzip.so
0x00007f999e078000  60K /usr/lib64/libnss_files-2.17.so
0x00007f999e28b000  220K    /usr/java/jdk1.8.0_151/jre/lib/amd64/libjava.so
0x00007f999e4b7000  64K /usr/java/jdk1.8.0_151/jre/lib/amd64/libverify.so
0x00007f999e6c6000  43K /usr/lib64/librt-2.17.so
0x00007f999e8ce000  1112K   /usr/lib64/libm-2.17.so
0x00007f999ebd0000  16615K  /usr/java/jdk1.8.0_151/jre/lib/amd64/server/libjvm.so
0x00007f999fbc7000  2077K   /usr/lib64/libc-2.17.so
0x00007f999ff8a000  19K /usr/lib64/libdl-2.17.so
0x00007f99a018e000  101K    /usr/java/jdk1.8.0_151/lib/amd64/jli/libjli.so
0x00007f99a03a4000  141K    /usr/lib64/libpthread-2.17.so
0x00007f99a05c0000  160K    /usr/lib64/ld-2.17.so

2.3 显示Java堆详细信息(配置参数以及使用情况)

[root@kvm15 ~]# jmap -heap 25274
Attaching to process ID 25274, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 918552576 (876.0MB)
   NewSize                  = 19398656 (18.5MB)
   MaxNewSize               = 306184192 (292.0MB)
   OldSize                  = 39321600 (37.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 262668288 (250.5MB)
   used     = 247940704 (236.45468139648438MB)
   free     = 14727584 (14.045318603515625MB)
   94.3930863858221% used
From Space:
   capacity = 4194304 (4.0MB)
   used     = 0 (0.0MB)
   free     = 4194304 (4.0MB)
   0.0% used
To Space:
   capacity = 18350080 (17.5MB)
   used     = 0 (0.0MB)
   free     = 18350080 (17.5MB)
   0.0% used
PS Old Generation
   capacity = 103284736 (98.5MB)
   used     = 42724416 (40.74517822265625MB)
   free     = 60560320 (57.75482177734375MB)
   41.36566317020939% used

30627 interned Strings occupying 4009112 bytes.

2.4 显示堆中存活的对象的统计信息

[root@kvm15 zengfiles]# jmap -histo:live 25274 | more

 num     #instances         #bytes  class name
----------------------------------------------
   1:        108533       10911296  [C
   2:        141720        4535040  java.util.HashMap$Node
   3:        107257        2574168  java.lang.String
   4:         41996        2548736  [Ljava.lang.Object;
   5:          9388        1630688  [Ljava.util.HashMap$Node;
   6:         45205        1446560  java.util.concurrent.ConcurrentHashMap$Node
   7:         11532        1275600  java.lang.Class
   8:         13645        1200760  java.lang.reflect.Method
   9:         35722         857328  java.util.ArrayList
  10:          2831         782424  [B
  11:         19338         773520  java.util.LinkedHashMap$Entry
  12:         31471         755304  io.termd.core.term.Feature
  13:         13489         647472  org.aspectj.weaver.reflect.ShadowMatchImpl
  14:         30539         488624  io.termd.core.term.OpCode$Literal
  15:          4488         480392  [I
  16:          8216         460096  java.util.LinkedHashMap
  17:           241         437232  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  18:         13489         431648  org.aspectj.weaver.patterns.ExposedState
  19:         26476         423616  io.termd.core.term.Sequence
  20:         22915         366640  java.lang.Object
  21:         10080         217328  [Ljava.lang.Class;
  22:          3931         188688  java.util.HashMap
  23:          2708         134904  [Ljava.lang.String;
  24:          3076         123040  java.lang.ref.SoftReference
  25:          1347         107760  java.lang.reflect.Constructor
  26:          1218          87696  org.springframework.core.annotation.AnnotationAttributes
  27:          2700          86400  com.taobao.text.Style$Composite
  28:          2647          84704  io.termd.core.term.OpCode$Printf
  29:          4643          74288  io.termd.core.term.OpCode$PushParam
  30:          2281          72992  java.lang.ref.WeakReference

2.5 打印类加载信息

-clstats是-permstat的替代方案,在JDK8之前,-permstat用来打印类加载器的数据
打印Java堆内存的永久保存区域的类加载器的智能统计信息。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。

[root@kvm15 zengfiles]# jmap -clstats 25274 | more
Attaching to process ID 25274, please wait...
finding class loader instances ..Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
done.
computing per loader stat ..done.
please wait.. computing liveness.liveness analysis may be inaccurate ...
class_loader    classes bytes   parent_loader   alive?  type

<bootstrap> 2474    4165685   null      live    <internal>
0x00000000ca596758  1   880   null      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca596b58  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000c9767270  1   1476    0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca593d50  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca596050  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca598950  1   1474    0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca59af50  1   1474    0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca2c4f20  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000c98e9480  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca597648  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca598148  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca2c4b38  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000c9401b58  0   0   0x00000000c94fd660  dead    java/util/ResourceBundle$RBClassLoader@0x00000001000b5ca0
0x00000000ca596f40  1   1473    0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca597a40  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000c98e8b90  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca596e78  1   1473    0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000c9d25ef0  1   1505      null      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000c9766f50  1   1473    0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca594070  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca596370  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca598c70  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca59a870  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca2c4100  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca2c4c00  1   880 0x00000000c9547130  dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000ca052828  0   0   0x00000000c9547130  dead    org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedWebappClassLoader@0x00000001004c20b8

2.6 打印等待终结的对象信息

[root@kvm15 zengfiles]# jmap -finalizerinfo 25274 |more
Attaching to process ID 25274, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Number of objects pending for finalization: 0

我这里没有

相关文章

网友评论

      本文标题:jmap - JVM堆内存分析

      本文链接:https://www.haomeiwen.com/subject/hukzwqtx.html