Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Unloading in Java's G1 Garbage Collector (G1GC)

In Java 6 we used to use the following GC configuration to prevent Perm Gen OutOfMemoryException after several redeployments of our app:

-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled

We're moving to Java 7 and want to use the new G1 GC, which from what I've read, moves the classes from the PermGen in Java memory to native memory.

Is there some flag to enable unloading unused classes?

like image 871
Andres Olarte Avatar asked Sep 23 '13 13:09

Andres Olarte


Video Answer


2 Answers

The G1 performs the class unloading during a Full GC, so you do not need to specify any parameters to enable this.

You can see for yourself by using the -XX:+TraceClassUnloading argument.

Also, check out this email thread from the HotSpot GC mailing list: Bug in G1GC it performs Full GC when code cache is full resulting in overkill. They discuss class unloading in G1 quite extensively. In summary, you can use -noclassgc if you are seeing issues with class unloading but usually there are no problems with class unloading in G1.

like image 170
Aleš Avatar answered Oct 19 '22 23:10

Aleš


G1 performs class unloading during the Remark phase, which is stop-the-world:

[GC remark 2019-03-26T14:27:52.926+0000: 18.798: [Finalize Marking, 0.0004509 secs] 2019-03-26T14:27:52.926+0000: 18.799: [GC ref-proc, 0.0002791 secs] 2019-03-26T14:27:52.926+0000: 18.799: [Unloading, 0.0058844 secs], 0.0073053 secs]

Note that it is Java 8 that replaces Permgen with Metaspace, and that CMS does class unloading too (with the switch CMSClassUnloadingEnabled), so if you still have Out of Memory errors, it won't help.

like image 32
Ayoross Avatar answered Oct 19 '22 22:10

Ayoross