📜 ⬆️ ⬇️

Visualization algorithms for garbage collection

Most developers take the garbage collection for granted. This is a standard process that periodically frees memory, removing 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" with five different garbage collection algorithms and published small animations that demonstrate their work.

Larger animations are available on github.com/kenfox/gc-viz .

Brief explanation on infographics. Each picture entirely is a memory allocated for the program. At first it is black, that is, unused, but gradually begins to be highlighted with bright yellow (write operations) and bright green (read operations). Over time, the recorded fragments darken to show the development of the process over time.
')
You may notice that during the execution of the program starts to ignore certain sections of memory. They are considered "garbage."

The first animation shows how the program works without the garbage collector, that is, according to the cleaning method at the end. The easiest way: you need to wait until the end of the process, and then clear everything at once. This is surprisingly effective technique, the author writes, if you have a way to break the process into separate 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 the reference 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, the object is deleted.

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

Red pixels appeared on the animation, which correspond to the operation of reference counting. It is possible to evaluate the efficiency of the collector, if immediately after the red flash the memory area is released.

In the author's blog and on his Github page, one can also study the algorithms of Mark-Sweep type collectors, which solves the problem of processing cyclic structures in memory (animation on the right), Mark-Compact with moving objects in memory ( animation ) and copying collector, which also objects in memory, but makes it in an easier way ( animation ).

On this topic:
Garbage Collection clearly

Source: https://habr.com/ru/post/240021/


All Articles