All the ways to collect garbage

As promised in my previous blog, I am writing about some of the techniques used by different programming languages to automatically collect the dead/no-more-used objects during runtime.

There are broadly three schemes/techniques which is the core of garbage collection. I will try writing about these three in the as straightforward way as possible. Also, I will go one step ahead and discuss the fourth technique, developed by the combination of the three schemes. Let’s start!

1. Reference Counting

The idea behind this type of garbage collection is to keep track of usage of an object at every point of the program. This idea means as soon as an object becomes garbage (To know when an object becomes eligible for waste, read geeksforgeeks), the space occupied by the object is rehabilitated by putting the free space address in a free list. Then, new objects are allocated by matching the required space with the available spaces on the free list. This concept appears very attractive because it makes sense to do garbage collection as soon as garbage is there. Does it? Take a step back from the virtual world, and enter the real world. Do you run to the dumpster at the end of your lane every time you have to throw away the gum packet? You don’t, right? You collect the garbage in your dustbin, and when the bin is full, you go to the dumpster. In computer science, this is known as a lazy approach, i.e., to take action only when needed. This leads us to the second technique available for garbage collection.

2. Mark and Sweep.

Let’s enter the real world again. Whenever you dustbin is full, you stop all your essential works ( Well, that could be watching Beyonce video too) and dump the dustbin to the dumpster at the end of your lane. This concept that process stops doing anything else and throws away the garbage is known as “Stop-the-World.” Mark and Sweep is one of the algorithms which builds upon Stop the World. Whenever the free memory for allocation is finished, the garbage collector kicks in and starts working. This being clear, let’s see how the algorithm works once the process is stopped from working. It works by having two phases. Firstly, it goes to all the registers, global variables and local variables {This is known as root set} and goes to all the objects reachable from the root set. It marks these reachable objects ( the live objects) and exits. Then there is the second phase of sweeping the whole heap. During this sweeping, everything not marked is the garbage, and their corresponding addresses are inscribed in the free list for future object allocations.

Writing this blog, I feel I have given a lot of information in very few words with few dangling pieces of information. So, I will stop here for this blog and continue about the other two types of garbage collectors in my next blog.

Till then, which languages you use and do you have any idea which type of garbage collectors they use.

Happy coding.

原文链接:All the ways to collect garbage

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容