Friday, 10 August 2018

jcmd: Get the statistics about the Java heap usage

jcmd provides 'GC.class_histogram' command, it return statistics about the Java heap usage.

This command take one option '-all', when you specify this option, jcmd inspect all objects, including unreachable objects.

Syntax
jcmd {PROCESS_ID} GC.class_histogram -all=true

HelloWorld.java
import java.util.ArrayList;
import java.util.List;

public class HelloWorld {
 public static void main(String args[]) throws Exception {
  Thread t1 = new Thread() {
   public void run() {
    while (true) {
     List<Integer> list = new ArrayList<>();

     for (int i = 0; i < 10000000; i++) {
      list.add(i);
     }
     try {
      Thread.sleep(5000);
     } catch (Exception e) {

     }
    }
   }
  };

  t1.start();
 }
}

Compile and run HelloWorld.java application.

Open command prompt and execute 'jcmd' command.

C:\>jcmd
11748 sun.tools.jcmd.JCmd
13220
16388 HelloWorld

As you see above output, application HelloWorld is running with process id 16388.


Execute the statement 'jcmd 16388 help' to get all the available command with the process id 16388.


As you see above image, 'GC.class_histogram' command is available with the process id 16388.

Execute the command 'jcmd 16388 GC.class_histogram > heapStats'.

Open heapStats file, you can see below kind of output.

16388:

 num     #instances         #bytes  class name
----------------------------------------------
   1:          1094         110008  [C
   2:           468          53544  java.lang.Class
   3:          1077          25848  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:            59           1888  java.util.concurrent.ConcurrentHashMap$Node
  14:            38           1824  sun.util.locale.LocaleObjectCache$CacheEntry
  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:            43            688  java.lang.Object
  27:            14            672  java.util.HashMap
  28:            12            672  sun.misc.URLClassPath$JarLoader
  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:            10            288  [Ljava.io.ObjectStreamField;
  46:            12            288  sun.misc.MetaIndex
  47:             3            240  [Ljava.util.WeakHashMap$Entry;
  48:             7            224  java.lang.ref.ReferenceQueue
  49:             5            200  java.util.WeakHashMap$Entry
  50:             4            192  java.util.Hashtable
  51:             6            192  java.util.Vector
  52:             4            160  java.lang.ref.Finalizer
  53:             8            144  [Ljava.lang.Class;
  54:             9            144  java.lang.ref.ReferenceQueue$Lock
  55:             6            144  java.util.ArrayList
  56:             3            144  java.util.WeakHashMap
  57:             6            144  sun.misc.PerfCounter
  58:             2            128  java.io.ExpiringCache$1
  59:             2            128  sun.nio.cs.SingleByte$Encoder
  60:             5            120  [Ljava.lang.reflect.Constructor;
  61:             3            120  java.io.FileDescriptor
  62:             3            120  java.security.ProtectionDomain
  63:             5            120  sun.reflect.NativeConstructorAccessorImpl
  64:             2             96  java.lang.ThreadGroup
  65:             2             96  java.nio.HeapByteBuffer
  66:             3             96  java.security.CodeSource
  67:             2             96  java.util.Properties
  68:             3             96  java.util.Stack
  69:             1             96  sun.misc.Launcher$AppClassLoader
  70:             2             96  sun.misc.URLClassPath
  71:             2             96  sun.nio.cs.StreamEncoder
  72:             1             88  java.lang.reflect.Method
  73:             1             88  sun.misc.Launcher$ExtClassLoader
  74:             2             80  java.io.BufferedWriter
  75:             2             80  java.io.ExpiringCache
  76:             5             80  java.lang.ThreadLocal
  77:             5             80  sun.reflect.DelegatingConstructorAccessorImpl
  78:             3             72  java.lang.RuntimePermission
  79:             3             72  java.util.Collections$SynchronizedSet
  80:             3             72  java.util.concurrent.atomic.AtomicLong
  81:             2             64  [Ljava.lang.Thread;
  82:             2             64  java.io.FileOutputStream
  83:             2             64  java.io.PrintStream
  84:             2             64  java.lang.ClassValue$Entry
  85:             2             64  java.lang.VirtualMachineError
  86:             2             64  java.lang.ref.ReferenceQueue$Null
  87:             1             48  [J
  88:             3             48  [Ljava.security.Principal;
  89:             2             48  java.io.BufferedOutputStream
  90:             2             48  java.io.File$PathStatus
  91:             2             48  java.io.OutputStreamWriter
  92:             2             48  java.nio.charset.CoderResult
  93:             3             48  java.nio.charset.CodingErrorAction
  94:             3             48  java.security.ProtectionDomain$Key
  95:             2             48  sun.misc.NativeSignalHandler
  96:             2             48  sun.misc.Signal
  97:             1             40  java.io.BufferedInputStream
  98:             1             40  java.lang.ClassLoader$NativeLibrary
  99:             1             40  sun.nio.cs.StandardCharsets$Aliases
 100:             1             40  sun.nio.cs.StandardCharsets$Cache
 101:             1             40  sun.nio.cs.StandardCharsets$Classes
 102:             1             32  [Ljava.lang.OutOfMemoryError;
 103:             2             32  [Ljava.lang.StackTraceElement;
 104:             1             32  [Ljava.lang.ThreadGroup;
 105:             1             32  java.io.FileInputStream
 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          4597         296096





Previous                                                 Next                                                 Home

No comments:

Post a Comment