Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java is Permanent Generation space garbage collected?

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?

like image 487
Ashish Avatar asked Sep 26 '10 02:09

Ashish


People also ask

What is permanent generation space in Java?

PermGen (Permanent Generation) is a special heap space separated from the main memory heap. The JVM keeps track of loaded class metadata in the PermGen. Additionally, the JVM stores all the static content in this memory section.

Is Java garbage collected?

Java Memory Management, with its built-in garbage collection, is one of the language's finest achievements. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse.

What is generational garbage collection in Java?

The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.

Which objects are garbage collected in Java?

Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues. When an object created in Java program is no longer reachable or used it is eligible for garbage collection.


2 Answers

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.

like image 199
Sagar V Avatar answered Sep 22 '22 21:09

Sagar V


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

like image 43
Stephen C Avatar answered Sep 20 '22 21:09

Stephen C