256

I’m currently monitoring a Java application with jconsole. The memory tab lets you choose between:

Heap Memory Usage
Non-Heap Memory Usage
Memory Pool “Eden Space”
Memory Pool “Survivor Space”
Memory Pool “Tenured Gen”
Memory Pool “Code Cache”
Memory Pool “Perm Gen”

What is the difference between them ?

assylias
  • 321,522
  • 82
  • 660
  • 783
Dani Cricco
  • 8,829
  • 8
  • 31
  • 35
  • Assuming that you're using the Sun JDK, the best answer will be found in their documentation: [Tuning Garbage Collection (JDK 1.5)](http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html) and [Garbage Collection FAQ (JDK 1.4)](http://java.sun.com/docs/hotspot/gc1.4.2/faq.html) – kdgregory Aug 11 '09 at 19:11

4 Answers4

363

Heap memory

The heap memory is the runtime data area from which the Java VM allocates memory for all class instances and arrays. The heap may be of a fixed or variable size. The garbage collector is an automatic memory management system that reclaims heap memory for objects.

  • Eden Space: The pool from which memory is initially allocated for most objects.

  • Survivor Space: The pool containing objects that have survived the garbage collection of the Eden space.

  • Tenured Generation or Old Gen: The pool containing objects that have existed for some time in the survivor space.

Non-heap memory

Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the Java VM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors. The method area is logically part of the heap but, depending on the implementation, a Java VM may not garbage collect or compact it. Like the heap memory, the method area may be of a fixed or variable size. The memory for the method area does not need to be contiguous.

  • Permanent Generation: 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: The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.

Here's some documentation on how to use Jconsole.

Gray
  • 115,027
  • 24
  • 293
  • 354
dfa
  • 114,442
  • 31
  • 189
  • 228
  • 4
    I'm not sure @dfa is completely correct as the Java Virtual Machine Specification clearly states: “Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.” However it is clear that jconsole shows the Code Cache and Permanent Generation as Non-Heap, that seems to contradict the specification. Can anyone provide more clarify on this contradiction? – James Bloom Apr 09 '13 at 07:09
  • @JamesBloom - I was wondering the same. Even though the basic definition states which memory pool belongs to which type(heap/non-heap), it could change is state explicitly? – Umang Desai Aug 06 '13 at 18:33
  • 2
    the doc this was semingly nicked from: http://docs.intergral.com/pages/viewpage.action?pageId=22478944 The doc contains some other good information about the JVM, worth a browse – Steve Siebert Jan 29 '14 at 19:30
  • 1
    Despite lots of upvotes, it's not so meaningful answer, actually. For example, what does "objects that have survived the garbage collection of the Eden space" mean? Are these objects moved to Survivor Space from Eden after surviving, or their space in Eden is considered as Survivor space? And what about garbage collection in pools other than Eden space, does it happen? Totally not clear. – Mikhail Batcer Mar 11 '16 at 06:29
  • and don't forget stack (on the non-heap side) :) – Toothless Seer Jun 29 '17 at 01:02
  • Mikhail, it's clear, objects come out of Eden space to a survivor area at the first sweep in the non-tenured space (eden+survivor(s)). Objects keep being moved between survivor areas until they reach MaxTenuringThreshold (default of 16). Then they go into Tenured heap. Once in Tenured Heap they stay until unreferenced, marked and swept. See the below/newer diagram on this page. – BodhiOne Jan 16 '20 at 17:30
91

The new keyword allocates memory on the Java heap. The heap is the main pool of memory, accessible to the whole of the application. If there is not enough memory available to allocate for that object, the JVM attempts to reclaim some memory from the heap with a garbage collection. If it still cannot obtain enough memory, an OutOfMemoryError is thrown, and the JVM exits.

The heap is split into several different sections, called generations. As objects survive more garbage collections, they are promoted into different generations. The older generations are not garbage collected as often. Because these objects have already proven to be longer lived, they are less likely to be garbage collected.

When objects are first constructed, they are allocated in the Eden Space. If they survive a garbage collection, they are promoted to Survivor Space, and should they live long enough there, they are allocated to the Tenured Generation. This generation is garbage collected much less frequently.

There is also a fourth generation, called the Permanent Generation, or PermGen. The objects that reside here are not eligible to be garbage collected, and usually contain an immutable state necessary for the JVM to run, such as class definitions and the String constant pool. Note that the PermGen space is planned to be removed from Java 8, and will be replaced with a new space called Metaspace, which will be held in native memory. reference:http://www.programcreek.com/2013/04/jvm-run-time-data-areas/

Diagram of Java memory for several threads Diagram of Java memory distribution

RespiteSage
  • 23
  • 1
  • 6
Pythoner
  • 5,265
  • 5
  • 33
  • 49
  • The diagram looks very self explanatory... Is this valid for any GC algorithm. G1 have different set. – Venkateswara Rao Dec 06 '16 at 07:55
  • @Pythoner I think the flag in dark purple should be `-XX:PermSize` and not `-XX:MaxPermSize` as it is already defined above that. – Anurag May 16 '18 at 00:06
35

With Java8, non heap region no more contains PermGen but Metaspace, which is a major change in Java8, supposed to get rid of out of memory errors with java as metaspace size can be increased depending on the space required by jvm for class data.

user2767149
  • 351
  • 3
  • 2
  • 1
    Actually, there is metaspace and class space: http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/considerations.html#sthref66 – mrswadge Feb 22 '17 at 14:20
4

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

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

  • Eden Space: When object created using new keyword memory allocated on this space.
  • Survivor Space (S0 and S1): This is the pool which contains objects which have survived after minor 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 the Young Generation.

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

enter image description here

Explanation

Let's imagine our application has just started.

So at this point all three of these spaces are empty (Eden, S0, S1).

Whenever a new object is created it is placed in the Eden space.

When the Eden space gets full then the garbage collection process (minor GC) will take place on the Eden space and any surviving objects are moved into S0.

Our application then continues running add new objects are created in the Eden space the next time that the garbage collection process runs it looks at everything in the Eden space and in S0 and any objects that survive get moved into S1.

PS: Based on the configuration that how much time object should survive in Survivor space, the object may also move back and forth to S0 and S1 and then reaching the threshold objects will be moved to old generation heap space.