Java heap terminology: young, old and permanent generations?
Asked Answered
V

3

360

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?
Ventral answered 24/1, 2010 at 21:51 Comment(3)
Assuming you're talking about the Sun JDK/OpenJDK, see the page on the OpenJDK website on Storage Management. There are a couple of links to even more information at the bottom.Spat
also related with this question "tenured generation"Incapacitate
If you are still looking, here is a great video that explains old and young generation in the GC: youtu.be/OnodHoNYE1YLouden
K
335

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.

Kweichow answered 24/1, 2010 at 23:2 Comment(10)
I believe that as of Java 7, strings are no longer interned in the permanent generation.Personify
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)Kweichow
Joshua -- is "old" synonymous with "tenured," and is "new" synonymous with "survivor?"Swastika
the perm gen is only applicable prior to Java 8.Sickert
@lwpro2 In 8 permGen is not removed but it is placed in the Old Generation section.Jaquelin
@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 ?Intervene
Thanks for mentioning Oracle. I never had to bother about PermGen on IBM JDK.Amour
excellent..may i know where method area, nativestack and runtime constant pool resides in this picture? and what they hold accordingly?Tetrahedral
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-collectionGujarat
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?Hull
A
17

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.

Actinomycete answered 24/1, 2010 at 21:57 Comment(0)
F
4

Memory in Sun's HotSpot JVM is organized into three generations: the "young" generation, the "old" generation and the "permanent" generation.

  • Young Generation: Newly created objects are allocated into the young generation.
  • 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 get promoted to the old gen. So, to say conclusively: long lived objects house in old generation.
  • Permanent Generation: The permanent generation holds objects that the JVM finds convenient not to have the garbage collector manage, such as objects describing classes and methods, as well as the classes and methods themselves.

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

How do the three generations interact with / relate to each other?

All objects, except for large ones, are first allocated to the young generation. If an object remains alive after x number of garbage collection cycles, it gets promoted to the old/tenured generation. Hence, we can say that the young generation contains short lived objects, while the old generation contains objects that will have a long life. The permanent generation does not interact with the other two generations.

Freeliving answered 29/6, 2015 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.