Thursday, 16 August 2018

jmap: get heap histogram

'jmap' provides '-histo[:live]' option to retrieve the histogram of a java object heap.

Syntax
jmap -histo {PROCESS_ID}
jmap -histo:live {PROCESS_ID}

if the "live" suboption is specified, jmap only count live objects.

Let us see with an example.

HelloWorld.java
public class HelloWorld {
 public static void main(String args[]) throws Exception {
  int count = 0;
  
  Thread t1 = new Thread() {
   public void run() {
    while(true){
     try{
      Thread.sleep(2000);
     }catch(Exception e){
      
     }
    }
   }
  };

  t1.start();
 }
}

Compile and run HelloWorld application.

Open command prompt and execute jcmd command.

C:\>jcmd
18484 HelloWorld
18612
23208 sun.tools.jcmd.JCmd

As you see the output of jcmd command, HelloWorld application is running with process id 18612.

Execute the command ‘jmap -histo 18484 > histo.txt’.  This command copies the heap histogram to histo.txt.


‘histo.txt’ file contains below contents.

 num     #instances         #bytes  class name
----------------------------------------------
   1:           416        3597256  [I
   2:          1891         208384  [C
   3:           468          53544  java.lang.Class
   4:            31          35512  [B
   5:          1436          34464  java.lang.String
   6:           528          26872  [Ljava.lang.Object;
   7:           115           8280  java.lang.reflect.Field
   8:           258           4128  java.lang.Integer
   9:           124           3968  java.util.Hashtable$Entry
  10:            96           3840  java.lang.ref.SoftReference
  11:           141           3384  java.lang.StringBuilder
  12:             8           3008  java.lang.Thread
  13:            79           2960  [Ljava.lang.String;
  14:            72           2304  java.io.File
  15:            29           1856  java.net.URL
  16:            38           1824  sun.util.locale.LocaleObjectCache$CacheEntry
  17:            12           1760  [Ljava.util.Hashtable$Entry;
  18:            53           1696  java.util.concurrent.ConcurrentHashMap$Node
  19:            16           1280  [S
  20:            34           1088  java.util.HashMap$Node
  21:             2           1064  [Ljava.lang.invoke.MethodHandle;
  22:             1           1040  [Ljava.lang.Integer;
  23:            26           1040  java.io.ObjectStreamField
  24:            12           1024  [Ljava.util.HashMap$Node;
  25:            10            800  java.lang.reflect.Constructor
  26:            20            800  java.util.LinkedHashMap$Entry
  27:            20            800  sun.util.locale.BaseLocale$Key
  28:            14            784  java.lang.Class$ReflectionData
  29:            31            744  java.lang.StringBuffer
  30:            15            720  java.nio.HeapByteBuffer
  31:            15            720  java.util.HashMap
  32:            42            672  java.lang.Object
  33:            14            672  java.nio.HeapCharBuffer
  34:            12            672  sun.misc.URLClassPath$JarLoader
  35:            19            608  java.util.Locale
  36:            19            608  sun.util.locale.BaseLocale
  37:            25            600  java.net.Parts
  38:             9            560  [Ljava.lang.reflect.Field;
  39:             5            528  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  40:            13            520  java.security.AccessControlContext
  41:            20            480  java.io.ExpiringCache$Entry
  42:            20            480  java.util.Locale$LocaleKey
  43:            20            480  sun.security.action.GetPropertyAction
  44:             1            384  java.lang.ref.Finalizer$FinalizerThread
  45:             6            384  java.nio.DirectByteBuffer
  46:             6            384  java.util.concurrent.ConcurrentHashMap
  47:             1            376  HelloWorld$1
  48:             1            376  java.lang.ref.Reference$ReferenceHandler
  49:            20            352  [Ljava.lang.Class;
  50:             6            336  java.nio.DirectLongBufferU
  51:            10            320  java.lang.OutOfMemoryError
  52:             8            320  java.lang.ref.Finalizer
  53:            13            312  sun.misc.URLClassPath$3
  54:            10            288  [Ljava.io.ObjectStreamField;
  55:            12            288  sun.misc.MetaIndex
  56:             7            280  java.io.FileDescriptor
  57:             3            240  [Ljava.util.WeakHashMap$Entry;
  58:             7            224  java.lang.ref.ReferenceQueue
  59:             7            224  java.util.Vector
  60:             5            200  java.util.WeakHashMap$Entry
  61:             4            192  java.util.Hashtable
  62:             2            176  java.lang.reflect.Method
  63:             7            168  java.util.ArrayList
  64:             9            144  java.lang.ref.ReferenceQueue$Lock
  65:             3            144  java.util.WeakHashMap
  66:             6            144  sun.misc.PerfCounter
  67:             2            128  java.io.ExpiringCache$1
  68:             4            128  java.io.FileInputStream
  69:             4            128  java.security.CodeSource
  70:             2            128  sun.nio.cs.SingleByte$Encoder
  71:             5            120  [Ljava.lang.reflect.Constructor;
  72:             5            120  java.lang.Class$1
  73:             3            120  java.security.ProtectionDomain
  74:             5            120  sun.reflect.NativeConstructorAccessorImpl
  75:             5            120  sun.usagetracker.UsageTrackerClient$1
  76:             1            104  sun.net.www.protocol.file.FileURLConnection
  77:             3             96  java.io.FileOutputStream
  78:             2             96  java.lang.ThreadGroup
  79:             3             96  java.lang.ThreadLocal$ThreadLocalMap$Entry
  80:             4             96  java.net.URLClassLoader$1
  81:             2             96  java.util.Properties
  82:             3             96  java.util.Stack
  83:             2             96  java.util.StringTokenizer
  84:             1             96  sun.misc.Launcher$AppClassLoader
  85:             2             96  sun.misc.URLClassPath
  86:             2             96  sun.nio.cs.SingleByte$Decoder
  87:             2             96  sun.nio.cs.StreamEncoder
  88:             2             88  [Ljava.net.URL;
  89:             1             88  sun.misc.Launcher$ExtClassLoader
  90:             1             80  [Ljava.lang.ThreadLocal$ThreadLocalMap$Entry;
  91:             2             80  java.io.BufferedWriter
  92:             2             80  java.io.ExpiringCache
  93:             5             80  java.lang.Class$3
  94:             2             80  java.lang.ClassNotFoundException
  95:             5             80  java.lang.ThreadLocal
  96:             5             80  sun.reflect.DelegatingConstructorAccessorImpl
  97:             3             72  java.lang.RuntimePermission
  98:             3             72  java.util.Collections$SynchronizedSet
  99:             3             72  java.util.concurrent.atomic.AtomicLong
 100:             2             64  [Ljava.lang.Thread;
 101:             2             64  java.io.FilePermission
 102:             2             64  java.io.PrintStream
 103:             2             64  java.lang.ClassValue$Entry
 104:             2             64  java.lang.NoSuchMethodError
 105:             2             64  java.lang.VirtualMachineError
 106:             2             64  java.lang.ref.ReferenceQueue$Null
 107:             2             64  sun.misc.URLClassPath$FileLoader$1
 108:             1             56  sun.nio.cs.UTF_8$Encoder
 109:             1             48  [J
 110:             2             48  [Ljava.io.File;
 111:             2             48  [Ljava.lang.reflect.Method;
 112:             3             48  [Ljava.security.Principal;
 113:             2             48  java.io.BufferedOutputStream
 114:             1             48  java.io.BufferedReader
 115:             2             48  java.io.File$PathStatus
 116:             3             48  java.io.FileInputStream$1
 117:             2             48  java.io.OutputStreamWriter
 118:             2             48  java.nio.charset.CoderResult
 119:             3             48  java.nio.charset.CodingErrorAction
 120:             3             48  java.security.ProtectionDomain$Key
 121:             1             48  java.util.Hashtable$Enumerator
 122:             2             48  sun.misc.NativeSignalHandler
 123:             2             48  sun.misc.Signal
 124:             1             48  sun.nio.cs.StreamDecoder
 125:             3             48  sun.reflect.ReflectionFactory$GetReflectionFactoryAction
 126:             1             40  java.io.BufferedInputStream
 127:             1             40  java.lang.ClassLoader$NativeLibrary
 128:             1             40  sun.nio.cs.StandardCharsets$Aliases
 129:             1             40  sun.nio.cs.StandardCharsets$Cache
 130:             1             40  sun.nio.cs.StandardCharsets$Classes
 131:             1             40  sun.nio.cs.UTF_8$Decoder
 132:             1             32  [Ljava.lang.OutOfMemoryError;
 133:             2             32  [Ljava.lang.StackTraceElement;
 134:             1             32  [Ljava.lang.ThreadGroup;
 135:             2             32  java.io.FilePermission$1
 136:             1             32  java.io.WinNTFileSystem
 137:             1             32  java.lang.ArithmeticException
 138:             2             32  java.lang.Boolean
 139:             1             32  java.lang.NullPointerException
 140:             1             32  java.lang.StringCoding$StringDecoder
 141:             1             32  java.lang.StringCoding$StringEncoder
 142:             2             32  java.nio.ByteOrder
 143:             1             32  java.security.BasicPermissionCollection
 144:             1             32  java.security.Permissions
 145:             2             32  java.util.HashSet
 146:             2             32  java.util.concurrent.atomic.AtomicInteger
 147:             1             32  java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl
 148:             2             32  sun.net.www.protocol.jar.Handler
 149:             1             32  sun.nio.cs.StandardCharsets
 150:             2             32  sun.reflect.ReflectionFactory$1
 151:             2             32  sun.usagetracker.UsageTrackerClient$2
 152:             2             32  sun.usagetracker.UsageTrackerClient$3
 153:             1             24  [Ljava.io.File$PathStatus;
 154:             1             24  [Ljava.lang.ClassValue$Entry;
 155:             1             24  [Ljava.security.ProtectionDomain;
 156:             1             24  [Lsun.launcher.LauncherHelper;
 157:             1             24  java.io.FilePermissionCollection
 158:             1             24  java.io.FileReader
 159:             1             24  java.lang.Class$MethodArray
 160:             1             24  java.lang.ClassValue$Version
 161:             1             24  java.lang.ThreadLocal$ThreadLocalMap
 162:             1             24  java.lang.invoke.MethodHandleImpl$4
 163:             1             24  java.lang.reflect.ReflectPermission
 164:             1             24  java.util.BitSet
 165:             1             24  java.util.Collections$EmptyMap
 166:             1             24  java.util.Collections$SetFromMap
 167:             1             24  java.util.Collections$UnmodifiableRandomAccessList
 168:             1             24  java.util.Locale$Cache
 169:             1             24  java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl$1
 170:             1             24  sun.launcher.LauncherHelper
 171:             1             24  sun.misc.Launcher$AppClassLoader$1
 172:             1             24  sun.misc.URLClassPath$FileLoader
 173:             1             24  sun.net.www.MessageHeader
 174:             1             24  sun.nio.cs.IBM437
 175:             1             24  sun.nio.cs.MS1252
 176:             1             24  sun.nio.cs.ThreadLocalCoders$1
 177:             1             24  sun.nio.cs.ThreadLocalCoders$2
 178:             1             24  sun.nio.cs.UTF_8
 179:             1             24  sun.usagetracker.UsageTrackerClient$4
 180:             1             24  sun.util.locale.BaseLocale$Cache
 181:             1             16  [Ljava.lang.Throwable;
 182:             1             16  [Ljava.security.cert.Certificate;
 183:             1             16  java.io.FileDescriptor$1
 184:             1             16  java.io.FileOutputStream$1
 185:             1             16  java.lang.CharacterDataLatin1
 186:             1             16  java.lang.ClassLoader$3
 187:             1             16  java.lang.ClassValue$Identity
 188:             1             16  java.lang.Compiler$1
 189:             1             16  java.lang.Runtime
 190:             1             16  java.lang.String$CaseInsensitiveComparator
 191:             1             16  java.lang.System$2
 192:             1             16  java.lang.SystemClassLoaderAction
 193:             1             16  java.lang.Terminator$1
 194:             1             16  java.lang.invoke.MemberName$Factory
 195:             1             16  java.lang.invoke.MethodHandleImpl$1
 196:             1             16  java.lang.invoke.MethodHandleImpl$2
 197:             1             16  java.lang.invoke.MethodHandleImpl$3
 198:             1             16  java.lang.invoke.MethodHandleStatics$1
 199:             1             16  java.lang.ref.Reference$1
 200:             1             16  java.lang.ref.Reference$Lock
 201:             1             16  java.lang.reflect.ReflectAccess
 202:             1             16  java.net.URLClassLoader$7
 203:             1             16  java.nio.Bits$1
 204:             1             16  java.nio.charset.CoderResult$1
 205:             1             16  java.nio.charset.CoderResult$2
 206:             1             16  java.security.ProtectionDomain$2
 207:             1             16  java.security.ProtectionDomain$JavaSecurityAccessImpl
 208:             1             16  java.util.Collections$EmptyList
 209:             1             16  java.util.Collections$EmptySet
 210:             1             16  java.util.Hashtable$EntrySet
 211:             1             16  java.util.WeakHashMap$KeySet
 212:             1             16  java.util.concurrent.atomic.AtomicBoolean
 213:             1             16  java.util.zip.ZipFile$1
 214:             1             16  sun.misc.Launcher
 215:             1             16  sun.misc.Launcher$ExtClassLoader$1
 216:             1             16  sun.misc.Launcher$Factory
 217:             1             16  sun.misc.Perf
 218:             1             16  sun.misc.Perf$GetPerfAction
 219:             1             16  sun.misc.Unsafe
 220:             1             16  sun.net.www.protocol.file.Handler
 221:             1             16  sun.reflect.ReflectionFactory
 222:             1             16  sun.usagetracker.UsageTrackerClient
Total          6638        4026944

Execute the command ‘jmap -histo:live 18484 > histo_liveObjects.txt’, it copies live objects histogram to histo_liveObjects.txt file.

Contents of ‘histo_liveObjects.txt’ file looks like below.

num     #instances         #bytes  class name
----------------------------------------------
   1:          1076         109688  [C
   2:           468          53544  java.lang.Class
   3:          1059          25416  java.lang.String
   4:           505          25240  [Ljava.lang.Object;
   5:             7          24960  [B
   6:            79           5688  java.lang.reflect.Field
   7:           256           4096  java.lang.Integer
   8:           117           3744  java.util.Hashtable$Entry
   9:            90           3600  java.lang.ref.SoftReference
  10:            85           2936  [I
  11:             7           2632  java.lang.Thread
  12:            61           2504  [Ljava.lang.String;
  13:            38           1824  sun.util.locale.LocaleObjectCache$CacheEntry
  14:            53           1696  java.util.concurrent.ConcurrentHashMap$Node
  15:            25           1600  java.net.URL
  16:            34           1088  java.util.HashMap$Node
  17:             2           1064  [Ljava.lang.invoke.MethodHandle;
  18:             1           1040  [Ljava.lang.Integer;
  19:            26           1040  java.io.ObjectStreamField
  20:             6            992  [Ljava.util.Hashtable$Entry;
  21:            11            944  [Ljava.util.HashMap$Node;
  22:            10            800  java.lang.reflect.Constructor
  23:            20            800  java.util.LinkedHashMap$Entry
  24:            14            784  java.lang.Class$ReflectionData
  25:            19            760  sun.util.locale.BaseLocale$Key
  26:            14            672  java.util.HashMap
  27:            12            672  sun.misc.URLClassPath$JarLoader
  28:            41            656  java.lang.Object
  29:             8            640  [S
  30:            19            608  java.util.Locale
  31:            19            608  sun.util.locale.BaseLocale
  32:             5            528  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  33:            20            480  java.io.ExpiringCache$Entry
  34:            12            480  java.security.AccessControlContext
  35:             8            456  [Ljava.lang.reflect.Field;
  36:            19            456  java.util.Locale$LocaleKey
  37:            13            416  java.io.File
  38:             1            384  java.lang.ref.Finalizer$FinalizerThread
  39:             6            384  java.nio.DirectByteBuffer
  40:             6            384  java.util.concurrent.ConcurrentHashMap
  41:             1            376  HelloWorld$1
  42:             1            376  java.lang.ref.Reference$ReferenceHandler
  43:             6            336  java.nio.DirectLongBufferU
  44:            10            320  java.lang.OutOfMemoryError
  45:             8            320  java.lang.ref.Finalizer
  46:            10            288  [Ljava.io.ObjectStreamField;
  47:            12            288  sun.misc.MetaIndex
  48:             7            280  java.io.FileDescriptor
  49:             3            240  [Ljava.util.WeakHashMap$Entry;
  50:             7            224  java.lang.ref.ReferenceQueue
  51:             5            200  java.util.WeakHashMap$Entry
  52:             4            192  java.util.Hashtable
  53:             6            192  java.util.Vector
  54:             8            144  [Ljava.lang.Class;
  55:             9            144  java.lang.ref.ReferenceQueue$Lock
  56:             6            144  java.util.ArrayList
  57:             3            144  java.util.WeakHashMap
  58:             6            144  sun.misc.PerfCounter
  59:             2            128  java.io.ExpiringCache$1
  60:             4            128  java.io.FileInputStream
  61:             2            128  sun.nio.cs.SingleByte$Encoder
  62:             5            120  [Ljava.lang.reflect.Constructor;
  63:             3            120  java.security.ProtectionDomain
  64:             5            120  sun.reflect.NativeConstructorAccessorImpl
  65:             3             96  java.io.FileOutputStream
  66:             2             96  java.lang.ThreadGroup
  67:             2             96  java.nio.HeapByteBuffer
  68:             3             96  java.security.CodeSource
  69:             2             96  java.util.Properties
  70:             3             96  java.util.Stack
  71:             1             96  sun.misc.Launcher$AppClassLoader
  72:             2             96  sun.misc.URLClassPath
  73:             2             96  sun.nio.cs.StreamEncoder
  74:             1             88  java.lang.reflect.Method
  75:             1             88  sun.misc.Launcher$ExtClassLoader
  76:             2             80  java.io.BufferedWriter
  77:             2             80  java.io.ExpiringCache
  78:             5             80  java.lang.ThreadLocal
  79:             5             80  sun.reflect.DelegatingConstructorAccessorImpl
  80:             3             72  java.lang.RuntimePermission
  81:             3             72  java.util.Collections$SynchronizedSet
  82:             3             72  java.util.concurrent.atomic.AtomicLong
  83:             2             64  [Ljava.lang.Thread;
  84:             2             64  java.io.PrintStream
  85:             2             64  java.lang.ClassValue$Entry
  86:             2             64  java.lang.VirtualMachineError
  87:             2             64  java.lang.ref.ReferenceQueue$Null
  88:             1             48  [J
  89:             3             48  [Ljava.security.Principal;
  90:             2             48  java.io.BufferedOutputStream
  91:             2             48  java.io.File$PathStatus
  92:             2             48  java.io.OutputStreamWriter
  93:             2             48  java.nio.charset.CoderResult
  94:             3             48  java.nio.charset.CodingErrorAction
  95:             3             48  java.security.ProtectionDomain$Key
  96:             2             48  sun.misc.NativeSignalHandler
  97:             2             48  sun.misc.Signal
  98:             1             40  java.io.BufferedInputStream
  99:             1             40  java.lang.ClassLoader$NativeLibrary
 100:             1             40  sun.nio.cs.StandardCharsets$Aliases
 101:             1             40  sun.nio.cs.StandardCharsets$Cache
 102:             1             40  sun.nio.cs.StandardCharsets$Classes
 103:             1             32  [Ljava.lang.OutOfMemoryError;
 104:             2             32  [Ljava.lang.StackTraceElement;
 105:             1             32  [Ljava.lang.ThreadGroup;
 106:             1             32  java.io.FilePermission
 107:             1             32  java.io.WinNTFileSystem
 108:             1             32  java.lang.ArithmeticException
 109:             2             32  java.lang.Boolean
 110:             1             32  java.lang.NullPointerException
 111:             2             32  java.nio.ByteOrder
 112:             1             32  java.security.BasicPermissionCollection
 113:             1             32  java.security.Permissions
 114:             2             32  java.util.HashSet
 115:             2             32  java.util.concurrent.atomic.AtomicInteger
 116:             1             32  java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl
 117:             2             32  sun.net.www.protocol.jar.Handler
 118:             1             32  sun.nio.cs.StandardCharsets
 119:             1             24  [Ljava.io.File$PathStatus;
 120:             1             24  [Ljava.lang.ClassValue$Entry;
 121:             1             24  [Ljava.lang.reflect.Method;
 122:             1             24  [Ljava.security.ProtectionDomain;
 123:             1             24  [Lsun.launcher.LauncherHelper;
 124:             1             24  java.io.FilePermissionCollection
 125:             1             24  java.lang.ClassValue$Version
 126:             1             24  java.lang.StringBuilder
 127:             1             24  java.lang.invoke.MethodHandleImpl$4
 128:             1             24  java.lang.reflect.ReflectPermission
 129:             1             24  java.util.BitSet
 130:             1             24  java.util.Collections$EmptyMap
 131:             1             24  java.util.Collections$SetFromMap
 132:             1             24  java.util.Collections$UnmodifiableRandomAccessList
 133:             1             24  java.util.Locale$Cache
 134:             1             24  sun.launcher.LauncherHelper
 135:             1             24  sun.misc.URLClassPath$FileLoader
 136:             1             24  sun.nio.cs.IBM437
 137:             1             24  sun.nio.cs.MS1252
 138:             1             24  sun.nio.cs.ThreadLocalCoders$1
 139:             1             24  sun.nio.cs.ThreadLocalCoders$2
 140:             1             24  sun.nio.cs.UTF_8
 141:             1             24  sun.util.locale.BaseLocale$Cache
 142:             1             16  [Ljava.lang.Throwable;
 143:             1             16  [Ljava.security.cert.Certificate;
 144:             1             16  java.io.FileDescriptor$1
 145:             1             16  java.lang.CharacterDataLatin1
 146:             1             16  java.lang.ClassValue$Identity
 147:             1             16  java.lang.Runtime
 148:             1             16  java.lang.String$CaseInsensitiveComparator
 149:             1             16  java.lang.System$2
 150:             1             16  java.lang.Terminator$1
 151:             1             16  java.lang.invoke.MemberName$Factory
 152:             1             16  java.lang.invoke.MethodHandleImpl$2
 153:             1             16  java.lang.invoke.MethodHandleImpl$3
 154:             1             16  java.lang.ref.Reference$1
 155:             1             16  java.lang.ref.Reference$Lock
 156:             1             16  java.lang.reflect.ReflectAccess
 157:             1             16  java.net.URLClassLoader$7
 158:             1             16  java.nio.Bits$1
 159:             1             16  java.nio.charset.CoderResult$1
 160:             1             16  java.nio.charset.CoderResult$2
 161:             1             16  java.security.ProtectionDomain$2
 162:             1             16  java.security.ProtectionDomain$JavaSecurityAccessImpl
 163:             1             16  java.util.Collections$EmptyList
 164:             1             16  java.util.Collections$EmptySet
 165:             1             16  java.util.Hashtable$EntrySet
 166:             1             16  java.util.WeakHashMap$KeySet
 167:             1             16  java.util.concurrent.atomic.AtomicBoolean
 168:             1             16  java.util.zip.ZipFile$1
 169:             1             16  sun.misc.Launcher
 170:             1             16  sun.misc.Launcher$Factory
 171:             1             16  sun.misc.Perf
 172:             1             16  sun.misc.Unsafe
 173:             1             16  sun.net.www.protocol.file.Handler
 174:             1             16  sun.reflect.ReflectionFactory
Total          4565         295568

Previous                                                 Next                                                 Home

No comments:

Post a Comment