The option “-XX:[+|-]CMSClassUnloadingEnabled” enables class unloading when using the Concurrent Mark Sweep (CMS) Garbage Collector (GC).
Starting from JDK 6.
- Obsoleted in JDK14.
- Expired in JDK15.
java -XX:[+|-]CMSClassUnloadingEnabled MainClass
- To disable class unloading for the CMS garbage collector: java -XX:-CMSClassUnloadingEnabled MainClass.
- To disable class unloading for the CMS garbage collector: java -XX:+CMSClassUnloadingEnabled MainClass.
Before talking about the flag, we need to introduce Garbage Collector (GC) and Concurrent Mark Sweep (CMS):
Garbage Collector is a way of the JVM to clean the memory and makes the java memory more efficient. It tracks (Mark step) and removes (sweep step) every unused object available in the JVM heap space. An object is considered unused when it’s referenced by null, assigned to another reference or by anonymous object …. There are 5 type of GC implementations:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
- Z Garbage Collector
CMS Garbage Collector, as mentioned above, is one of the implementations of GC, which uses multiple thread for garbage collection. It’s created for applications that favor shorter garbage collection pauses and that can afford to share processor resources with the garbage collector while the application is running. It achieves long pauses by two ways:
- By using free lists to manage reclaimed space instead of compacting the Old Generation.
- By doing mainly the job in Mark (marking a reference) and Sweep (removing the marked reference) process simultaneously with the application. Which means that the GC is not stopping or freezing the application’s threads to do its job. But it does take part of using the CPU time with the application. By default, GC algorithm can use number of threads up to ¼ of the number of physical cores of your machine.
In contrast to the Throughput Collector, the CMS Collector does not perform GC in the permanent generation by default. If permanent generation GC is desired, it can be enabled via “-XX:+CMSClassUnloadingEnabled”. In earlier JVM versions, it may be required to additionally set the flag “-XX:+CMSPermGenSweepingEnabled”. Note that, even if this flag is not set, there will be an attempt to garbage-collect permanent generation once it runs out of space, but the collection will not be concurrent – instead, once again, a full GC will be run.
This option is enabled by default.
CMSInitiatingOccupancyFraction, CMSScavengeBeforeRemark, CMSScavengeBeforeRemark, CMSIncrementalSafetyFactor, CMSPermGenSweepingEnabled, CMSIncrementalMode, UseConcMarkSweepGC and UseCMSInitiatingOccupancyOnly.