Sunday, October 2, 2022
HomeJavaExamine the contents of the Java Metaspace area

Examine the contents of the Java Metaspace area


Following is the pattern output of a java program when ‘-Xlog:class+load’ argument is handed:

[0.004s][info][class,load] opened: /dwelling/ec2-user/jdk-9.0.4/lib/modules

[0.006s][info][class,load] java.lang.Object supply: jrt:/java.base

[0.007s][info][class,load] java.io.Serializable supply: jrt:/java.base

[0.007s][info][class,load] java.lang.Comparable supply: jrt:/java.base

[0.007s][info][class,load] java.lang.CharSequence supply: jrt:/java.base

[0.007s][info][class,load] java.lang.String supply: jrt:/java.base

[0.007s][info][class,load] java.lang.replicate.AnnotatedElement supply: jrt:/java.base

[0.007s][info][class,load] java.lang.replicate.GenericDeclaration supply: jrt:/java.base

[0.007s][info][class,load] java.lang.replicate.Kind supply: jrt:/java.base

[0.008s][info][class,load] java.lang.Class supply: jrt:/java.base

[0.008s][info][class,load] java.lang.Cloneable supply: jrt:/java.base

[0.008s][info][class,load] java.lang.ClassLoader supply: jrt:/java.base

[0.008s][info][class,load] java.lang.System supply: jrt:/java.base

[0.008s][info][class,load] java.lang.Throwable supply: jrt:/java.base

[0.008s][info][class,load] java.lang.Error supply: jrt:/java.base

[0.008s][info][class,load] java.lang.ThreadDeath supply: jrt:/java.base

[0.008s][info][class,load] java.lang.Exception supply: jrt:/java.base

[0.008s][info][class,load] java.lang.RuntimeException supply: jrt:/java.base

[0.008s][info][class,load] java.lang.SecurityManager supply: jrt:/java.base

[0.008s][info][class,load] java.safety.ProtectionDomain supply: jrt:/java.base

[0.009s][info][class,load] java.safety.AccessControlContext supply: jrt:/java.base

[0.009s][info][class,load] java.safety.SecureClassLoader supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ReflectiveOperationException supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ClassNotFoundException supply: jrt:/java.base

[0.009s][info][class,load] java.lang.LinkageError supply: jrt:/java.base

[0.009s][info][class,load] java.lang.NoClassDefFoundError supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ClassCastException supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ArrayStoreException supply: jrt:/java.base

[0.009s][info][class,load] java.lang.VirtualMachineError supply: jrt:/java.base

[0.009s][info][class,load] java.lang.OutOfMemoryError supply: jrt:/java.base

[0.009s][info][class,load] java.lang.StackOverflowError supply: jrt:/java.base

[0.009s][info][class,load] java.lang.IllegalMonitorStateException supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ref.Reference supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ref.SoftReference supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ref.WeakReference supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ref.FinalReference supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ref.PhantomReference supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ref.Finalizer supply: jrt:/java.base

[0.009s][info][class,load] java.lang.Runnable supply: jrt:/java.base

[0.009s][info][class,load] java.lang.Thread supply: jrt:/java.base

[0.009s][info][class,load] java.lang.Thread$UncaughtExceptionHandler supply: jrt:/java.base

[0.009s][info][class,load] java.lang.ThreadGroup supply: jrt:/java.base

[0.010s][info][class,load] java.util.Map supply: jrt:/java.base

[0.010s][info][class,load] java.util.Dictionary supply: jrt:/java.base

[0.010s][info][class,load] java.util.Hashtable supply: jrt:/java.base

[0.010s][info][class,load] java.util.Properties supply: jrt:/java.base

[0.010s][info][class,load] java.lang.Module supply: jrt:/java.base

[0.010s][info][class,load] java.lang.replicate.AccessibleObject supply: jrt:/java.base

3. jcmd GC.class_histogram

JDK accommodates a device known as ‘jcmd’. You may invoke this device when JVM is operating to examine the Contents of the Metaspace area. While you invoke this device with ‘GC.class_histogram’ argument, it would print the checklist of lessons which are loaded into the reminiscence.  You may invoke this device in two modes:

a. Print loaded lessons on the console

jcmd {pid} GC.class_histogram

While you invoke the ‘jcmd’ as proven above it would print all of the loaded lessons within the console. Right here {pid} is the method id of your java software. 

b. Print loaded lessons on a File

jcmd {pid} GC.class_histogram filename={file-path}

While you invoke the ‘jcmd’ as proven above, it would print all of the loaded lessons within the file path specified within the ‘filename’ argument. Right here {pid} is the method id of your java software.

Here’s a weblog submit which lets you establish the method id rapidly.

Following is the pattern output of the open supply BuggyApp program when ‘jcmd GC.class_histogram’ argument is handed:

jcmd 19684 GC.class_histogram

19684:

 

 num     #cases         #bytes  class title

———————————————-

   1:        143036       75523008  [Ljavassist.bytecode.ConstInfo;

   2:        718060       70032224  [C

   3:       1573553       50353696  java.util.HashMap$Node

   4:        430124       24732832  [Ljava.lang.Object;

   5:       1001290       24030960  javassist.bytecode.Utf8Info

   6:        858268       20598432  java.util.ArrayList

   7:        718037       17232888  java.lang.String

   8:        144011       14987488  java.lang.Class

   9:        143081       11447152  [Ljava.util.HashMap$Node;

  10:        143036        9154304  javassist.bytecode.ClassFile

  11:        143035        9154240  javassist.CtNewClass

  12:        286124        6892400  [B

  13:        143085        6868080  java.util.HashMap

  14:        286078        6865872  javassist.bytecode.ClassInfo

  15:        143036        6865728  [[Ljavassist.bytecode.ConstInfo;

  16:        143049        5721960  javassist.bytecode.MethodInfo

  17:        143042        5721680  javassist.bytecode.CodeAttribute

  18:        143323        4586336  java.util.Hashtable$Entry

  19:        143038        4577216  java.lang.ref.WeakReference

  20:        143036        4577152  javassist.bytecode.ConstPool

  21:        143045        3433080  javassist.bytecode.MethodrefInfo

  22:        143045        3433080  javassist.bytecode.NameAndTypeInfo

  23:        143042        3433008  javassist.bytecode.ExceptionTable

  24:        143036        3432864  javassist.bytecode.LongVector

  25:        143036        3432864  javassist.bytecode.SourceFileAttribute

  26:        143622        2323336  [I

  27:            10         788688  [Ljava.util.Hashtable$Entry;

  28:           642          20544  java.util.concurrent.ConcurrentHashMap$Node

  29:           244          13664  java.lang.invoke.MemberName

  30:           341          10912  sun.misc.FDBigInteger

  31:           212           8480  java.lang.ref.SoftReference

  32:           140           8400  [Ljava.lang.ref.SoftReference;

  33:           234           7488  java.lang.invoke.LambdaForm$Name

  34:           176           7040  java.lang.invoke.MethodType

  35:           256           6144  java.lang.Long

  36:            16           6016  java.lang.Thread

  37:           173           5880  [Ljava.lang.Class;

  38:           366           5856  java.lang.Object

  39:           177           5664  java.lang.invoke.MethodType$ConcurrentWeakInternSet$WeakEntry

  40:            10           5280  [Ljava.util.concurrent.ConcurrentHashMap$Node;

  41:           256           4096  java.lang.Byte

  42:           256           4096  java.lang.Integer

  43:           256           4096  java.lang.Short

  44:            73           4088  java.lang.invoke.MethodTypeForm

  45:            82           3808  [Ljava.lang.invoke.LambdaForm$Name;

  46:            77           3696  java.lang.invoke.LambdaForm

4. Programmatic approach

You can also use a programmatic approach to print the classes that are loaded into the memory. Open source Guava library provides APIs to print the loaded classes. Below is the code sample that leverage Guava library to print the loaded classes in the memory:

ClassPath classPath = ClassPath.from(BuggyAppLoader.class.getClassLoader());

Set<ClassInfo> classes = classPath.getAllClasses();

for(ClassInfo classInfo : classes) {

    logger.info(classInfo.getName());

}

org.apache.catalina.core.AsyncContextImpl

org.apache.catalina.core.AsyncListenerWrapper

org.apache.catalina.core.Constants

org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor

org.apache.catalina.core.ContainerBase$PrivilegedAddChild

org.apache.catalina.core.ContainerBase$StartChild

org.apache.catalina.core.ContainerBase$StartStopThreadFactory

org.apache.catalina.core.ContainerBase$StopChild

org.apache.catalina.core.ContainerBase

org.apache.catalina.core.DefaultInstanceManager$1

org.apache.catalina.core.DefaultInstanceManager$2

org.apache.catalina.core.DefaultInstanceManager$3

org.apache.catalina.core.DefaultInstanceManager$AnnotationCacheEntry

org.apache.catalina.core.DefaultInstanceManager$AnnotationCacheEntryType

org.apache.catalina.core.DefaultInstanceManager

org.apache.catalina.core.JreMemoryLeakPreventionListener

org.apache.catalina.core.NamingContextListener

org.apache.catalina.core.StandardContext$1

org.apache.catalina.core.StandardContext$ContextFilterMaps

org.apache.catalina.core.StandardContext$NoPluggabilityServletContext

org.apache.catalina.core.StandardContext

org.apache.catalina.core.StandardContextValve

org.apache.catalina.core.StandardEngine$AccessLogListener

org.apache.catalina.core.StandardEngine$NoopAccessLog

org.apache.catalina.core.StandardEngine

org.apache.catalina.core.StandardEngineValve

org.apache.catalina.core.StandardHost$1

org.apache.catalina.core.StandardHost$MemoryLeakTrackingListener

org.apache.catalina.core.StandardHost

org.apache.catalina.core.StandardHostValve

org.apache.catalina.core.StandardPipeline

org.apache.catalina.core.StandardServer

org.apache.catalina.core.StandardService

org.apache.catalina.core.StandardThreadExecutor

org.apache.catalina.core.StandardWrapper

org.apache.catalina.core.StandardWrapperFacade

org.apache.catalina.core.StandardWrapperValve

org.apache.catalina.core.ThreadLocalLeakPreventionListener

5. Heap Dump analysis

Another option to see the classes that are loaded into memory is to inspect Heap Dump. Heap dump reports all the data, objects, classes that are loaded into memory. You can use one of the approaches given here to capture the heap dump. Once a heap dump is captured, you can use the heap dump analysis tools such as Eclipse MAT, HeapHero,… to analyze the heap dump.

Below is the excerpt from the report generated by the HeapHero tool that shows the classes that are loaded into the memory.

Oracle Java, Java Tutorial and Materials, Oracle Java Career, Oracle Java Skills, Oracle Java Jobs, Core Java, Oracle Java Prep, Oracle Java Preparation, Oracle Java News

Note: All the approaches mentioned above will not add noticeable overhead to your application, however the heap dump approach is an intrusive option and it will add considerable overhead to your application. When heap dump is captured your application will be paused until capturing is complete.

Source: javacodegeeks.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments