Tuesday, July 8, 2014

Garbage Collection in PermGen

This article tries to answer below questions:
  • Will garbage collection happen in PermGen in Java? 
  • What parameters control this behavior?  
  • How to tune PermGen size?


1. Firstly we need to understand the minor GC and major GC concept as shown below.
When the young generation fills up, a young generation collection (sometimes referred to as a minor collection) of just that generation is performed. When the old or permanent generation fills up, what is known as a full collection (sometimes referred to as a major collection) is typically done. That is, all generations are collected. Commonly, the young generation is collected first, using the collection algorithm designed specifically for that generation, because it is usually the most efficient algorithm for identifying garbage in the young generation. Then what is referred to below as the old generation collection algorithm for a given collector is run on both the old and permanent generations. If compaction occurs, each generation is compacted separately.
So the answer is yes, GC can happen in PermGen and it is counted as major/full GC.
We can use "jstat" to monitor the GC activity.

2. It depends on JDK versions.
In JDK before Java SE 6 Update 3 or earlier, "-XX:+CMSPermGenSweepingEnabled" is used to enable GC in PermGen. By default, GC will never happen in PermGen.

In JDK 7, "-XX:+CMSPermGenSweepingEnabled" is no longer used because GC by default will happen in PermGen. However another parameter "-XX:+CMSClassUnloadingEnabled" is introduced to tell the PermGen GC sweep to take action on class objects. By default, class objects get an exemption, even when the PermGen space is being visited during a garabage collection.

In JDK 8, PermGen no longer exists.

3. -Xmx (the maximum heap size) does NOT include the PermGen.
-XX:MaxPermSize controls the maximum memory used by the permanent generation.
-XX:PermSize controls the initial memory used by the permanent generation.

No comments:

Post a Comment

Popular Posts