Garbage collection in programming?

Sourav Jha
4 min readJan 17, 2023

--

Photo by Antoine GIRET on Unsplash

Garbage collection is a method of automatically freeing memory that is no longer being used by a program. It works by identifying objects in memory that are no longer reachable by the program, and then reclaiming the memory used by those objects. This is done by a garbage collector, which is a program or algorithm that runs in the background, monitoring memory usage and freeing memory as necessary.

Why we need Garbage collection?

  1. Memory management: Garbage collection automates the process of freeing memory that is no longer being used by a program, which can help to prevent memory leaks and other memory-related issues.
  2. Simplifying code: Without garbage collection, developers would have to manually manage memory allocation and deallocation in their code. This can be complex and error-prone, and can make the code harder to read and understand.
  3. Improved performance: Garbage collection can help to improve the performance of a program by reclaiming memory that is no longer being used, which can help to prevent the program from running out of memory and crashing.
  4. Better utilisation of resources: Garbage collection can ensure that the system resources are used efficiently, by freeing up memory that is no longer needed. This can be especially important in resource-constrained environments such as mobile devices or embedded systems.
  5. Improved scalability: Garbage collection can help to improve the scalability of a program, by allowing it to continue running even as the amount of data it needs to process increases.

Algortihms for garbage collection in programming?

There are several types of garbage collection, each with its own set of advantages and disadvantages. Some of the more common types include:

  1. Reference counting: This method of garbage collection assigns a reference count to each object in memory, and frees the memory of an object when its reference count reaches zero.
  2. Mark-and-sweep: This method of garbage collection periodically “marks” all objects in memory that are currently in use, and then “sweeps” to reclaim the memory of any objects that were not marked.
  3. Copying: This method of garbage collection divides memory into two equal parts, and copies all live objects from one half to the other. The half containing the dead objects is then freed.
  4. Generational: This method of garbage collection assumes that most objects die young and separates memory into different generations for different age objects. It’s more efficient to check for garbage in the young generation rather than in the old generation.
  5. Concurrent: This method of garbage collection runs in parallel with the program, which allows the program to continue running while the garbage collector is working.
  6. Incremental/Real-time: This method of garbage collection divides the garbage collection process into small parts that can be spread out over time. This can help to reduce the impact of garbage collection on the performance of the program.

Each type of garbage collection algorithm has its own trade-offs and is more suitable for certain use cases than others.

Two most popular algorithms are

Photo by Mateusz Wacławek on Unsplash

Reference counting is a method of garbage collection where each object in memory is assigned a reference count, which is the number of references to that object from other parts of the program. When the reference count of an object reaches zero, it is considered no longer in use and its memory is freed. This method is simple to implement and can be efficient in certain situations, but it can also lead to “memory leaks” where objects are no longer needed but their reference count is never decremented.

Mark-and-sweep is a method of garbage collection that involves periodically “marking” all objects in memory that are currently in use, and then “sweeping” to reclaim the memory of any objects that were not marked. This method is more complex to implement than reference counting, but it is less prone to memory leaks. Mark-and-sweep is also a more versatile algorithm in the sense that it can handle more complex memory management scenarios like circular references where two or more objects references to each other.

--

--