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

Go · Advanced · question 41 of 100

How does Go’s garbage collector work? What is a "tricolor mark and sweep" algorithm?

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

Go’s garbage collector (GC) is a concurrent, tricolor, mark-and-sweep collector. This means that it operates concurrently with the program execution, allowing the program to continue running while the GC is working. The tricolor mark and sweep algorithm uses three colors (white, grey, and black) to separate objects into different states during the garbage collection process.

The high-level description of the tricolor mark and sweep algorithm in Go is as follows:

1. Initially, all objects are colored white, which represents objects that have not been visited by the garbage collector.

2. The garbage collector starts at the application’s "roots" (global variables, thread-local storages, and the stacks of running goroutines), marking them as grey, meaning they’re reachable, but their descendants might not have been visited yet.

3. The garbage collector then iterates over each grey object, visiting their descendants:

a. If a descendant is white, meaning it hasn’t been visited before, it is marked as grey.

b. Once all descendants of the grey object have been visited, the object itself is marked as black.

4. The garbage collector continues to perform the third step until there are no more grey objects left.

5. White objects are unreachable after the mark phase completes, and they will be reclaimed in the sweep phase.

To illustrate the tricolor mark and sweep algorithm, let’s visualize an example. Before the GC starts, all objects are white:

A (white) -> B (white) -+-> D (white)
                          |
C (white) ----------------+

The first step is to mark roots, A and C, as grey:

A (grey) -> B (white) -+-> D (white)
                        |
C (grey) --------------+

Now, the GC iterates over grey objects, marking their descendants. Let’s start by visiting A and mark its child B:

A (black) -> B (grey) -+-> D (white)
                         |
C (grey) ---------------+

Next, we visit B and mark its child D:

A (black) -> B (black) -+-> D (grey)
                          |
C (grey) ----------------+

Now, we visit D and mark its child C:

A (black) -> B (black) -+-> D (black)
                          |
C (grey) ----------------+

Finally, we visit C:

A (black) -> B (black) -+-> D (black)
                          |
C (black) ---------------+

At this point, there are no grey objects left. Objects A, B, C, and D are all black (reachable), so no objects will be reclaimed in the sweep phase in this example.

The tricolor mark and sweep algorithm allows Go’s garbage collector to run concurrently with the program to some extent, which significantly improves performance. However, the concurrent aspect of Go’s GC is not perfect, and it introduces some latency as the garbage collector must occasionally pause the application execution to ensure accurate marking, a process known as "stop-the-world". Go’s garbage collector is designed to minimize these pauses, making them generally unnoticeable in most applications.

In summary, Go’s garbage collector is based on the concurrent tricolor mark and sweep algorithm, which separates objects into three colors (white, grey, and black) to determine their reachability. The garbage collector runs concurrently with the program, marking objects while the program is executing and minimizing global pauses as much as possible.

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