• 【深入JVM】JVM工具之JMAP


    一、工具介绍

      假设把javain文件夹配置到环境变量。在cmd输入jmap会有例如以下提示:



    翻译:打印出某个java进程(使用pid)内存内的,全部‘对象’的情况(如:产生那些对象,及其数量)。

    能够输出全部内存中对象的工具,甚至能够将VM 中的heap,以二进制输出成文本。


    二、工具使用方法

          jmap [ option ] pid
           jmap [ option ] executable core
           jmap [ option ] [server-id@]remote-hostname-or-IP

    2.1)、options: 
    executable Java executable from which the core dump was produced.
    (可能是产生core dump的java可运行程序)
    core 将被信息打印的core dump文件
    remote-hostname-or-IP 远程debug服务的主机名或ip
    server-id 唯一id,假如一台主机上多个远程debug服务 
    2.2、基本參数:
    -dump:[live,]format=b,file=<filename> 使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的。假如指定live选项,那么仅仅输出活的对象到文件. 
    -finalizerinfo 打印正等候回收的对象的信息.
    -heap 打印heap的概要信息。GC使用的算法。heap的配置及wise heap的使用情况.
    -histo[:live] 打印每一个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 假设live子參数加上后,仅仅统计活的对象数量. 
    -permstat 打印classload和jvm heap长久层的信息. 包括每一个classloader的名字,活泼性,地址,父classloader和载入的class数量. 另外,内部String的数量和占用内存数也会打印出来. 
    -F 强迫.在pid没有对应的时候使用-dump或者-histo參数. 在这个模式下,live子參数无效. 
    -h | -help 打印辅助信息 
    -J 传递參数给jmap启动的jvm. 
    pid 须要被打印配相信息的java进程id,创业与打工的差别 - 博文预览,能够用jps查问.


    三、測试代码

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.lang.management.ManagementFactory;
    
    public class JMapStudy {
    
    	public static void main(String[] args) {
    		//Gets the currentProcess's name
    		String name = ManagementFactory.getRuntimeMXBean().getName();
    		
    		//result Format:16756@WIN--20141018KO
    		String pid = name.split("@")[0];
    		String command = "tasklist /fi "pid eq " + pid + """;
    		executeCmd(command);
    		new JMapThread().start();
    		command = "jmap -histo " + pid;
    		executeCmd(command);
    
    	}
    
    
    	/**
    	 * Executes the special command by means of runtime, and output the result
    	 * to console
    	 * 
    	 * @param command
    	 */
    	public static void executeCmd(String command) {
    		try {
    			String s;
    			Process process = Runtime.getRuntime().exec(command);
    			BufferedReader bufferedReader =
    
    			new BufferedReader(new InputStreamReader(process.getInputStream()));
    			while ((s = bufferedReader.readLine()) != null)
    				System.out.println(s);
    			process.waitFor();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    class JMapThread extends Thread
    {
    	/**
    	 * This method is used for creating a process to be tested memory.
    	 */
    	public void run() {
    
    		for (int i = 0; i < 10; i++) {
    			try {
    				Thread.sleep(2000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			System.out.println("now i :" + i);
    		}
    	}
    }
    


    四、执行结果

    映像名称                       PID 会话名              会话#       内存使用 
    ========================= ======== ================ =========== ============
    javaw.exe                    12928 Console                    1     13,864 K
    now i :0
    now i :1
    now i :2
    
     num     #instances         #bytes  class name
    ----------------------------------------------
       1:          1518         280472  [C
       2:          1020         182192  <symbolKlass>
       3:           107         110032  [B
       4:            30          96184  [I
       5:          1398          33552  java.lang.String
       6:           259          27200  <constMethodKlass>
       7:           774          24768  java.util.TreeMap$Entry
       8:           259          20976  <methodKlass>
       9:            21          15344  <constantPoolKlass>
      10:           332          14280  [Ljava.lang.Object;
      11:            39          12792  <objArrayKlassKlass>
      12:            21           8864  <instanceKlassKlass>
      13:            21           6440  <constantPoolCacheKlass>
      14:           192           6128  [Ljava.lang.String;
      15:            76           6080  java.lang.reflect.Method
      16:            60           5760  java.lang.Class
      17:           103           4944  java.nio.HeapCharBuffer
      18:            47           3968  [S
      19:            60           2880  java.nio.HeapByteBuffer
      20:            40           2880  java.lang.reflect.Field
      21:           115           2288  [Ljava.lang.Class;
      22:            23           2096  [Ljava.util.HashMap$Entry;
      23:            51           2088  [[I
      24:            83           1992  java.util.Hashtable$Entry
      25:            13           1400  [Ljava.util.Hashtable$Entry;
      26:            83           1328  java.lang.StringBuilder
      27:            53           1272  java.util.HashMap$Entry
      28:            32           1024  java.util.concurrent.ConcurrentHashMap$Segment
      29:            15            840  java.net.URL
      30:            32            768  java.util.concurrent.locks.ReentrantLock$NonfairSync
      31:            35            752  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
      32:            45            720  java.io.File
      33:            10            640  java.lang.reflect.Constructor
      34:            16            640  java.util.HashMap
      35:            19            608  java.util.LinkedHashMap$Entry
      36:            19            608  java.util.Locale
      37:            18            576  java.lang.ref.Finalizer
      38:            36            576  java.lang.ProcessEnvironment$CheckedEntry
      39:             5            520  java.lang.Thread
      40:            20            480  java.util.concurrent.ConcurrentHashMap$HashEntry
      41:            14            448  java.lang.ref.SoftReference
      42:             7            392  sun.nio.cs.ext.GBK$Decoder
      43:            21            336  java.lang.StringBuffer
      44:            10            320  java.io.ObjectStreamField
      45:             2            320  [Ljava.lang.reflect.Method;
      46:            13            312  java.io.ExpiringCache$Entry
      47:            13            312  java.io.FileDescriptor
      48:            17            272  sun.security.action.GetPropertyAction
      49:            11            264  java.net.Parts
      50:            10            240  java.security.AccessControlContext
      51:             5            240  java.util.TreeMap
      52:             6            240  java.util.Hashtable
      53:             9            216  java.util.ArrayList
      54:             5            200  sun.misc.URLClassPath$JarLoader
      55:             4            184  [Ljava.lang.reflect.Field;
      56:             7            168  java.util.Vector
      57:             3            168  sun.nio.cs.ext.GBK$Encoder
      58:             2            160  [Ljava.util.concurrent.ConcurrentHashMap$Segment;
      59:             4            160  java.util.StringTokenizer
      60:             8            152  [Ljava.io.ObjectStreamField;
      61:             3            144  java.io.BufferedReader
      62:             9            144  java.io.FileInputStream
      63:             4            128  java.io.BufferedInputStream
      64:             5            120  sun.reflect.NativeConstructorAccessorImpl
      65:             3            120  sun.nio.cs.StreamDecoder
      66:             2            112  java.io.ExpiringCache$1
      67:             1            104  JMapThread
      68:             1            104  java.lang.ref.Finalizer$FinalizerThread
      69:            13            104  java.lang.Object
      70:             1            104  java.lang.ref.Reference$ReferenceHandler
      71:             2             96  java.net.Inet6Address
      72:             1             96  sun.net.www.protocol.file.FileURLConnection
      73:             2             96  sun.nio.cs.StreamEncoder
      74:             3             96  java.lang.ClassLoader$NativeLibrary
      75:             4             96  java.io.FileOutputStream
      76:             6             96  sun.misc.URLClassPath$3
      77:             2             96  java.lang.ThreadGroup
      78:             4             96  java.lang.OutOfMemoryError
      79:             2             96  java.util.Properties
      80:             2             96  java.util.LinkedHashMap
      81:             4             96  java.io.BufferedOutputStream
      82:             5             80  [Ljava.lang.reflect.Constructor;
      83:             5             80  java.lang.Boolean
      84:             5             80  sun.misc.MetaIndex
      85:             1             80  [Ljava.lang.ThreadLocal$ThreadLocalMap$Entry;
      86:             5             80  sun.reflect.DelegatingConstructorAccessorImpl
      87:             2             80  java.util.concurrent.ConcurrentHashMap
      88:             2             80  java.io.BufferedWriter
      89:             2             80  java.lang.ProcessImpl
      90:             5             80  java.lang.Class$1
      91:             3             72  java.util.Stack
      92:             3             72  java.lang.RuntimePermission
      93:             3             72  java.lang.ref.WeakReference
      94:             3             72  java.lang.NoSuchMethodError
      95:             1             72  sun.misc.Launcher$AppClassLoader
      96:             1             72  sun.misc.Launcher$ExtClassLoader
      97:             3             72  sun.nio.cs.Surrogate$Parser
      98:             2             64  java.lang.ClassNotFoundException
      99:             2             64  java.io.PrintStream
     100:             2             64  sun.misc.URLClassPath$FileLoader$1
     101:             2             64  java.io.FilePermission
     102:             2             64  [Ljava.lang.Thread;
     103:             4             64  java.lang.Integer
     104:             2             64  java.security.CodeSource
     105:             2             64  java.lang.ThreadLocal$ThreadLocalMap$Entry
     106:             4             64  java.net.URLClassLoader$1
     107:             1             64  java.util.regex.Pattern
     108:             2             64  java.util.LinkedHashMap$KeyIterator
     109:             2             64  sun.misc.URLClassPath
     110:             2             64  java.security.PrivilegedActionException
     111:             2             64  java.util.LinkedList$ListItr
     112:             2             64  java.io.ExpiringCache
     113:             1             64  java.util.regex.Matcher
     114:             1             56  [Ljava.util.regex.Pattern$GroupHead;
     115:             3             56  [Ljava.io.File;
     116:             3             48  java.lang.ThreadLocal
     117:             2             48  java.lang.ref.ReferenceQueue
     118:             2             48  [Ljava.net.URL;
     119:             3             48  java.lang.ClassLoader$3
     120:             1             48  [J
     121:             2             48  java.lang.ProcessBuilder
     122:             3             48  java.nio.charset.CodingErrorAction
     123:             2             48  java.net.InetAddress$CacheEntry
     124:             2             48  java.util.LinkedList$Entry
     125:             2             48  java.security.Permissions
     126:             2             48  java.lang.ref.ReferenceQueue$Null
     127:             2             48  java.util.LinkedList
     128:             2             48  java.net.Inet4Address
     129:             2             48  java.lang.management.ManagementPermission
     130:             2             48  java.io.InputStreamReader
     131:             2             48  java.io.OutputStreamWriter
     132:             1             40  java.lang.ProcessEnvironment
     133:             1             40  sun.nio.cs.StandardCharsets$Classes
     134:             1             40  sun.nio.cs.StandardCharsets$Cache
     135:             1             40  java.util.Properties$LineReader
     136:             2             40  [Ljava.net.InetAddress;
     137:             5             40  java.lang.Class$3
     138:             1             40  sun.nio.cs.StandardCharsets$Aliases
     139:             1             32  java.security.ProtectionDomain
     140:             2             32  java.nio.charset.CoderResult
     141:             2             32  sun.security.action.LoadLibraryAction
     142:             1             32  sun.misc.SoftCache
     143:             2             32  sun.misc.Signal
     144:             1             32  sun.nio.cs.ext.ExtendedCharsets
     145:             2             32  java.lang.ProcessImpl$1
     146:             1             32  java.util.Collections$SynchronizedMap
     147:             2             32  java.nio.ByteOrder
     148:             4             32  java.lang.ref.ReferenceQueue$Lock
     149:             2             32  java.util.HashSet
     150:             1             32  [Ljava.lang.ThreadGroup;
     151:             2             32  java.net.InetAddress$Cache
     152:             2             32  java.io.FilePermission$1
     153:             1             32  java.util.RandomAccessSubList
     154:             2             32  java.net.InetAddress$Cache$Type
     155:             1             32  java.util.AbstractList$ListItr
     156:             2             32  sun.misc.NativeSignalHandler
     157:             1             32  [Ljava.lang.OutOfMemoryError;
     158:             1             32  java.util.HashMap$EntryIterator
     159:             1             32  java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl
     160:             4             32  sun.reflect.ReflectionFactory$GetReflectionFactoryAction
     161:             1             24  sun.management.VMManagementImpl
     162:             1             24  java.security.BasicPermissionCollection
     163:             1             24  java.lang.StringCoding$StringDecoder
     164:             1             24  sun.misc.Launcher$AppClassLoader$1
     165:             1             24  java.io.WinNTFileSystem
     166:             1             24  java.lang.reflect.ReflectPermission
     167:             1             24  [Ljava.net.InetAddress$Cache$Type;
     168:             3             24  sun.reflect.ReflectionFactory$1
     169:             1             24  java.util.regex.Pattern$TreeInfo
     170:             1             24  java.lang.NumberFormatException
     171:             1             24  sun.security.action.GetIntegerAction
     172:             1             24  sun.nio.cs.StandardCharsets
     173:             1             24  java.util.SubList$1
     174:             1             24  java.lang.ThreadLocal$ThreadLocalMap
     175:             1             24  sun.nio.cs.ext.GBK
     176:             1             24  java.lang.StringCoding$StringEncoder
     177:             1             24  sun.management.RuntimeImpl
     178:             1             24  java.util.BitSet
     179:             1             24  sun.net.www.MessageHeader
     180:             1             24  java.io.FileReader
     181:             1             24  java.lang.ref.Reference
     182:             1             24  java.util.Collections$UnmodifiableMap
     183:             1             16  sun.misc.Launcher$ExtClassLoader$1
     184:             1             16  [Ljava.security.Principal;
     185:             1             16  java.lang.SystemClassLoaderAction
     186:             1             16  java.security.ProtectionDomain$Key
     187:             1             16  java.security.Policy$UnsupportedEmptyCollection
     188:             1             16  java.util.HashMap$EntrySet
     189:             1             16  sun.security.action.GetBooleanAction
     190:             1             16  java.io.FilePermissionCollection
     191:             1             16  [Ljava.lang.StackTraceElement;
     192:             2             16  sun.net.www.protocol.jar.Handler
     193:             1             16  java.util.regex.Pattern$5
     194:             1             16  java.util.regex.Pattern$Start
     195:             1             16  java.util.regex.Pattern$Single
     196:             1             16  java.util.concurrent.atomic.AtomicInteger
     197:             1             16  java.util.regex.Pattern$Node
     198:             1             16  sun.misc.Launcher
     199:             1             16  sun.misc.URLClassPath$FileLoader
     200:             1             16  java.util.regex.Pattern$LastNode
     201:             1             16  java.util.HashMap$KeySet
     202:             1             16  java.util.Collections$EmptyMap
     203:             1             16  [Ljava.security.cert.Certificate;
     204:             1             16  java.nio.charset.CoderResult$1
     205:             1             16  java.lang.ProcessEnvironment$CheckedEntrySet
     206:             1             16  java.util.Collections$EmptyList
     207:             1             16  java.nio.charset.CoderResult$2
     208:             1             16  java.net.Inet6AddressImpl
     209:             1             16  sun.jkernel.DownloadManager$1
     210:             1             16  java.net.Inet4AddressImpl
     211:             1             16  [Ljava.security.ProtectionDomain;
     212:             1             16  java.lang.ProcessEnvironment$CheckedEntrySet$1
     213:             1              8  sun.net.www.protocol.file.Handler
     214:             1              8  java.net.InetAddress$1
     215:             1              8  sun.misc.Unsafe
     216:             1              8  java.util.Collections$EmptySet
     217:             1              8  java.net.InetAddressImplFactory
     218:             1              8  java.lang.ref.Reference$Lock
     219:             1              8  java.util.Collections$ReverseComparator
     220:             1              8  sun.net.InetAddressCachePolicy$2
     221:             1              8  sun.misc.Launcher$Factory
     222:             1              8  sun.jkernel.DownloadManager$2
     223:             1              8  java.lang.ProcessEnvironment$NameComparator
     224:             1              8  java.lang.ProcessEnvironment$EntryComparator
     225:             1              8  java.util.Hashtable$EmptyEnumerator
     226:             1              8  java.lang.Compiler$1
     227:             1              8  java.lang.Terminator$1
     228:             1              8  java.nio.charset.Charset$3
     229:             1              8  java.lang.System$2
     230:             1              8  java.lang.Runtime
     231:             1              8  java.net.UnknownContentHandler
     232:             1              8  sun.misc.ASCIICaseInsensitiveComparator
     233:             1              8  java.net.URLClassLoader$7
     234:             1              8  java.lang.reflect.ReflectAccess
     235:             1              8  java.security.ProtectionDomain$2
     236:             1              8  sun.reflect.ReflectionFactory
     237:             1              8  sun.net.InetAddressCachePolicy$1
     238:             1              8  java.util.Hashtable$EmptyIterator
     239:             1              8  java.security.Security$1
     240:             1              8  java.lang.String$CaseInsensitiveComparator
    Total          7599         899640
    now i :3
    now i :4
    now i :5
    now i :6
    now i :7
    now i :8
    now i :9
    

    相关链接:

    http://blog.csdn.net/fenglibing/article/details/6411953


    http://blog.csdn.net/zhaozheng7758/article/details/8623530

  • 相关阅读:
    读书笔记之复盘
    读书笔记之大话设计模式
    代码重构之重命名
    代码重构之下降方法
    代码重构之提升方法
    代码重构之移动方法
    代码重构之封装集合
    JSON之实体对象转JSON字符串,字段名全部转为小写
    SQL代码之多组织数据汇总成树形表格展示
    Python socket学习笔记(三)
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6767151.html
Copyright © 2020-2023  润新知