【问题标题】:How does V8 manage its heap?V8 如何管理它的堆?
【发布时间】:2015-08-21 03:22:18
【问题描述】:

我知道当 V8 的垃圾收集器工作时,它会从 GC 的根开始跟踪,以便标记无法访问的对象,然后将其清除。我的问题是 GC 遍历是如何遍历这些对象的?必须有一个数据结构来存储所有可访问或不可访问的对象。位图?链接表?

顺便说一句,JVM 也会这样做吗?

【问题讨论】:

  • 您想知道所有可达/不可达,还是根源?我自己对 v8 了解不多,但我知道大多数 GC 的答案都很复杂,而且我不太确定您在寻找答案的哪一部分。

标签: java garbage-collection heap-memory v8 mark-and-sweep


【解决方案1】:

艾伦秀,

Google 的 V8 堆被组织成几个不同的空间。有一篇很棒的帖子“A tour of V8: Garbage Collection”解释了 V8 堆的组织方式:

New-space: Most objects are allocated here. New-space is small and is
designed to be garbage collected very quickly, independent of other
spaces.

Old-pointer-space: Contains most objects which may have pointers to 
other objects. Most objects are moved here after surviving in new-space 
for a while.

Old-data-space: Contains objects which just contain raw data (no 
pointers to other objects). Strings, boxed numbers, and arrays of
unboxed doubles are moved here after surviving in new-space for a 
while.

Large-object-space: This space contains objects which are larger than
the size limits of other spaces. Each object gets its own mmap'd region
of memory. Large objects are never moved by the garbage collector.

Code-space: Code objects, which contain JITed instructions, are 
allocated here. This is the only space with executable memory (although
Codes may be allocated in large-object-space, and those are executable, too).

Cell-space, property-cell-space and map-space: These spaces contain
Cells, PropertyCells, and Maps, respectively. Each of these spaces
contains objects which are all the same size and has some constraints 
on what kind of objects they point to, which simplifies collection.

Conrad 的文章继续解释了 V8 GC 是基于 Cheney's Algorithm 的风格构建的。

V8 的堆实现位于heap.ccheap.h。堆的初始化从line 5423 开始。在heap.hline 615 上找到的方法Address NewSpaceStart() 包含新空间开始的地址位置,并且利用时间局部性将对象存储在哪里。

现在回答您的第二个问题:JVM 也这样做吗? 一个有趣的事实:有 3 个主要的生产 JVM,它们都以不同的方式实现其 GC 算法。有一篇很棒的性能博客写了文章“How Garbage Collection differs in the three big JVMs”,它将更详细地讨论它们的实现。

GC 也有多种类型,例如如果你想要 low-latency environment、如果你想要 re-wrote the JVM in Scalathe Latency tuning options within the .NET environment

如果您有任何问题,请告诉我!

感谢您的宝贵时间,

热烈的问候,

【讨论】:

  • 感谢您的回答。它非常接近我想要的。还有一个问题,如何在一个空间中组织对象。例如,在旧数据空间中,GC 开始从 Root 跟踪任何可访问的对象,并使用指针获取它们的地址以标记它们。那些没有标记的会被扫掉,但是不可达的时候GC怎么知道它们的地址和长度呢?
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 2011-09-16
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多