Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track when class is loaded and destroyed in jvm?

Tags:

How do I keep track when class is loaded and destroyed in jvm? Is there any callback method that is exposed by the jvm?

like image 690
Jyotirup Avatar asked Mar 29 '12 07:03

Jyotirup


People also ask

How a class is loaded within JVM?

In order to actually load a class, the JVM uses Classloader objects. Every already loaded class contains a reference to its class loader, and that class loader is used to load all the classes referenced from that class.

Which classes are loaded when JVM starts?

Initially when a JVM starts up, nothing is loaded into it. The class file of the program being executed is loaded first and then other classes and interfaces are loaded as they get referenced in the bytecode being executed.

What is ClassLoader in JVM?

The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren't loaded into memory all at once, but when required by an application.

Which component of JVM is responsible for loading a class?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.


1 Answers

If you're using a Sun/Oracle JVM, you could use the TraceClassLoading and TraceClassUnloading options. Use the following to see what options your JVM supports:

java -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal -version 

If these options are supported, run your Java application using -XX:+TraceClassLoading -XX:+TraceClassUnloading. You should see messages like:

[Loaded ... from ...] [Unloading class ...] 
like image 51
Dan Cruz Avatar answered Oct 20 '22 21:10

Dan Cruz