In Java is Permanent Generation space garbage collected?

Solution 1:

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.

Solution 2:

In current generation JVMs, 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 have incorrectly attributed "OutOfMemoryError : permgen" exceptions to permgen not being collected at all. The reality is different. The most common cause of these 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.


UPDATE

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