Java - PermGen space vs Heap space | JavaInUse



Java - PermGen space vs Heap space


JVM memory is divided into separate parts as follows-

Java Heap space:

Java objects are instantiations of Java classes. Our JVM has an internal representation of those Java objects and those internal representations are stored in the heap. This Java heap memory is divided again into regions, called generations.
  • Young Generation : It is place where lived for short period and divided into two spaces:
    • Eden Space : When object created using new keyword memory allocated on this space. Newly created objects are usually located in this space. When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to the survivor spaces.
    • Survivor Space : This is the pool which contains objects which have survived after java garbage collection from Eden space.
  • Tenured Generation : This pool is basically contain tenured and virtual (reserved) space and will be holding those objects which survived after garbage collection from Young Generation. This memory pool contains objects which survived after multiple garbage collection means object which survived after garbage collection from Survivor space.

Java PermGen space:

JVM also has an internal representation of the Java classes and those are stored in the permanent generation. Class definitions are stored here, as are static instances. The PermGen is garbage collected like the other parts of the heap.
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. PermGen always has a fixed maximum size.PerGen space cannot be made to auto increase so it is difficult to tune it. The GC here is not that efficient so we get Out of Memory : Permgen space error quite a few times.
Next we will create a simple java project to generate a PermGen space error.

Lets Begin

Lets create a java project to generate the Permgen space error and analyze it-
Create the class TestMemory as follows-
package com.javainuse;

public class TestMemory {

	static int i = 0;

	public static void main(String[] args) {

		if (i < 25) {
			i++;
			System.out.println(i);
			main(new String[] { (args[0] + args[0]).intern() });
		}

	}

}

Click on run as configuration and to the arguments add the following.
-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:C:/log/gc.log
This will redirect verbose garbage collection output to a file gc.log.

On running the application we get the following-

Go to C:/log/gc.log we see the following



See Also

Overriding equals() in Java Internal working of ConcurrentHashMap in Java Image Comparison in Java Java - PermGen space vs MetaSpace Implement Counting Sort using Java + Performance Analysis Java 8 Features Java Miscelleneous Topics Java Basic Topics Java- Main Menu