问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/18960493/class-unloading-in-javas-g1-garbage-collector-g1gc