Garbage collection in V8, an illustrated guide

What’s v8?

V8, the javascript runtime engine, not to be confused with your fav tomato juice 🍹, compiles and executes your beautifully written javascript. V8 comes with a generational, stop-the-world garbage collector, which I am going to try to explain below. It comes hand in hand together with Chrome. SpiderMonkey would be the Mozilla’s equivalent, and Chakra, Microsoft’s. Basically when running javascript, you need an engine to handle it for ya and V8 is one of your options, whether it’s in the browser or in node.js environment. (P.S. it’s all ✨ open-source ✨.)

What is garbage collection?

The essential point of garbage collection is the ability to manage memory usage by a specific program. Languages such as C are generally able to hook into program’s memory management and allocate and free object within the context of the program. ECMAScript, on the other hand, lacks that particular interface to access memory management (yes, that means no API). Which basically means all memory management rights™ in a program get passed over to V8.

Dead or alive?

The basic check for when an object is dead or alive, is whether or not the client, or the program that executes the code, can reach it. The most reachable object you can think of, is probably an object defined in root scope.

So how do we get to garbage collection even? (tell me! tell me!)

V8 allocates memory in the heap as you create new objects, or new ‘pointers’. (javascript doesn’t have real pointers, so ‘pointers’ are technically just copied references to an original object). There are a bunch of different spaces for different types of objects in the heap, and it would be organized something like this:

🆒 sources.js



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Irina Shestak

Irina Shestak

javascript, hot takes, 80% 🍜.