WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Guru · question 82 of 100

How does Go’s garbage collector work under the hood? Can you explain its design and implementation?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

Go’s garbage collector (GC) is a concurrent, tri-color, mark-sweep collector with a write barrier, implemented using a combination of concurrent mark phases and a stop-the-world (STW) phase for the final sweep. This design’s main goals are to keep the garbage collection pause times low and maintain decent performance even as the heap grows.

The garbage collector consists of four main components: the Mark phase, the Scan phase, the Install write barrier phase, and the Sweep phase. Let’s discuss each of these phases in detail:

1. **Mark phase**: The Mark phase identifies all live objects in the heap, meaning objects that are still in use by the program. It traverses the object graph starting from the "root set" – a set of objects directly accessible by the program, such as global variables, local variables on the stack, and other runtime data structures.

During the marking process, Go GC uses a tri-color abstraction to classify objects:

- *White*: An object which has not been visited yet.

- *Gray*: An object which has been visited but its children haven’t been visited.

- *Black*: An object which has been visited and its children have been visited.

Initially, all objects are white. When an object is visited, it becomes gray, and when its children have been visited, it becomes black. The marking process ends when no gray objects are left.

2. **Scan phase**: While the Mark phase operates concurrently with the program, the Scan phase is a short STW pause in which the GC scans the stacks and marks additional objects as live. This step is necessary because the program might have created new objects during the Mark phase.

3. **Install write barrier phase**: After the Scan phase, the GC installs a write barrier, which is a mechanism to maintain the tri-color invariant during the concurrent marking process. The write barrier intercepts all pointers modifications: if a black object points to a white object, it will mark the white object as gray to ensure that no white objects are incorrectly collected.

4. **Sweep phase**: The Sweep phase is also concurrent with the program; it reclaims the memory occupied by the white objects that were not marked as live during the Mark phase. It iterates through the heap and frees up memory, updating the allocator’s data structures to make the memory available for future allocation.

The GC algorithm adjust the heap size according to the garbage collection’s performance in previous cycles. If the garbage collector takes too much time, the heap size is increased, allowing the program to allocate more memory before the next GC cycle starts. Conversely, if the garbage collection is fast, the heap size might be reduced to save memory.

To visualize the garbage collection process, consider the following chart showing the different phases of Go’s garbage collector:

In summary, Go’s garbage collector is designed to minimize pauses and maintain a balance between pause-times, throughput, and memory overhead. It uses a concurrent, tri-color, mark-sweep algorithm in conjunction with a write barrier and a combination of concurrent and stop-the-world phases to achieve this goal.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics