In Java is Permanent Generation space garbage collected?
Asked Answered
E

2

39

I have read that Perm gen (or Permanent Generation) space is not garbage collected. However, in CMS collection I can see some classes unloading in my GC log. So is perm gen garbage collected during full collection or CMS collection?

Erfert answered 26/9, 2010 at 2:56 Comment(2)
Possible duplicate of #88735 ? Anyway check that thread it will help for sure.Remembrancer
no its not duplicate at all. This is a concept question asks for Yes/No answer, not a question seeking solution for an error.Roadside
R
36

The PermGen is garbage collected like the other parts of the heap.

The thing to note here is that the PermGen contains meta-data of the classes and the objects i.e. pointers into the rest of the heap where the objects are allocated. The PermGen also contains Class-loaders which have to be manually destroyed at the end of their use else they stay in memory and also keep holding references to their objects on the heap. The "Presenting the Permanent Generation" article by Jon Masamitsu on the Sun / Oracle blog site might help you.

Remembrancer answered 26/9, 2010 at 3:27 Comment(5)
Corrected. It was a Sun blog so it is now an Oracle blog :)Remembrancer
I've inlined the title and author so that people can find it if the link breaks again.Idea
Permgen is not part of heapWolsey
People are always mess up with native heap and java heap. PermGen is in native heap within java process. I think by using the term heap they sometimes mean java and native heap.Zayas
PermGen also has String Pool.Stab
I
31

In recent JVMs prior to Java 8, permgen is indeed collected like other parts of the heap. The visualgc page states that it is collected together with the old generation.

In older JVMs this was apparently not always so. For instance, in Java 5 the CMS collector apparently did not collect permGen by default: you could enable it with -XX:+CMSPermGenSweepingEnabled. I also recall hearing that some really old JVMs did not implement permgen collection at all, though I cannot find a reliable source for this ... ermm ... "factoid".

The other point, is that a lot of people used to incorrectly attribute "OutOfMemoryError : permgen" exceptions to permgen not being collected at all. The reality is different. The most common cause of permgen OOME's is an insidious kind of storage leak that manifests when you hot-load code into an executing JVM. The leak occurs because when an instance of some old class that has been replaced remains reachable. This causes the object's class to be reachable, which causes the classes classloader to be reachable, which causes all of the old classes to be reachable, together with their code objects, their string literals, and their static frames and static. A lot of these leaked objects live in permgen space.


As of Java 8, permgen no longer exists: PermGen elimination in JDK 8

Idea answered 22/3, 2011 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.