Unloading classes in java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, the concept of class unloading comes into play predominantly within the context of class loading and garbage collection. Java applications rely heavily on dynamically loading classes into the Java Virtual Machine (JVM) during runtime. Understanding how and when classes are unloaded is crucial, particularly for long-running applications or applications requiring dynamic reloading of classes, such as web servers and application servers.
Class Loading Basics
The class loader is a component of the JVM that is responsible for loading classes dynamically into memory. The default class loading mechanism follows the delegation model in which classes are loaded by delegating the responsibility up the hierarchy. This hierarchy typically comprises:
- Bootstrap ClassLoader: Part of the core JVM, responsible for loading core Java classes.
- Extension ClassLoader: Loads classes from the
extdirectory or any other extension directory configurations. - Application/System ClassLoader: Loads classes from the application's classpath.
Classes loaded into the JVM are stored in a region of the heap known as the metaspace.
Unloading Classes
Criteria for Class Unloading
Classes can be unloaded to reclaim memory, particularly from the metaspace. However, there are specific conditions that must be met for a class to be eligible for unloading:
- ClassLoader Unavailability: The class loader that loaded the class must be unreachable. If any instance of the class loader or its static properties is reachable, the classes won't be unloaded.
- Class Instance Unavailability: No instance of the class must be reachable. If any object of the class or its subclasses exists, the class will remain loaded.
- No Active Threads: Threads actively using the class will prevent its unloading. Any method invocation or reference by the thread keeps the class active.
Practical Example
Consider a scenario where a custom class loader is used to load specific classes dynamically. Below is a simplified example illustrating the basics of class unloading:
In this snippet, fetchClassDataFromSomeSource represents a method to obtain bytecode, possibly from a file or a network resource.
Implications and Best Practices
Metaspace Configuration
Metaspace replaced the earlier PermGen space in Java 8. It grows dynamically by default; however, to control resource usage, consider setting these JVM flags:
-XX:MetaspaceSize=N: Set the initial size of the metaspace.-XX:MaxMetaspaceSize=N: Set the maximum size of the metaspace.
Monitoring Class Unloading
Tools such as jvisualvm, jconsole, or profilers can track class loading/unloading activities. Logging relevant GC activity can also aid in understanding class unloading processes. Use:
Avoiding Memory Leaks
Carefully manage references, especially static references, to allow garbage collection and subsequent unloading of classes. Memory leaks can occur if obsolete class references persist due to poorly architected class loaders or static fields.
Summary Table
| Key Aspect | Description |
| Class Loading | Classes are loaded into a JVM dynamically during runtime. |
| Class Unloading Criteria | ClassLoader unavailability, no active objects/threads. |
| GC and Metaspace | Metaspace holds classes; unloading reclaims memory. |
| Best Practices | Control metaspace size, leverage GC logs and monitoring tools. |
Conclusion
Understanding class unloading in Java involves recognition of its dependencies on garbage collection, class loader architecture, and runtime behavior. While JVM generally manages these aspects efficiently, developers can fine-tune settings and be mindful of class loader implementations to optimize memory usage and ensure application stability.

