15

Please feel free to correct me if I am wrong. In JVM heap, there are two generations, old and young. When doing full GC, in old generation, there are heavy operations like compact spaces and fixing the hole, which will make JVM hang. And I find in young generation, a light weighted GC is applied, and there are another area called Eden involved in young generation from my search results. However, after search a lot of documents, I still have two confusions about GC in young generation,

  1. In young generation, it seems GC does not work in the way which old generation GC works (i.e. old generation GC compact and fixing the hole)? If so, how did GC in young generation works?
  2. What is Eden space and how this space is utilized in young generation? Appreciate if any document for a newbie could be recommended.
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • Sometimes ago, I have written couple of articles elaboration mechanics of generational and concurrent GC in HotSpot JVM. You should find your answers there. - [Understanding GC pauses in JVM, HotSpot's minor GC](http://blog.ragozin.info/2011/06/understanding-gc-pauses-in-jvm-hotspots_02.html) - [Understanding GC pauses in JVM, HotSpot's CMS collector](http://blog.ragozin.info/2011/06/understanding-gc-pauses-in-jvm-hotspots_02.html) – Alexey Ragozin Dec 02 '12 at 10:38

1 Answers1

31

This is the single, most important diagram you have to memorize and understand:

Java memory layout
(source: oracle.com)

It comes from Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning, one stop place to learn everything about GC internals. But to address your immediate questions:

Allocating new objects using new operator (almost) always happens in Eden space. But Eden is actually a stack. When you create new object needing N bytes, single pointer advances by N bytes on that stack and that's it. Allocating is that fast, no searching for free spot, compacting, whatever.

Of course this stack is not infinite, at some point we'll reach its end, triggering minor GC. Also most likely multiple objects are already garbage. So what JVM does in minor GC is the following:

  • traverse graph of objects starting from GC roots

  • copy all objects reachable from GC roots to one of survivor spaces (no gaps, we know all of them and this is a single process)

  • wipe out eden space (basically just moving this stack pointer back to 0)

In subsequent minor collections there are additional steps:

  • one of survivor spaces is examined as well. Live objects from both eden and one of survivor spaces are copied to second survivor space. This means there is always exactly one free survivor space.

So how are objects ending in tenured generation? First young objects are copied to one of survivor spaces. Then they are copied to the other and again and again. Once given object jumps back and forth too many times (configurable, 8 by default), it is promoted to tenured space.

Major GC runs when tenured space is full.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thanks, Tomasz. Two more questions. 1. how young generation GC is triggered? Fixed time interval based or when Eden space is used out, or more complex conditions? 2. for your comment, "Once this jumping back and forth ends, they are promoted to tenured space.", under which condition will the "jumping back and forth" process ends? From your description, it seems we can always copy back and force like an endless process? – Lin Ma Dec 01 '12 at 16:26
  • @LinMa: I clarified my answer. Minor GC is triggered when eden is full. Major when tenured is full. Objects are promoted to tenured from survivor if they were copied from one survivor to another 8 times. – Tomasz Nurkiewicz Dec 01 '12 at 16:30
  • Thanks, Tomasz. 1. You mentioned "traverse graph of objects starting from GC roots", I think you mean traverse objects in Eden? 2. The free space in old generation will not be used directly by new allocation of the program, but will only be used by objects promoted from young generation to old generation? – Lin Ma Dec 02 '12 at 04:45
  • @LinMa: 1. tenured object are also traversed, e.g. because they can reference back object in young generation. But they are not touched. 2. Sometimes [objects are allocated directly in old generation](http://stackoverflow.com/questions/9053144) – Tomasz Nurkiewicz Dec 02 '12 at 09:44
  • Thanks Tomasz, pretty cool link which many ideas never found in other documents. You mentioned "copy all objects reachable from GC roots to one of survivor spaces", 1. I think in the context you mean copy all objects from Eden to one of survivor spaces? 2. When copy to the survivor spaces, how do we know where to put the object in survivor spaces? Since I think young generation minor GC only marks whether an object is free or used and not do compaction, so there are no continuous free space in survivor space? If so, where to decide where to copy the object to in survivor space? – Lin Ma Dec 02 '12 at 10:32
  • @LinMa: All object from Eden and one of the survivor spaces are copied to the second survivor space (always empty). They are implicitly compacted since they are placed one after another in survivor space with no gaps. Please open another question if you need further details (and make sure you understand Oracle documentation quoted above) to avoid lengthy chats in comments. – Tomasz Nurkiewicz Dec 02 '12 at 11:42
  • Hi @Tomasz, I post one more question in a new topic here, appreciate if you could help to take a look as well, http://stackoverflow.com/questions/13670744/old-generation-space-utlization-in-jvm – Lin Ma Dec 02 '12 at 15:36
  • Hi @Tomasz, I have a new question here, appreciate if you could help to take a look, => http://stackoverflow.com/questions/13683866/garbage-collection-issue-in-young-generation – Lin Ma Dec 04 '12 at 01:40