V8 如何管理它的堆?

All*_*Lin 3 java garbage-collection v8 heap-memory mark-and-sweep

我知道V8的垃圾收集在工作时,会从GC的root开始追踪,这样无法到达的对象就会被标记然后被清除。我的问题是GC是如何遍历那些对象的?必须有一个数据结构来存储所有可达或不可达的对象。位图?链接表?

顺便说一句,JVM 也做同样的事情吗?

Dev*_*sai 5

艾伦秀,

Google 的 V8 堆被组织成几个不同的空间。有一篇很棒的文章“ V8 之旅:垃圾收集”,其中解释了 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.
Run Code Online (Sandbox Code Playgroud)

Conrad 的文章接着解释了 V8 GC 是根据切尼算法的风格构建的。

V8 的堆实现位于heap.ccheap.h中。堆的初始化开始于line 5423heap.hAddress NewSpaceStart()line 615的方法包含 new-space 开始的地址位置,并利用时间局部性将对象存储在该位置。

现在回答第二个问题:JVM 也会做同样的事情吗?一个有趣的事实:有 3 种主要的生产 JVM,它们的 GC 算法实现方式都不同。有一个很棒的性能博客写了一篇文章“垃圾收集在三大 JVM 中有何不同”,该文章将更详细地讨论它们的实现。

GC 也有多种风格,例如您是否需要低延迟环境、是否在 Scala 中重写 JVM,以及.NET 环境中的延迟调整选项

请让我知道,如果你有任何问题!

感谢您的时间,

温暖的问候,