The 3 Garbage Removal Paradigms in programming

Arnav
4 min readFeb 21, 2024

You are the Emperor of a kingdom, who recently has exiled some ministers from your nation, and you order all their documents and possessions to be removed form the nation’s treasury. You demand that you do not want to see a single item belonging to those ministers. So the soldiers go to the ministers one by one, ask what and where their items were and remove them from them treasury.

You are the compiler, and you want to remove some variables from the scope, along with the data they have in the heap. For max memory management, you have to remove it all. You go through the elements that went out of scope, and remove them from the memory

The soldiers check who was exiled first, and the remove his documents, and then the seconds and so on. This seems easy. But wait, a merchant, and a Scientist, both own a set of papers together. And they do not know it. When the merchant was asked for the location of the documents, he gave their location and they were removed. But when the Scientist gave the location of the documents, the soldiers went to the same location but could not find the documents there. This could case a disruption. How could they be sure that all documents related to the Scientist has been removed and is not any other problem? Also, what if a document is owned by a person who is not exiled and also owned by the exiled one. In that case, we cannot remove the document. So How do we do that?

The compiler finds two variables pointing at the same piece of data. The compiler obviously cannot know that without more work. So it removes the first one and frees its memory from the heap. Then it goes to the second one, but the memory it was pointing to was already gone! This can cause errors. Also, what if one pointer is still in scope! We have a variable. Say we make a function and send the pointer to our variable in that. When that function goes out of scope, and the code deletes its data from the heap, our access to the variable will also go. This is a huge issue.

The King and The Compiler came up with 3 different ways to handle this problem. Manual Garbage Collection, Garbage Collectors and Ownership Model.

Manual Garbage Collection

The king assigns a person to keep a track of the people. He is the person who assigned all the documents to the people. So he knows who has written which document and when that document can be removed, if some owner is still in the city, then he won’t remove it. This is a good strategy for little villages, as human error can creep into this. For example, he can forget to delete the documents of some people who have been removed, or perhaps delete the documents of someone who is still a citizen! However, in simple programs it is a simple and effective method.

In the C/C++ era, Manual Garbage Collection . The person to keep track is you, the user. He has assigned the variables, and is the best person to when which memory is no longer needed and can be deleted. In low level languages, this gives us ultimate control over data management. But as Uncle Ben said, “With Great Power Comes Great Responsibility”, this makes your code error prone, as human error can creep in at any point into your code. You can forget to free data at times, wasting memory and also can free important variables!

Automatic Garbage Collectors

The king exiles all the people who needed to be. Then, he proceeds and asks his general to go through the documents, and see if they point to people who are still members of the nation. If this is not the case, he is asked to destroy the documents. This eliminates chances of error, as it is more algorithmic. At the same time, it is not that efficient, and in larger nations, this process can take a lot of time.

In languages like Java/JavaScript, Automatic Garbage Collection takes place, where the memory which cannot be traced back to any variable in scope is removed. This is easier to do, as the user doe snot have to worry about garbage collection, although at the cost of efficiency, as the compiler has to scan through the heap for useless variables

The OwnerShip Model

This time, to make things more efficient, the king now makes it a rule that all the documents can only be kept under one person at a time. No two ministers can own the same document. This way, whenever a minister is exiled, the document can easily traced back to that minister. Although hard to implement, this eliminates any possible errors, and is efficient,

This paradigm is what builds languages like Rust. It uses the OwnerShip Model. In this system, the owner of a particular data in the heap can be one only. If he tries to share data with someone, his ownership is revoked and that data is owned by the variable with whom it was shared. This although seems absurd at first, is really useful and efficient in saving memory without the use of Garbage Collection Systems.

--

--