Jmap
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. This article focuses on Java heap dump and Jamp commands
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
The memory composition of the Java Virtual machine and the heap memory describes the common memory errors in how Java GC works:
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 the host name or IP address of the remote debugging server (see JSADEBUGD).
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 to the specified file in the hprof binary format 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 object information that waits for finalization.
-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 column chart 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 prefixed with a ' * '. 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 the Jmap-dump or Jmap-histo option. Live sub-options are not supported in this mode.
-h
Print help information.
-help
Print help information.
-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 jhat
the command to see jhat -port 5000 heapDump
the access in the browser: http://localhost:5000/
View more information
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.
Summarize
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 directly use the Jmap command that comes with the JVM
3. You can use the command to see how the jmap -heap
heap is being used, and to see how each heap space is occupied.
4. Use to jmap -histo:[live]
view objects in 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 use the jmap -dump:format=b,file=<fileName>
command to save heap information to a file, and then view the details with the Jhat command
6. In memory leakage, 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 be Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can‘t attach to the process
an error: This is a bug:http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7050524 mentioned in the ORACLA documentation, The solution is as follows:
echo 0 | sudo tee/proc/sys/kernel/yama/ptrace_scope This method is valid until the next restart.
Permanent effective method sudo vi/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, make the repair Change to take effect.
Java Command Learning Series (iii)--JMAP