艾伦秀,
Google 的 V8 堆被组织成几个不同的空间。有一篇很棒的帖子,“V8 之旅:垃圾收集 http://www.jayconrod.com/posts/55/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 的构建风格切尼算法 http://en.wikipedia.org/wiki/Cheney's_algorithm.
V8 的堆实现位于heap.cc https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/heap.cc?r=10000 and heap.h https://code.google.com/p/v8/source/browse/branches/3.24/src/heap.h?r=20595。堆的初始化开始于line 5423
。方法Address NewSpaceStart()
发现于line 615
of heap.h https://code.google.com/p/v8/source/browse/branches/3.24/src/heap.h?r=20595包含 new-space 开始的地址位置,并利用时间局部性将对象存储在何处。
现在回答你的第二个问题:JVM 也做同样的事情吗?一个有趣的事实:有 3 种主要的生产 JVM,它们的 GC 算法实现方式都不同。有一个很棒的性能博客写了这篇文章,“三大 JVM 中的垃圾收集有何不同 http://apmblog.dynatrace.com/2011/05/11/how-garbage-collection-differs-in-the-three-big-jvms/”,其中将更详细地讨论它们的实现。
GC 也有多种口味,例如如果您想要低延迟环境 https://blogs.janestreet.com/building-a-lower-latency-gc/, 如果你用 Scala 重写 JVM https://github.com/lihaoyi/Metascala, and .NET 环境中的延迟调整选项 https://msdn.microsoft.com/en-us/library/vstudio/bb384202%28v=vs.100%29.aspx.
请让我知道,如果你有任何问题!
感谢您的时间,
温暖的问候,