Garbage collection in go. Which algorithm does go uses to clean memory?

Quick tech learn
2 min readAug 26, 2023

--

Go uses a variant of mark and sweep garbage collector algorithm.

In the realm of computer science and programming, managing memory efficiently is very essential for the performance and stability of any software product. One essential method employed in memory management is garbage collection, a process responsible for identifying and cleaning memory that is no longer used by the currently running code.

There are many GC(Garbage Collection) algorithms in use. Among them, golang uses the most fundamental “Mark and Sweep” algorithm.

In this blog post, we will delve into the workings of Mark and Sweep, exploring its principles, advantages, and limitations.

Mark and Sweep in a Nutshell

The Mark and Sweep garbage collection algorithm is a straightforward but effective method for identifying and cleaning up unreachable objects in memory. It operates in two distinct phases: marking and sweeping.

  1. Mark Phase: During this phase, the algorithm traverses the entire memory space, starting from a designated set of roots (typically global variables, stack frames, or registers) and marks all objects that are still reachable as true or 1.
  2. Sweep Phase: Once the marking phase is complete, the sweep phase takes over. It iterates through the entire memory space again. Any object which is set to false or 0 is considered unreachable and can be safely deallocated.

Once the sweep phase is over, again the algorithm sets the flag of all objects to 0 so the the next time when the algorithm runs, it can start from its Mark Phase.

Advantages of Mark and Sweep

  1. Simplicity: One of the primary advantages of the Mark and Sweep algorithm is its simplicity. The two-phase process is conceptually straightforward, making it easier to implement and understand compared to more complex algorithms.
  2. No Fragmentation: Mark and Sweep doesn’t suffer from fragmentation issues, as it cleans up memory in a contiguous block. This prevents memory fragmentation, a problem that occurs in some other garbage collection algorithms.
  3. Predictable Pauses: Mark and Sweep provides predictable pause times. Once it starts, it collects all the garbage at once, ensuring that memory is freed in a single, well-defined step. This predictability is beneficial for real-time and latency-sensitive applications.

Limitations of Mark and Sweep

  1. Pause Times: While Mark and Sweep has predictable pause times, they can still be relatively long in applications with large heaps. During the sweep phase, the algorithm must examine every object, which can result in noticeable pauses.
  2. Fragmentation: While it avoids fragmentation within the memory blocks it collects, Mark and Sweep does not address memory fragmentation between those blocks. Over time, this can lead to inefficient memory usage.
  3. Inefficient for Small Heaps: In scenarios with small heaps, Mark and Sweep might not be the most efficient choice. The overhead of marking and sweeping may outweigh the benefits of garbage collection.

--

--

Quick tech learn

Blogs are written by a fullstack developer with experience in tech stack like golang, react, SQL, mongoDB and cloud services like AWS