13

如果我错了,请随时纠正我。在 JVM 堆中,有两代,老的和年轻的。在做full GC的时候,老年代,有压缩空间、修复漏洞等繁重的操作,会导致JVM挂掉。而且我发现在年轻代中,应用了轻量级GC,从我的搜索结果中,还有一个叫做Eden的领域涉及年轻代。但是查了很多文档,对于young generation的GC,我还是有两个疑惑,

  1. 在年轻一代中,似乎 GC 不像老一代 GC 那样工作(即老一代 GC 紧凑并修复漏洞)?如果是这样,年轻一代的 GC 是如何工作的?
  2. 什么是伊甸园空间以及这个空间在年轻一代中是如何利用的?感谢是否可以推荐任何适合新手的文件。
4

1 回答 1

30

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.

于 2012-12-01T15:49:33.440 回答