Java heap terminology: young, old and permanent generations?

257,052

Solution 1

This seems like a common misunderstanding. In Oracle's JVM, the permanent generation is not part of the heap. It's a separate space for class definitions and related data. In Java 6 and earlier, interned strings were also stored in the permanent generation. In Java 7, interned strings are stored in the main object heap.

Here is a good post on permanent generation.

I like the descriptions given for each space in Oracle's guide on JConsole:

For the HotSpot Java VM, the memory pools for serial garbage collection are the following.

  • Eden Space (heap): The pool from which memory is initially allocated for most objects.
  • Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.
  • Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.
  • Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
  • Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.

Java uses generational garbage collection. This means that if you have an object foo (which is an instance of some class), the more garbage collection events it survives (if there are still references to it), the further it gets promoted. It starts in the young generation (which itself is divided into multiple spaces - Eden and Survivor) and would eventually end up in the tenured generation if it survived long enough.

Solution 2

The Heap is divided into young and old generations as follows :

Young Generation : It is place where lived for short period and divided into two spaces:

  • Eden(Adam and Eve first lived) Space : When object created using new keyword memory allocated on this space.
  • Survivor Space : This is the pool which contains objects which have survived after java garbage collection from Eden space.

Old Generation : This pool basically contains tenured and virtual (reserved) space and will be holding those objects which survived after garbage collection from Young Generation.

  • Tenured Space: This memory pool contains objects which survived after multiple garbage collection means object which survived after garbage collection from Survivor space.

Permanent Generation : This memory pool as name also says contain permanent class metadata and descriptors information so PermGen space always reserved for classes and those that is tied to the classes for example static members.

Java8 Update: PermGen is replaced with Metaspace which is very similar.
Main difference is that Metaspace re-sizes dynamically i.e., It can expand at runtime.
Java Metaspace space: unbounded (default)

Code Cache (Virtual or reserved) : If you are using HotSpot Java VM this includes code cache area that containing memory which will be used for compilation and storage of native code.

enter image description here

Courtesy

Solution 3

What is the young generation?

The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. A young generation full of dead objects is collected very quickly. Some survived objects are aged and eventually move to the old generation.

What is the old generation?

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

What is the permanent generation?

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application.

PermGen has been replaced with Metaspace since Java 8 release.

PermSize & MaxPermSize parameters will be ignored now

How does the three generations interact/relate to each other?

enter image description here

Image source & oracle technetwork tutorial article: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

"The General Garbage Collection Process" in above article explains the interactions between them with many diagrams.

Have a look at summary diagram:

enter image description here

Solution 4

The Java virtual machine is organized into three generations: a young generation, an old generation, and a permanent generation. Most objects are initially allocated in the young generation. The old generation contains objects that have survived some number of young generation collections, as well as some large objects that may be allocated directly in the old generation. The permanent generation holds objects that the JVM finds convenient to have the garbage collector manage, such as objects describing classes and methods, as well as the classes and methods themselves.

Solution 5

Memory in SunHotSpot JVM is organized into three generations: young generation, old generation and permanent generation.

  • Young Generation : the newly created objects are allocated to the young gen.
  • Old Generation : If the new object requests for a larger heap space, it gets allocated directly into the old gen. Also objects which have survived a few GC cycles gets promoted to the old gen i.e long lived objects house in old gen.
  • Permanent Generation : The permanent generation holds objects that the JVM finds convenient to have the garbage collector manage, such as objects describing classes and methods, as well as the classes and methods themselves.

FYI: The permanent gen is not considered a part of the Java heap.

How does the three generations interact/relate to each other? Objects(except the large ones) are first allocated to the young generation. If an object remain alive after x no. of garbage collection cycles it gets promoted to the old/tenured gen. Hence we can say that the young gen contains the short lived objects while the old gen contains the objects having a long life. The permanent gen does not interact with the other two generations.

Share:
257,052
knorv
Author by

knorv

Updated on July 09, 2022

Comments

  • knorv
    knorv almost 2 years

    I'm trying to understand What the concepts of young, old and permanent generations are in the Java heap terminology, and more specifically the interactions between the three generations.

    My questions are:

    • What is the young generation?
    • What is the old generation?
    • What is the permanent generation?
    • How does the three generations interact/relate to each other?
  • Tim Goodman
    Tim Goodman over 10 years
    I believe that as of Java 7, strings are no longer interned in the permanent generation.
  • Ates Goral
    Ates Goral over 10 years
    You're right, I'm surprised this survived so long before a mention. Then in Java 8 permanent generation will get replaced by metaspace (although I'm not sure how different this will really be, other than being unbounded by default)
  • joadha
    joadha over 10 years
    Joshua -- is "old" synonymous with "tenured," and is "new" synonymous with "survivor?"
  • Jackie
    Jackie almost 10 years
    the perm gen is only applicable prior to Java 8.
  • Bhavik Ambani
    Bhavik Ambani over 9 years
    @lwpro2 In 8 permGen is not removed but it is placed in the Old Generation section.
  • Vikas Verma
    Vikas Verma almost 9 years
    @Joshua McKinnon Hello sir, I have one question that what is the use of the different memory spaces for variable and I'm specifically talking about Eden, Survivor and Tenured space ?
  • Oleg Mikheev
    Oleg Mikheev over 7 years
    Thanks for mentioning Oracle. I never had to bother about PermGen on IBM JDK.
  • gstackoverflow
    gstackoverflow over 7 years
    @Premraj what does it mean Metaspace re-sizes dynamically i.e., It can expand at runtime. ? The only difference that by default it has not up border?
  • Admin
    Admin almost 7 years
    excellent..may i know where method area, nativestack and runtime constant pool resides in this picture? and what they hold accordingly?
  • Admin
    Admin almost 7 years
    excellent..may i know where method area, nativestack and runtime constant pool resides in this picture? and what they hold accordingly?
  • Admin
    Admin almost 7 years
    excellent..may i know where method area, nativestack and runtime constant pool resides in this picture? and what they hold accordingly?
  • Ravindra babu
    Ravindra babu almost 7 years
    refer to docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html for more details. The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it. Each run-time constant pool is allocated from the Java Virtual Machine's method area
  • Admin
    Admin almost 7 years
    are u sure...ive been reading that its part of permgen space(which is not heap)? journaldev.com/2856/…
  • Admin
    Admin almost 7 years
    if code cache is used for native method code, what does native method stack(each thread will have one) will have?
  • Ravindra babu
    Ravindra babu almost 7 years
    Oracle documentation is more authentic
  • Very Objective
    Very Objective almost 6 years
    Is the threshold set for young generation object in time units (e.g. ms)? or GC rounds?
  • recepinanc
    recepinanc over 4 years
    In case you are still waiting for an answer, yes you are right @joadha. Check out this link: codeahoy.com/2017/08/06/basics-of-java-garbage-collection
  • Hayk Mkrtchyan
    Hayk Mkrtchyan about 2 years
    Well, but what gives us this generations? Assume, if our object which survived several times, goes to the old generation. But how is the algorithm of GC applied for this generations? What if my object in old generation has no references anymore?