How does V8 manage its heap?

2019-08-03 07:00发布

问题:

I know when V8's Garbage Collection is working, it will trace from GC's root so that unreachable objects will be marked and then be swept. My question is how GC traverses traverse those objects? There must be a data structure to store all objects reachable or unreachable. Bitmap? Linked table?

BTW, does JVM do the same?

回答1:

AllenShow,

Google's V8 Heap is organized into a couple of different spaces. There's a great post, "A tour of V8: Garbage Collection" which explains how the V8 heap is organized as:

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's article goes on to explain the V8 GC is built from a flavor of Cheney's Algorithm.

V8's heap implementation resides in heap.cc and heap.h. Initialization of the heap begins at line 5423. The method Address NewSpaceStart() found on line 615 of heap.h contains the address location of where new-space starts, and the objects are stored where by taking advantage of temporal locality.

Now for your second question: does JVM do the same? A fun fact: there are 3 major production JVMs and they all implement their GC algorithms differently. There's a great performance blog which wrote the article, "How Garbage Collection differs in the three big JVMs" which will discusses their implementations in further detail.

There are also flavors of GC, such as if you want a low-latency environment, if you re-wrote the JVM in Scala, and the Latency tuning options within the .NET environment.

Please let me know if you have any questions!

Thank you for your time,

Warm Regards,