V8 如何管理它的堆?

How does V8 manage its heap?

我知道V8的Garbage Collection在工作的时候,会从GC的root开始追溯,标记不可达的对象,然后进行清理。我的问题是GC是怎么遍历遍历那些对象的?必须有一个数据结构来存储所有可达或不可达的对象。位图?已链接 table?

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

艾伦秀,

Google 的 V8 堆被组织成几个不同的 space。有一个很棒的 post, "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.cc and heap.h. Initialization of the heap begins at line 5423. The method Address NewSpaceStart() found on line 615 of heap.h包含new-space开始的地址位置,利用时间局部性将对象存储在何处

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

GC 也有不同的风格,比如你想要 low-latency environment, if you re-wrote the JVM in Scala, and the Latency tuning options within the .NET environment

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

感谢您的宝贵时间,

温暖的问候,