Imagine you arrive at a restaurant. There are a hundred tables at the restaurant. Out of 100, 95 tables are still occupied and at the remaining 5 tables, people have just left and their plates are still on the tables. Now for you to sit, restaurant staff will clear out one of those five tables or all of them and you would sit and enjoy your meal.
Another thing about V8 is that it uses the generational algorithm for garbage collection. This means it divides data into Younger Generation and Older Generation. We will talk about these generations in detail in further sections.
You might ask why V8 uses a generational algorithm? Well, the V8 team believes in something known as the generational hypothesis. Which states: All objects die shortly after creation. They have found this hypothesis by observing multiple applications and their memory footprints. This is the reason they use the generational algorithm and run garbage collection cycles mostly on younger generation memory. Now let us talk about the younger and older generation and see how garbage is collected in both of them.
Now, we will understand the whole process step by step and with help of some images.
So as we can see, the whole memory in V8 is divided into 2 parts. Young generation and Old generation. The young generation in turn is divided into two parts. Don’t worry about that right now. We will see why it is being done in further steps.
Now when we start declaring objects and start using them, V8 will keep filling up the first part of the young generation.
When the first part of the young generation is completely filled, V8 will mark the objects dead or alive and the alive objects will be moved to the second part of the young generation.
Now all the new objects will be placed in the second part of the young generation until that is filled.
Now comes the interesting part. Once the second part of the young generation is also completely filled, V8 will again mark the objects dead or alive and the alive objects will now be moved to Old Generation because if the object is still surviving, it is like to stay alive for a long time. After this, the second part is cleared and the same process from step 1 to step 5 is followed for new objects.
Now while V8 is doing this process, at some point in time, there will be no enough space for a new object in the old generation, a full mark, and sweep garbage collection algorithm will come into the picture.
Mark and sweep is a two-step process. The first part mark is where V8 will try to reach out to every accessible object from the call stack and global scope. All reachable objects are marked as alive and rest are marked as dead.
Now we have to understand that Old generation is quite large and the object sizes in it are also very large. That is why V8 can just remove dead objects as it does in the young generation because it will be a very heavy computation. Due to this limitation, the sweep process is done in various cycles where V8 will create some parallel threads and keep on sweeping memory in parts.
Sigh!! That was quite a ride. Now there is a lot of other detailing and tricks on how V8 manages garbage collection which we can not cover in a single post. I would suggest you go through the references section of this article and definitely check out the links if those excite you. Till then, Happy Coding!!