问题描述
如果我错了,请随时纠正我。在JVM堆中,有两代人,老少皆宜。在进行完整的GC时,在老一代中,像紧凑空间和固定孔这样的繁重操作会导致JVM挂起。我发现在年轻一代中,应用了轻量级GC,另外还有一个领域叫做伊甸参与了我的搜索结果。然而,在搜索了很多文件后,我仍然对年轻一代GC有两个混淆,在年轻一代中,GC似乎确实如此不适合老一代GC工作的方式(即旧一代GC紧凑和固定孔)?如果是这样,年轻一代的GC如何运作?
它来自,一站式学习有关GC内部的一切。但要解决您的直接问题:
使用 new
运算符()总是发生在伊甸园的空间。但伊甸园实际上是一个堆栈。当您创建需要N个字节的新对象时,单个指针在该堆栈上前进N个字节,就是这样。分配的速度很快,没有寻找空闲点,压缩,无论如何。
当然,这个堆栈并不是无限的,在某些时候我们会达到最后,触发次要的GC。也很可能多个对象已经是垃圾。所以JVM在小GC中的作用如下:
-
从 GC根开始的对象遍历图 >
复制从GC根到达的所有对象到其中一个存活空间(没有空白,我们知道它们全都是单个进程) > -
消除eden空间(基本上只是将这个堆栈指针移回到
0
) >
在后续的小集合中还有其他步骤:
主要的GC在终身空间满时运行。 / p>
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,
- 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?
- What is Eden space and how this space is utilized in young generation? Appreciate if any document for a newbie could be recommended.
This is the single, most important diagram you have to memorize and understand:
Java memory layout http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/190244.gif
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.
这篇关于年轻一代的JVM垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!