文章目录
- 1. 介绍
- 2. 主要选项
- 3. 生成java堆转储快照 jmap -dump
- 4. 显示堆详细信息 jmap -heap pid
- 5. 显示堆中对象统计信息 jmap -histo pid
jmap(Memory Map for Java)
1. 介绍
位置:jdk\bin
作用:
-
jdk安装后会自带一些小工具,jmap命令(Memory Map for Java)是其中之一。主要用于打印指定Java进程(或核心文件、远程调试服务器)的共享对象内存映射或堆内存细节。
-
jmap命令可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等。可以使用jmap生成Heap Dump。
-
如果不想使用jmap命令,要想获取Java堆转储快照还有一些比较“暴力”的手段:譬如在前面用过的 -XX:+HeapDumpOnOutOfMemoryError参数,可以让虚拟机在OOM异常出现之后自动生成dump文件,通过-XX:+HeapDumpOnCtrlBreak参数可以使用[ctrl]+[Break]键让虚拟机生成dump文件,又或者在Linux系统下通过Kill -3 命令发送进程退出信息“恐吓”一下虚拟机,也能拿到dump文件。
-
jmap的作用并不仅仅是为了获取dump文件,他还可以查询finalize执行队列,java堆和永久代的详细信息,如空间使用率、当前用的是哪种收集器等。
语法:
D:\javasoft\java_soft\jdk\bin>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 formatdump-options:live dump only live objects; if not specified,all objects in the heap are dumped.format=b binary formatfile=<file> dump heap to <file>Example: jmap -dump:live,format=b,file=heap.bin <pid>-F force. Use with -dump:<dump-options> <pid> or -histoto force a heap dump or histogram when <pid> does notrespond. The "live" suboption is not supportedin this mode.-h | -help to print this help message-J<flag> to pass <flag> directly to the runtime system
2. 主要选项
选项 | 作用 |
---|---|
-dump | 生成java堆转储快照,格式为:-dump:[live,]format=b,file=,其中live子参数说明是否只dump出存活对象 |
-finalizerinfo | 显示在F-Queue中等待Finalizer线程执行finalize方法的对象,只在linux/solaris平台下有效 |
-heap | 显示堆详细信息,如使用哪种回收期、参数配置、分带状况等,只在linux/solaris平台下有效 |
-histo | 显示堆中对象统计信息,包括类、实例数量和合计容量 |
-permstat | 以ClassLoader为统计口径显示永久代内存状况,只在linux/solaris平台下有效 |
-F | 当虚拟机进程对-dump选项没有响应时,可以使用这个选项强制生成dump快照,只在linux/solaris平台下有效 |
3. 生成java堆转储快照 jmap -dump
生成java对转存快照,格式:jmap -dump:[live,]format=b,file=文件名
可以使用jdk提供的jvisualvm.exe查看hprof文件
4. 显示堆详细信息 jmap -heap pid
D:\javasoft\java_soft\jdk\bin>jmap -heap 8644
Attaching to process ID 8644, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.271-b09using thread-local object allocation.
Parallel GC with 8 thread(s)Heap Configuration:MinHeapFreeRatio = 0MaxHeapFreeRatio = 100MaxHeapSize = 1853882368 (1768.0MB)NewSize = 38797312 (37.0MB)MaxNewSize = 617611264 (589.0MB)OldSize = 78643200 (75.0MB)NewRatio = 2SurvivorRatio = 8MetaspaceSize = 21807104 (20.796875MB)CompressedClassSpaceSize = 1073741824 (1024.0MB)MaxMetaspaceSize = 17592186044415 MBG1HeapRegionSize = 0 (0.0MB)Heap Usage:
PS Young Generation
Eden Space:capacity = 185597952 (177.0MB)used = 1365920 (1.302642822265625MB)free = 184232032 (175.69735717773438MB)0.7359563967602402% used
From Space:capacity = 16252928 (15.5MB)used = 0 (0.0MB)free = 16252928 (15.5MB)0.0% used
To Space:capacity = 18874368 (18.0MB)used = 0 (0.0MB)free = 18874368 (18.0MB)0.0% used
PS Old Generationcapacity = 101187584 (96.5MB)used = 26049224 (24.84247589111328MB)free = 75138360 (71.65752410888672MB)25.743498332759877% used21147 interned Strings occupying 2233672 bytes.
5. 显示堆中对象统计信息 jmap -histo pid
显示堆中对象统计信息,包括类、实例数量和合计容量
命令格式:jmap -histo[:live]
D:\javasoft\java_soft\jdk\bin>jps -l
10048 sun.tools.jps.Jps
13072 org/netbeans/Main
16212 com.xin.demo.threaddemo.bookdemo.DeadLockDemo
8644 com.xin.demo.XinJavaDemoApplication
5560
7016 org.jetbrains.jps.cmdline.Launcher
9544 com.xin.demo.threaddemo.bookdemo.OOMTestD:\javasoft\java_soft\jdk\bin>jmap -histo 9544num #instances #bytes class name
----------------------------------------------1: 1066 1822048 [B2: 6646 978560 [C3: 390 186216 [I4: 6079 145896 java.lang.String5: 722 82784 java.lang.Class6: 1372 68968 [Ljava.lang.Object;7: 845 33800 java.util.LinkedHashMap$Entry8: 791 31640 java.util.TreeMap$Entry9: 783 25056 java.util.HashMap$Node10: 443 22376 [Ljava.lang.String;11: 305 19520 java.net.URL12: 57 16656 [Ljava.util.HashMap$Node;13: 406 16240 java.lang.ref.Finalizer14: 555 13320 java.util.LinkedList$Node15: 107 10272 java.util.jar.JarFile$JarFileEntry16: 123 9840 [Ljava.util.WeakHashMap$Entry;17: 286 9152 java.util.LinkedList18: 152 8512 sun.misc.URLClassPath$JarLoader19: 113 8136 java.lang.reflect.Field20: 120 7680 java.util.jar.JarFile21: 235 7520 java.util.Hashtable$Entry22: 123 6888 sun.nio.cs.UTF_8$Encoder23: 121 6776 java.util.zip.ZipFile$ZipFileInputStream24: 111 6216 java.util.zip.ZipFile$ZipFileInflaterInputStream25: 257 6168 java.io.ExpiringCache$Entry26: 123 5904 java.util.WeakHashMap27: 110 5280 java.util.zip.Inflater28: 121 4840 java.lang.ref.SoftReference29: 119 4760 sun.nio.cs.UTF_8$Decoder30: 130 4160 java.lang.ref.ReferenceQueue31: 256 4096 java.lang.Integer32: 120 3840 java.util.zip.ZipCoder33: 111 3552 java.util.concurrent.ConcurrentHashMap$Node34: 132 3168 java.lang.StringBuffer35: 22 3072 [Ljava.util.Hashtable$Entry;36: 8 3008 java.lang.Thread37: 120 2880 java.util.ArrayDeque38: 59 2832 java.util.HashMap39: 110 2640 java.util.zip.ZStreamRef40: 5 2600 [J41: 106 2544 java.lang.StringBuilder42: 30 2400 [S43: 2 2384 [[Ljava.lang.Object;44: 132 2112 java.lang.ref.ReferenceQueue$Lock45: 26 2080 java.lang.reflect.Constructor46: 16 2048 [Ljava.util.concurrent.ConcurrentHashMap$Node;47: 60 1920 java.io.File48: 39 1872 sun.util.locale.LocaleObjectCache$CacheEntry49: 1 1568 [[B50: 96 1536 java.lang.Object51: 37 1480 java.io.ObjectStreamField52: 26 1456 java.lang.Class$ReflectionData53: 20 1280 java.util.concurrent.ConcurrentHashMap54: 2 1064 [Ljava.lang.invoke.MethodHandle;55: 1 1040 [Ljava.lang.Integer;56: 1 1040 [[C57: 36 864 java.util.jar.Attributes$Name58: 21 840 sun.util.locale.BaseLocale$Key59: 11 648 [Ljava.lang.reflect.Field;60: 19 608 java.util.Locale61: 19 608 sun.util.locale.BaseLocale62: 13 520 java.security.AccessControlContext63: 21 504 java.util.Locale$LocaleKey64: 10 480 java.util.Hashtable65: 12 480 java.util.WeakHashMap$Entry66: 18 432 sun.misc.MetaIndex67: 22 416 [Ljava.lang.Class;68: 10 400 java.io.FileDescriptor69: 13 392 [Ljava.io.ObjectStreamField;70: 1 384 com.intellij.rt.execution.application.AppMainV2$171: 1 384 java.lang.ref.Finalizer$FinalizerThread72: 16 384 java.net.Parts73: 6 384 java.nio.DirectByteBuffer74: 16 384 java.util.ArrayList75: 1 376 java.lang.ref.Reference$ReferenceHandler76: 6 336 java.nio.DirectLongBufferU77: 7 336 java.nio.HeapCharBuffer78: 6 336 java.util.LinkedHashMap79: 10 320 java.lang.OutOfMemoryError80: 3 312 [D81: 13 312 [Ljava.lang.reflect.Constructor;82: 13 312 sun.reflect.NativeConstructorAccessorImpl83: 6 288 java.nio.HeapByteBuffer84: 6 288 java.util.Properties85: 3 280 [[Ljava.lang.String;86: 5 280 java.util.ResourceBundle$CacheKey87: 5 280 sun.util.calendar.ZoneInfo88: 3 264 java.lang.reflect.Method89: 11 264 java.net.StandardSocketOptions$StdSocketOption90: 8 256 java.util.Vector91: 3 240 [Ljava.lang.ThreadLocal$ThreadLocalMap$Entry;92: 5 240 java.util.Hashtable$Enumerator93: 7 224 java.nio.file.attribute.FileTime94: 9 216 sun.misc.JarIndex95: 13 208 sun.reflect.DelegatingConstructorAccessorImpl96: 5 200 java.security.ProtectionDomain97: 6 192 java.io.FileInputStream98: 6 192 java.lang.ThreadLocal$ThreadLocalMap$Entry99: 4 192 java.util.TreeMap100: 8 192 sun.misc.URLClassPath$3101: 4 192 sun.nio.cs.StreamEncoder102: 3 192 sun.nio.cs.ext.DoubleByte$Encoder103: 2 192 sun.util.calendar.Gregorian$Date104: 4 160 java.io.BufferedWriter105: 4 160 java.lang.ClassLoader$NativeLibrary106: 5 160 java.security.CodeSource107: 4 160 java.security.PrivilegedActionException108: 5 160 java.util.ResourceBundle$LoaderReference109: 5 160 sun.util.locale.provider.LocaleProviderAdapter$Type110: 6 144 sun.misc.PerfCounter111: 3 144 sun.misc.URLClassPath112: 2 128 java.io.ExpiringCache$1113: 4 128 java.lang.StringCoding$StringEncoder114: 8 128 java.util.HashSet115: 4 128 java.util.Stack116: 1 120 java.net.SocksSocketImpl117: 5 120 java.util.Collections$UnmodifiableRandomAccessList118: 5 120 sun.misc.FloatingDecimal$PreparedASCIIToBinaryBuffer119: 2 112 java.lang.Package120: 2 112 sun.nio.cs.ISO_8859_1$Encoder121: 2 112 sun.nio.cs.ext.DoubleByte$Decoder122: 2 96 java.io.BufferedReader123: 3 96 java.io.FileOutputStream124: 4 96 java.io.OutputStreamWriter125: 4 96 java.lang.RuntimePermission126: 3 96 java.lang.StringCoding$StringDecoder127: 2 96 java.lang.ThreadGroup128: 6 96 java.lang.ThreadLocal129: 3 96 java.lang.ref.WeakReference130: 1 96 java.net.DualStackPlainSocketImpl131: 2 96 java.util.ResourceBundle$BundleReference132: 6 96 java.util.jar.Attributes133: 4 96 jdk.net.ExtendedSocketOptions$ExtSocketOption134: 1 96 sun.misc.Launcher$AppClassLoader135: 3 96 sun.net.spi.DefaultProxySelector$NonProxyInfo136: 2 96 sun.nio.cs.StreamDecoder137: 4 96 sun.security.action.GetPropertyAction138: 1 88 java.util.jar.JarVerifier139: 1 88 sun.misc.Launcher$ExtClassLoader140: 5 80 [Ljava.security.Principal;141: 2 80 [Lsun.util.locale.provider.LocaleProviderAdapter$Type;142: 2 80 java.io.BufferedInputStream143: 2 80 java.io.ExpiringCache144: 5 80 java.security.ProtectionDomain$Key145: 2 80 java.util.ServiceLoader$LazyIterator146: 2 80 sun.misc.FloatingDecimal$BinaryToASCIIBuffer147: 2 80 sun.misc.URLClassPath$1148: 2 80 sun.misc.URLClassPath$2149: 2 80 sun.util.locale.LanguageTag150: 3 72 java.io.ByteArrayOutputStream151: 3 72 java.lang.ThreadLocal$ThreadLocalMap152: 3 72 java.net.Proxy$Type153: 3 72 java.util.Arrays$ArrayList154: 3 72 java.util.Collections$SynchronizedSet155: 1 72 java.util.ResourceBundle$RBClassLoader156: 3 72 java.util.concurrent.atomic.AtomicLong157: 3 72 sun.misc.FloatingDecimal$ExceptionalBinaryToASCIIBuffer158: 1 72 sun.util.locale.provider.JRELocaleProviderAdapter159: 1 64 [F160: 2 64 [Ljava.lang.Thread;161: 2 64 java.io.DataInputStream162: 2 64 java.io.FileNotFoundException163: 2 64 java.io.FilePermission164: 2 64 java.io.PrintStream165: 2 64 java.lang.ClassValue$Entry166: 2 64 java.lang.VirtualMachineError167: 2 64 java.lang.ref.ReferenceQueue$Null168: 2 64 java.security.BasicPermissionCollection169: 2 64 java.security.Permissions170: 2 64 java.util.ArrayList$Itr171: 2 64 java.util.LinkedHashMap$LinkedEntryIterator172: 2 64 java.util.ServiceLoader173: 1 48 [Ljava.io.File;174: 2 48 [Ljava.lang.reflect.Method;175: 1 48 [Ljava.net.URL;176: 2 48 [Ljava.util.Enumeration;177: 1 48 [Ljava.util.concurrent.TimeUnit;178: 2 48 java.io.BufferedOutputStream179: 2 48 java.io.File$PathStatus180: 2 48 java.io.FilePermissionCollection181: 2 48 java.lang.Class$1182: 2 48 java.net.InetAddress$Cache183: 2 48 java.net.InetAddress$Cache$Type184: 1 48 java.net.SocketInputStream185: 2 48 java.net.URLClassLoader$3186: 2 48 java.nio.charset.CoderResult187: 3 48 java.nio.charset.CodingErrorAction188: 3 48 java.util.Collections$UnmodifiableSet189: 2 48 java.util.Date190: 3 48 java.util.HashMap$KeySet191: 3 48 java.util.LinkedHashMap$LinkedKeySet192: 2 48 java.util.ServiceLoader$1193: 1 48 java.util.StringTokenizer194: 2 48 java.util.jar.Manifest195: 2 48 sun.misc.CompoundEnumeration196: 2 48 sun.misc.FileURLMapper197: 2 48 sun.misc.NativeSignalHandler198: 2 48 sun.misc.Signal199: 3 48 sun.net.www.protocol.jar.Handler200: 2 48 sun.nio.cs.Surrogate$Parser201: 1 48 sun.nio.cs.US_ASCII$Decoder202: 1 48 sun.util.locale.provider.LocaleResources$ResourceReference203: 1 48 sun.util.resources.TimeZoneNames204: 1 48 sun.util.resources.en.TimeZoneNames_en205: 1 40 java.util.ResourceBundle$1206: 1 40 sun.nio.cs.StandardCharsets$Aliases207: 1 40 sun.nio.cs.StandardCharsets$Cache208: 1 40 sun.nio.cs.StandardCharsets$Classes209: 1 40 sun.nio.cs.ext.ExtendedCharsets210: 1 32 [Ljava.lang.OutOfMemoryError;211: 2 32 [Ljava.lang.StackTraceElement;212: 1 32 [Ljava.lang.ThreadGroup;213: 1 32 [Ljava.net.Proxy$Type;214: 1 32 java.io.ByteArrayInputStream215: 2 32 java.io.FileInputStream$1216: 1 32 java.io.WinNTFileSystem217: 1 32 java.lang.ArithmeticException218: 2 32 java.lang.Boolean219: 2 32 java.lang.ClassLoader$2220: 1 32 java.lang.NullPointerException221: 1 32 java.net.InetAddress$InetAddressHolder222: 1 32 java.net.Socket223: 2 32 java.net.URLClassLoader$3$1224: 2 32 java.nio.ByteOrder225: 2 32 java.util.Hashtable$KeySet226: 2 32 java.util.LinkedHashMap$LinkedEntrySet227: 1 32 java.util.LinkedHashMap$LinkedKeyIterator228: 2 32 java.util.concurrent.atomic.AtomicInteger229: 1 32 java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl230: 1 32 jdk.net.ExtendedSocketOptions$1231: 1 32 sun.instrument.InstrumentationImpl232: 2 32 sun.misc.URLClassPath$JarLoader$1233: 1 32 sun.nio.cs.StandardCharsets234: 1 32 sun.util.locale.provider.LocaleResources235: 1 32 sun.util.locale.provider.LocaleServiceProviderPool236: 1 24 [Ljava.io.File$PathStatus;237: 1 24 [Ljava.lang.ClassValue$Entry;238: 1 24 [Ljava.net.InetAddress$Cache$Type;239: 1 24 [Ljava.security.ProtectionDomain;240: 1 24 [Lsun.launcher.LauncherHelper;241: 1 24 java.io.FileReader242: 1 24 java.io.InputStreamReader243: 1 24 java.lang.ClassValue$Version244: 1 24 java.lang.invoke.MethodHandleImpl$4245: 1 24 java.lang.reflect.ReflectPermission246: 1 24 java.net.Inet4Address247: 1 24 java.net.Inet6AddressImpl248: 1 24 java.net.Proxy249: 1 24 java.util.BitSet250: 1 24 java.util.Collections$EmptyMap251: 1 24 java.util.Collections$SetFromMap252: 1 24 java.util.Collections$UnmodifiableCollection$1253: 1 24 java.util.Locale$Cache254: 1 24 java.util.ResourceBundle$Control$CandidateListCache255: 1 24 java.util.concurrent.TimeUnit$1256: 1 24 java.util.concurrent.TimeUnit$2257: 1 24 java.util.concurrent.TimeUnit$3258: 1 24 java.util.concurrent.TimeUnit$4259: 1 24 java.util.concurrent.TimeUnit$5260: 1 24 java.util.concurrent.TimeUnit$6261: 1 24 java.util.concurrent.TimeUnit$7262: 1 24 sun.instrument.TransformerManager263: 1 24 sun.launcher.LauncherHelper264: 1 24 sun.misc.URLClassPath$FileLoader265: 1 24 sun.nio.cs.ISO_8859_1266: 1 24 sun.nio.cs.ThreadLocalCoders$1267: 1 24 sun.nio.cs.ThreadLocalCoders$2268: 1 24 sun.nio.cs.US_ASCII269: 1 24 sun.nio.cs.UTF_16270: 1 24 sun.nio.cs.UTF_16BE271: 1 24 sun.nio.cs.UTF_16LE272: 1 24 sun.nio.cs.UTF_8273: 1 24 sun.nio.cs.ext.GBK274: 1 24 sun.reflect.NativeMethodAccessorImpl275: 1 24 sun.util.locale.BaseLocale$Cache276: 1 24 sun.util.locale.provider.SPILocaleProviderAdapter$1277: 1 24 sun.util.locale.provider.TimeZoneNameProviderImpl278: 1 24 sun.util.resources.LocaleData$1279: 1 16 [Ljava.lang.Throwable;280: 1 16 [Ljava.security.CodeSigner;281: 1 16 [Ljava.security.cert.Certificate;282: 1 16 [Lsun.instrument.TransformerManager$TransformerInfo;283: 1 16 [Lsun.util.calendar.ZoneInfoFile$ZoneOffsetTransitionRule;284: 1 16 java.io.FileDescriptor$1285: 1 16 java.lang.CharacterDataLatin1286: 1 16 java.lang.ClassValue$Identity287: 1 16 java.lang.Runtime288: 1 16 java.lang.String$CaseInsensitiveComparator289: 1 16 java.lang.System$2290: 1 16 java.lang.Terminator$1291: 1 16 java.lang.invoke.MemberName$Factory292: 1 16 java.lang.invoke.MethodHandleImpl$2293: 1 16 java.lang.invoke.MethodHandleImpl$3294: 1 16 java.lang.ref.Reference$1295: 1 16 java.lang.ref.Reference$Lock296: 1 16 java.lang.reflect.ReflectAccess297: 1 16 java.net.InetAddress$2298: 1 16 java.net.URLClassLoader$7299: 1 16 java.nio.Bits$1300: 1 16 java.nio.charset.CoderResult$1301: 1 16 java.nio.charset.CoderResult$2302: 1 16 java.security.ProtectionDomain$2303: 1 16 java.security.ProtectionDomain$JavaSecurityAccessImpl304: 1 16 java.util.Collections$EmptyIterator305: 1 16 java.util.Collections$EmptyList306: 1 16 java.util.Collections$EmptySet307: 1 16 java.util.Hashtable$EntrySet308: 1 16 java.util.ResourceBundle$Control309: 1 16 java.util.ResourceBundle$RBClassLoader$1310: 1 16 java.util.TimeZone$1311: 1 16 java.util.WeakHashMap$KeySet312: 1 16 java.util.concurrent.atomic.AtomicBoolean313: 1 16 java.util.jar.JarVerifier$3314: 1 16 java.util.jar.JavaUtilJarAccessImpl315: 1 16 java.util.zip.ZipFile$1316: 1 16 jdk.net.ExtendedSocketOptions$PlatformSocketOptions317: 1 16 sun.misc.ASCIICaseInsensitiveComparator318: 1 16 sun.misc.FloatingDecimal$1319: 1 16 sun.misc.Launcher320: 1 16 sun.misc.Launcher$BootClassPathHolder$1321: 1 16 sun.misc.Launcher$Factory322: 1 16 sun.misc.Perf323: 1 16 sun.misc.Unsafe324: 1 16 sun.net.spi.DefaultProxySelector325: 1 16 sun.net.www.protocol.file.Handler326: 1 16 sun.reflect.DelegatingMethodAccessorImpl327: 1 16 sun.reflect.ReflectionFactory328: 1 16 sun.util.calendar.Gregorian329: 1 16 sun.util.calendar.ZoneInfoFile$1330: 1 16 sun.util.calendar.ZoneInfoFile$Checksum331: 1 16 sun.util.locale.provider.AuxLocaleProviderAdapter$NullProvider332: 1 16 sun.util.locale.provider.JRELocaleProviderAdapter$1333: 1 16 sun.util.locale.provider.SPILocaleProviderAdapter334: 1 16 sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter335: 1 16 sun.util.resources.LocaleData336: 1 16 sun.util.resources.LocaleData$LocaleDataResourceBundleControl
Total 25196 3662360