Memory Management in Java

Here we look into how the jvm memory is structured. The internals of Java memory model depends on which JVM is used, though at high level things are similar. There are two main memory regions in the JVM - the heap and the stack. Local variables and methods reside on the stack, everything else on the heap.

Java Heap Memory Structure

Java Heap Memory is divided into various regions called generations:
  • Young Generation (Eden and Survivor space)
  • Old Generation (Tenured Space)
  • Permanent Generation (permGen)
When a new object is first created it goes to Young generation. When young generation fills up it causes a minor garbage collection (GC). Some objects that survive this GC are aged. The longer an object survives it keeps moving to old generation (after a threshold age). So, Old Generation stores long surviving objects. At some point the Old generation also needs to be collected which causes a major garbage collection. Young generations are more frequently garbage collected than old generation.


Objects in permGen are relatively permanent. Class definitions (application classes and Java SE library classes) and static variables are stored in this memory. The permanent generation is collected in full garbage collection. The exact deletion logic of objects depends on which JVM is used.
Note: With Java 8 Perm gen is removed completely. Its replaced by Metaspace.
Also with Java8, Oracle (Sun) Hotspot JVM and Oracle JRockit JVM are converged.

Where Java static variables are stored in memory?

Static member variables are stored in Permanent Generation area of heap. The reason is because static don't belong to the object but to the class, so its considered as part of class definition. If the static variables are primitive types they will be stored in the permGen. If the static variable is a reference variable, e.g. static Person obj = new Person(), then refrence variable obj would be stored in permGen and the newly created objected would be placed in young generation.

What is difference between permGen and Metaspace?

As you might know by know that with Java 8, permGen is replaced by Metaspace.
There are many difference between them, the major two being:
  • Metaspace can expand at runtime.
  • Metaspace is part of Native memory whereas permGen is part of heap.