zoukankan      html  css  js  c++  java
  • Java内存分析工具jmap

    1. jmap

    1.1 概述

    JVM Memory Map命令用于生成heap dump文件,如果不使用这个命令,还可以使用-XX:+HeapDumpOnOutOfMemoryError参数来让虚拟机出现OOM的时候自动生成dump文件。 

    jmap不仅能生成dump文件,还可以查询finalize执行队列、Java堆和老年代的详细信息,如当前使用率、当前使用的是哪种收集器等。

    > 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

    1.2 参数

    option:选项参数,不可同时使用多个选项参数。

    pid:Java进程id。

    executable:产生核心dump的Java可执行文件。

    core:需要打印配置信息的核心文件。

    remote-hostname-or-ip:远程调试的主机名或ip。

    server-id:可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标示服务器。

    1.3 options参数

    heap:显示Java堆详细信息;

    histo:线下堆中对象的统计信息;

    clstats:Java堆中内存的类加载器的统计信息;

    finalizerinfo:显示在F-Queue队列等待Finlizer线程执行finalizer方法的对象;

    dump:生成堆转储快照;

    F:当-dump没有响应时,强制生成dump快照;

    2. 用法

    所有测试基于如下JDK版本:

    > java -version
    java version "1.8.0_121"
    Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
    

      

    2.1 jmap -dump:live,format=b,file=dump.hprof 129665

    dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名。

    > jmap -dump:live,format=b,file=dump.hprof 129665
    Dumping heap to /opt/huawei/inputMethod/flight/flight/dump.hprof ...
    Heap dump file created
    

      

    dump.hprof这个文件可以通过eclipse的打开:

    2.2 jmap -heap 129665

    打印heap的概要信息,GC使用的算法,heap的配置和使用情况,可以用此来判断内存目前的使用情况以及垃圾回收情况。

    > jmap -heap 129665
    Attaching to process ID 129665, please wait...
    Debugger attached successfully.
    Server compiler detected.
    JVM version is 25.121-b13
    
    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                  = 805306368 (768.0MB)
       MaxNewSize               = 805306368 (768.0MB)
       OldSize                  = 1342177280 (1280.0MB)
       NewRatio                 = 2
       SurvivorRatio            = 8
       MetaspaceSize            = 21807104 (20.796875MB)
       CompressedClassSpaceSize = 1073741824 (1024.0MB)
       MaxMetaspaceSize         = 17592186044415 MB
       G1HeapRegionSize         = 0 (0.0MB)
    
    Heap Usage:
    New Generation (Eden + 1 Survivor Space):
       capacity = 724828160 (691.25MB)
       used     = 291033744 (277.55140686035156MB)
       free     = 433794416 (413.69859313964844MB)
       40.15210225827871% used
    Eden Space:
       capacity = 644349952 (614.5MB)
       used     = 291033744 (277.55140686035156MB)
       free     = 353316208 (336.94859313964844MB)
       45.167031222189024% used
    From Space:
       capacity = 80478208 (76.75MB)
       used     = 0 (0.0MB)
       free     = 80478208 (76.75MB)
       0.0% used
    To Space:
       capacity = 80478208 (76.75MB)
       used     = 0 (0.0MB)
       free     = 80478208 (76.75MB)
       0.0% used
    concurrent mark-sweep generation:
       capacity = 1342177280 (1280.0MB)
       used     = 4730600 (4.511451721191406MB)
       free     = 1337446680 (1275.4885482788086MB)
       0.3524571657180786% used
    
    7119 interned Strings occupying 644232 bytes.
    

    2.3 jmap -finalizerinfo 129665

    打印等待回收的对象信息。

    > jmap -finalizerinfo 129665
    Attaching to process ID 129665, please wait...
    Debugger attached successfully.
    Server compiler detected.
    JVM version is 25.121-b13
    Number of objects pending for finalization: 0
    

    Number of objects pending for finalization:0 说明当前F-Queue队列中并没有等待Finalizer线程执行finalizer方法的对象。

    2.4 jmap -histo:live 129665

    打印堆的对象统计,包括对象数、内存大小等。jmap -histo:live这个命令执行,JVM会先触发gc,然后再统计信息。

    第一列:编号id

    第二列:实例个数

    第三列:所有实例大小

    第四列:类名

    > jmap -histo:live 129665 | grep com.netflix
     658:             1             40  com.netflix.hystrix.strategy.HystrixPlugins
     790:             1             24  com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties$4
     934:             1             16  com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties
    

    2.5 jmap -clstats 129665

    打印Java类加载器的智能统计信息,对于每个类加载器而言,对于每个类加载器而言,它的名称,活跃度,地址,父类加载器,它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。  

    jmap -clstats 129665
    Attaching to process ID 129665, please wait...
    Debugger attached successfully.
    Server compiler detected.
    JVM version is 25.121-b13
    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>	1574	2848839	  null  	live	<internal>
    0x00000000b002b6a0	1224	2203269	0x00000000b002d248	live	sun/misc/Launcher$AppClassLoader@0x000000010000f6a0
    0x00000000b02f3a68	0	0	0x00000000b002b6a0	dead	java/util/ResourceBundle$RBClassLoader@0x000000010007aad8
    0x00000000b0003e58	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x0000000100009df8
    0x00000000b002ce78	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x0000000100009df8
    0x00000000b002d248	112	240746	  null  	live	sun/misc/Launcher$ExtClassLoader@0x000000010000fa48
    
    total = 6	2912	5295796	    N/A    	alive=3, dead=3	    N/A

    2.6 -F

    强制模式。如果指定的pid没有响应,请使用jmap -dump或jmap -histo选项。此模式下,不支持live子选项。

    比如:

    jmap -F -histo 129665 | grep com.netflix
    Iterating over heap. This may take a while...
    733:		1	40	com.netflix.hystrix.strategy.HystrixPlugins
    873:		1	24	com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties$4
    1047:		1	16	com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties
    Heap traversal took 7.434 seconds.
    

      

      

     

  • 相关阅读:
    赫尔维茨公式
    从解析几何的角度分析二次型
    Struts 1 Struts 2
    记一次服务器被入侵的调查取证
    契约式设计 契约式编程 Design by contract
    lsblk df
    Linux Find Out Last System Reboot Time and Date Command 登录安全 开关机 记录 帐号审计 历史记录命令条数
    Infrastructure for container projects.
    更新文档 版本控制 多版本并发控制
    Building Microservices: Using an API Gateway
  • 原文地址:https://www.cnblogs.com/lujiango/p/9002270.html
Copyright © 2011-2022 走看看