Jmap is a JDK-brought tool software that is used primarily to print shared object memory mappings or heap memory details for a specified Java process (or core file, remote debugging server). You can use Jmap to generate a heap Dump.
What is heap dump
Heap dump is a memory image that responds to Java heap usage, including system information, virtual machine properties, full thread dump, state of all classes and objects, and so on. In general, we suspect a memory leak in the case of low memory, GC anomalies, and so on. At this point we can make heap dump to see the specifics. Analyze the cause.
Basic Knowledge
Common Memory Errors:
OutOfMemoryError old generation memory is low. Outofmemoryerror:permgen space has insufficient memory for permanent generation. OUTOFMEMORYERROR:GC overhead limit exceed garbage collection time consumes 98% or more of the system run time.
Jmap
Usage Summary
usage:jmap [option]<pid>(to connect to running process) jmap [option]<executable <core>(to connect to a core file) jmap [option] [[email protected]]<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;ifThe "Live"Suboption is specified, only count live objects-Permstat to print permanent generation 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;ifNot specified, all objects in the heap is 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 isn't supported in Thismode. -H | -help to print ThisHelp message-j<flag> to pass <flag> directly to the runtime system
Process that specifies the process number (PID) jmap [option] Specifies the core file jmap [option] Specifies the remote debugging server jmap [option] [[email protected]]
Parameters:
the option options parameter is mutually exclusive (not used concurrently). To use the option parameter, follow the command name directly. The PID requires the process ID of the print configuration information. The process must be a Java process. To get a list of running Java processes, you can use JPS. Executable the Java executable that generated the core dump. Core files that need to print configuration information. remote-hostname-or-IP remotely debug server (see JSADEBUGD) hostname or IP address. server-id An optional unique ID that identifies the server with this option parameter if more than one debug server is running on the same remote host.
Options:
<no option>If you print a Shared object mapping using JMAP with no option parameters, the start address, map size, and path name of the shared object file will be printed for each shared object loaded in the target virtual machine. This is similar to the Solaris Pmap tool. -dump:[live,]format=b,file=<filename>Dumps the Java heap in the Hprof binary format to the file that specifies filename. The live sub-option is optional. If you specify the live sub-option, only the active objects in the heap are dumped. To browse heap dump, you can use Jhat (Java Heap Analysis tool) to read the generated files. -Finalizerinfo Prints the object information waiting to be finalized. -The heap prints summary information for a heap, including the GC algorithm used, the heap configuration information, and the Generation wise heap usage. -histo[:live] Prints a histogram of the heap. These include each Java class, the number of objects, the memory size (in bytes), the fully qualified class name. The class name inside the printed virtual machine will be accompanied by a ' *' prefix. If the live sub-option is specified, only the active object is computed. -Permstat Smart Statistics for the class loader that prints the permanently saved area of Java heap memory. For each classloader, its name, activity, address, parent ClassLoader, the number of classes it loads, and the size of the class will be printed. In addition, the number and size of strings that are included are also printed. -F forced mode. If the specified PID is not responding, use Jmap-dump or Jmap-histo option. Live sub-options are not supported in this mode. -h Print help information. -Help print assistance information. The-j<flag> specifies parameters that are passed to the JVM running Jmap.
Example
To view the Java heap usage, execute the command:
[Email protected]:~/workspace/design_apaas/apaasweb/control/bin$ jmap-heap 31846
Attaching to process ID 31846, please wait ... Debugger attached successfully. Server compiler detected. JVM version is24.71-b01using Thread-local object allocation. Parallel GC with4 Thread (s)//GC ModeHeap Configuration://heap Memory Initialization configurationMinheapfreeratio = 0//JVM startup parameter-xx:minheapfreeratio set JVM heap minimum idle ratio (default)Maxheapfreeratio = 100//JVM startup parameter-xx:maxheapfreeratio set JVM heap max idle ratio (default)Maxheapsize = 2082471936 (1986.0MB)//corresponding JVM startup parameters-xx:maxheapsize= Setting the maximum size of the JVM heapNewSize = 1310720 (1.25MB)//corresponding JVM startup parameters-xx:newsize= Set the default size of the ' Cenozoic ' of the JVM heapMaxnewsize = 17592186044415 MB//corresponding JVM startup parameters-xx:maxnewsize= Setting the maximum size of the ' Cenozoic ' of the JVM heapOldsize = 5439488 (5.1875MB)//corresponding JVM startup parameter-xx:oldsize=<value>: Setting the size of the ' Laosheng generation ' of the JVM heapNewratio = 2//corresponds to JVM startup parameter-xx:newratio=: The size ratio of ' cenozoic ' and ' Laosheng generation 'Survivorratio = 8//corresponding JVM startup parameters-xx:survivorratio= Setting the ratio between Eden and survivor areas in the young generationPermSize = 21757952 (20.75MB)//corresponds to the JVM startup parameter-xx:permsize=<value>: Sets the initial size of the JVM Heap's ' Immortal generation 'MaxPermSize = 85983232 (82.0MB)//corresponding JVM startup parameter-xx:maxpermsize=<value>: Set the maximum size of the JVM Heap's ' Immortal generation 'g1heapregionsize = 0 (0. 0MB) Heap Usage://Heap Memory UsagePS Young Generationeden Space://Eden Area Memory distributionCapacity = 33030144 (31.5MB)//Eden Area Total Capacityused = 1524040 (1.4534378051757812MB)//Eden area is already in useFree = 31506104 (30.04656219482422MB)//Eden Area remaining capacity4.614088270399305% used//Eden Zone Usage rateFrom Space://the memory distribution of one of the survivor areasCapacity = 5242880 (5. 0MB) Used= 0 (0. 0MB) Free= 5242880 (5. 0MB)0%usedto Space://memory distribution in another survivor areaCapacity = 5242880 (5. 0MB) Used= 0 (0. 0MB) Free= 5242880 (5. 0MB)0%Usedps Old Generation//current memory distribution in old areaCapacity = 86507520 (82. 5MB) Used= 0 (0. 0MB) Free= 86507520 (82. 5MB)0%Usedps Perm Generation//current "Immortal generation" Memory distributionCapacity = 22020096 (21. 0MB) Used= 2496528 (2. 3808746337890625MB) Free= 19523568 (18. 619125366210938MB)11.3374982561384%used670 interned Strings occupying 43720 bytes.
View the number and size of objects in heap memory (histogram). Execute command:
[Email protected]:~/workspace/design_apaas/apaasweb/control/bin$ jmap-histo 3331
Num #instances #bytes class name number byte class name -------------------------- -------------------- 1: 7 1322080 [I 2: 5603 722368 < Methodklass> 3: 5603 641944 <constMethodKlass> 4: 34022 544352 Java.lang.Integer 5: 371 437208 <constantPoolKlass> 6: 336 270624 <constantPoolCacheKlass> 7: 371 253816 <instanceklassklass >
Jmap-histo:live This command executes, the JVM triggers the GC first and then counts the information.
Output the details of memory usage to a file and execute the command:
[Email protected]:~/workspace/design_apaas/apaasweb/control/bin$ jmap-dump:format=b,file=heapdump 6900
Then use the Jhat command to see Jhat-port heapdump in the browser: http://localhost:5000/View Details
This command executes, the JVM writes the entire heap information dump to a file, and if the heap is larger, it can cause the process to be time-consuming, and in the process of execution to ensure that the dump information is reliable, it pauses the application.
Summary
1. If the program is out of memory or frequent GC, there is a high likelihood of a memory leak, which is the case with Java heap dump to view the object. 2. To make a heap dump, you can use the Jmap command that comes with the JVM directly 3. You can use the Jmap-heap command to see how the heap is being used, and to see how each heap space is occupied. 4. Use Jmap-histo:[live] To view the objects in the heap memory. If a large number of objects continue to be referenced and not released, then a memory leak is generated, and it is necessary to combine the code to release the unused objects. 5. You can also save the heap information to a file using the jmap-dump:format=b,file=<filename> command, and then use the Jhat command to view the details 6. In memory leaks, Overflow or other prerequisites, it is recommended to dump several times of memory, memory files for the number of archives, easy to follow-up memory collation analysis.
Error attaching to Process:sun.jvm.hotspot.debugger.DebuggerException:Can ' t attach to the process
Using Jmap for the first time in Ubuntu will cause an error: Error attaching to Process:sun.jvm.hotspot.debugger.DebuggerException:Can ' t attach to the Process, which is one of the bug:http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7050524 mentioned in the ORACLA documentation, and is resolved as follows:
1.echo 0 | sudo tee/proc/sys/kernel/yama//etc/sysctl.d/10-ptrace.conf Edit the following line: Kernel.yama.ptrace_scope = 1 modified to: Kernel.yama.ptrace_scope = 0 Restart the system for the changes to take effect.
Reference:
http://www.hollischuang.com/archives/303
Jmap of the Java Command Learning series