Visualization of garbage collection algorithms

  • Tutorial
Most developers take garbage collection for granted. This is a standard process that periodically frees memory by deleting unnecessary objects. But the American programmer Ken Fox (Ken Fox) wanted to thoroughly understand and look "under the hood" of modern garbage collectors.

Ken “played around” with five different garbage collection algorithms and published small animations that clearly demonstrate their work.

Larger animations are available at .

Brief explanation of infographics. Each picture as a whole is a memory allocated for the program. At first it is black, that is, unused, but gradually begins to be highlighted in bright yellow (write operations) and bright green (read operations). Over time, the recorded fragments darken to show the development of the process in time.

You may notice that in the course of execution the program starts to ignore individual sections of memory. They are considered "garbage."

The first animation shows how the program works without the garbage collector, that is, by the method of cleaning at the end. The easiest way: you need to wait for the end of the process, and then clean it all at once. This is a surprisingly effective technique, the author writes, if you have a way to break the process down into individual tasks. This is done, for example, by the Apache web server.

For contrast, this is what the same program looks like, but with a garbage collector using a link counting technique. This is another relatively simple method, when the number of references to an object in memory is counted, and if it drops to zero, then the object is deleted.

Link counting is the only algorithm that is well compatible with different resource managers.

Red pixels appeared on the animation that correspond to the link counting operation. You can evaluate the efficiency of the collector, if immediately after the red flash the memory area is freed.

In the author’s blog and on his Github page, one can also study collector algorithms such as Mark-Sweep, which solves the problem of processing cyclic structures in memory (animation on the right), Mark-Compact with moving objects in memory ( animation) and copy collector, which also moves objects in memory, but does it in a simpler way ( animation ).

By topic:
Garbage Collection clearly

Also popular now: