Java command: jmap use

I. Introduction

After jdk installation comes with a number of small tools, jmap command (Java Memory Map) is one of them. Java is mainly used to print the specified process (or core file, remote debugging server) shared object memory maps or heap memory details.

jmapCommand to get a snapshot of a running jvm heap, which can pile off-line analysis to check for memory leaks, inspect large objects created some seriously affect the performance of the system up to check what objects, various objects share memory size Wait. Jmap can be used to generate Heap Dump.

java memory = direct memory (Direct Memory) + jvm memory (MaxPermSize + Xmx)

1) direct memory heap with

直接内存 By the program itself is a piece of memory management, it is more efficient than the standard memory pool to be used primarily as a communication network to store data when the data buffer and the buffer disk data exchange.

DirectMemoryCapacity can be -XX:MaxDirectMemorySizespecified, if not specified, the default is the same as the maximum Java heap (-Xmx specified). However, the latest version of OSX on the JVM, the default size of the direct memory be amended to read "assigned by default when you do not specify the size of the direct memory direct memory size is 64MB", you can -XX:MaxMemorySizebe displayed directly specify the size of memory .

2) heap (Heap) and non-stack (Non-heap) memory

According to the official statement: "Java virtual machine has a heap that is the runtime data area from which memory for all class instances and arrays are allocated from here heap is created when the Java virtual machine started.." "In the JVM heap of external memory is called non-heap memory (non-heap memory) ".

JVM can be seen mainly manage two types of memory: 堆和非堆.

  • That is, Java code and memory, is left to the developer to use;
  • 非堆 JVM is left to their own use.

Therefore methods zone, or inside the JVM process necessary to optimize memory (e.g., the JIT compiler code cache), the structure of each class (e.g., runtime constant pool, field and method data) and code for methods and constructors are in a non-stack memory.

3) the stack and heap

Stack program to solve operational problems, namely how to program execution, or how to handle the data; heap solve data storage problems, namely how to put the data, where to put.

In Java, a thread will accordingly have a corresponding thread stack, it is very easy to understand, because different threads execute logic is different and therefore require a separate thread stack. The heap is shared by all threads. Because the stack is running the unit, so the information inside the store with all the relevant information of the current thread (or program). Including local variables, program running state, the return value of the method and the like; and the stack is only responsible for storing object information.

A Java heap is the runtime data area, the class (object space dispense these objects by establishing new, newarray, anewarray multianewarray and other instructions, program code, they do not need to explicitly release. Heap garbage collection is responsible for the the heap is the advantage of dynamically allocated memory size, survival do not have to tell the compiler in advance, because the dynamic allocation of memory, Java's garbage collector automatically taken away these data it is no longer used at run time. but the disadvantage is that, due to the dynamic allocation of memory access slower at runtime. advantage of the stack is accessed faster than reactor, after register, stack data can be shared, but the disadvantage is that there is data in the stack size and survival must be determined, lack of flexibility. the main stack to store some basic types of variables (, int, short, long, byte, float, double, boolean, char) and object handle.

The thread occupied for memory size and allocation applications in MaxPermSize in.

Second, the introduction command

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

Note:

  • -heap: The case of printing jvm heap of
  • -histo: Histogram of print jvm heap. Output information includes the class name, number of objects, the size occupied by the object.
  • -histo:live : Same as above, but only agreed to the case of live objects
  • -permstat: Print permanent generation heap cases

Third, the use case

1、jmap -heap [pid]

Pid show as a whole heap of information.

First, start a tomcat, and then use the following command to get the tomcat process ID.

ps -ef|grep tomcat

Here Insert Picture Description
Then execute:

jmap -heap 86038

Output content:

Attaching to process ID 86038, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13

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

Heap Configuration:         # 堆内存初始化配置
   MinHeapFreeRatio         = 0                        # -XX:MinHeapFreeRatio设置JVM堆最小空闲比率  
   MaxHeapFreeRatio         = 100                      # -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率  
   MaxHeapSize              = 2147483648 (2048.0MB)    # -XX:MaxHeapSize=设置JVM堆的最大大小
   NewSize                  = 44564480 (42.5MB)        # -XX:NewSize=设置JVM堆的‘新生代’的默认大小
   MaxNewSize               = 715653120 (682.5MB)      # -XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
   OldSize                  = 89653248 (85.5MB)        # -XX:OldSize=设置JVM堆的‘老生代’的大小
   NewRatio                 = 2                        # -XX:NewRatio=:‘新生代’和‘老生代’的大小比率
   SurvivorRatio            = 8                        # -XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
   MetaspaceSize            = 21807104 (20.796875MB)   
   CompressedClassSpaceSize = 1073741824 (1024.0MB)    
   MaxMetaspaceSize         = 17592186044415 MB        
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:           # Eden区内存分布
   capacity = 426770432 (407.0MB)
   used     = 79056192 (75.39385986328125MB)
   free     = 347714240 (331.60614013671875MB)
   18.524289892698096% used
From Space:           # 其中一个Survivor区的内存分布
   capacity = 30932992 (29.5MB)
   used     = 0 (0.0MB)
   free     = 30932992 (29.5MB)
   0.0% used
To Space:             # 另一个Survivor区的内存分布
   capacity = 31457280 (30.0MB)
   used     = 0 (0.0MB)
   free     = 31457280 (30.0MB)
   0.0% used
PS Old Generation
   capacity = 72351744 (69.0MB)
   used     = 21741336 (20.734153747558594MB)
   free     = 50610408 (48.265846252441406MB)
   30.049498184867527% used

16180 interned Strings occupying 2074344 bytes.

2、jmap -histo[:live] [pid]

Display memory situation of class.

jmap -histo 86038

Results of the:

 num     #instances         #bytes  class name
----------------------------------------------
   1:          7287       46167552  [I
   2:         78166       33933008  [B
   3:        221419       25746168  [C
   4:        116110        2786640  java.lang.String
   5:         11708         886224  [Ljava.lang.Object;
   6:         18869         603808  java.util.HashMap$Node
   7:         24275         582600  java.lang.StringBuilder
   8:          6464         568832  java.lang.reflect.Method
   9:          4715         541352  java.lang.Class
  10:          4847         418760  [S
  11:          1686         391288  [Ljava.util.HashMap$Node;

Note:

  • instances: Number of instances;
  • bytes: Memory footprint size;
  • classs name: Class name.

It is basically in accordance with the size of that use in reverse order.

jmap -histo:live 86038

Get all objects of memory conditions to survive.

Note:

  • The command to obtain results with jmap -histo [pid] For consistent results;
  • 执行jmap -histo:live [pid]时,JVM会先触发gc,然后再统计信息

As can be seen from the print result, there is content [C, [B and other class name, only that it takes up so much memory, but do not know what the object is created. Next you need to dump out the other, the use of memory analysis tool further clarify who it is referenced by any object.

In addition you can execute the following command to save the print contents to a file.

jmap -histo:live 86038>a.log

By comparing multiple print content, you can compare the GC which objects recovered.

3、jmap -dump:live,format=b,file=a.log [pid]

Memory dump information to a.log file.

This command is JVM会将整个heap的信息dump写入到一个文件,heap如果比较大的话,就会导致这个过程比较耗时,并且执行的过程中为了保证dump的信息是可靠的,所以会暂停应用executed .

This command is usually used to analyze memory leaks OOM, the usual practice is:

1) First, configure the JVM startup parameters, let the JVM Dump file is automatically generated when encountering OutOfMemoryError

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path

2) then use the command

// 如果只dump heap中的存活对象,则加上选项-live。
jmap -dump:format=b,file=/path/heap.bin [pid]

IV Summary

  • 1, if the program is insufficient memory or frequent GC, probably a memory leak, this time is necessary to use the situation to see Java Heap Dump object.

  • 2, can be used to make Dump heap jvm comes jmap commands directly

  • 3, you can use jmap -heap command to view the heap usage, look at the occupancy of each heap space.

  • 4, jmap -histo: [live] you can check the heap memory object. If you have a large number of objects it continues to be cited, and has not been freed, it had a memory leak, we should combine the code, not the object released.

  • 5, can also be used jmap -dump: format = b, file = command stack save information to a file, and then with jhat command to view details

  • 6, appear in the memory leak, overflow or other prerequisite, it is recommended dump several memory, the memory file archiving are numbered for easy follow-up memory consolidation analysis.

  • 7, in the case of using cms gc, do jmap -heap sometimes cause the process to become T, it is strongly recommended that do not execute this command if you want to get the current memory usage of each region, by jstat -gc or jstat - gccapacity to get.

Published 100 original articles · won praise 45 · views 640 000 +

Guess you like

Origin blog.csdn.net/wangzhongshun/article/details/103017723