Fundamentals 31 min read

Evolution of Garbage Collection in Go: From Mark‑Sweep to Hybrid Write Barriers

This article explains how Go's garbage collection has progressed from the simple stop‑the‑world mark‑and‑sweep algorithm in Go 1.3, through the tri‑color concurrent marking with insert and delete write barriers in Go 1.5, to the hybrid write‑barrier approach introduced in Go 1.8 that dramatically reduces pause times while preserving memory safety.

Xueersi Online School Tech Team
Xueersi Online School Tech Team
Xueersi Online School Tech Team
Evolution of Garbage Collection in Go: From Mark‑Sweep to Hybrid Write Barriers

Garbage Collection (GC) is an automatic memory‑management mechanism that frees unreachable objects without programmer intervention. Modern languages, including Go, rely on GC, and its performance is a key comparison metric.

Go 1.3 – Mark‑and‑Sweep : The classic algorithm consists of a Mark phase (identifying reachable objects) and a Sweep phase (reclaiming the rest). It requires a Stop‑The‑World (STW) pause, causing noticeable latency, and suffers from heap fragmentation.

Go 1.5 – Tri‑color Concurrent Marking : Introduces three colors (white, gray, black) to allow concurrent marking with reduced STW. Two invariants are defined:

Strong invariant : Black objects never reference white objects.

Weak invariant : White objects referenced by black objects must be protected by a gray object.

To enforce these invariants, Go uses two write‑barrier mechanisms.

Insert Barrier (pseudo‑code) :

添加下游对象(当前下游对象slot, 新下游对象ptr) {   
  //1
  标记灰色(新下游对象ptr)   
  //2
  当前下游对象slot = 新下游对象ptr                      
}

Usage example:

//A 之前没有下游,新添加一个下游对象B,B被标记为灰色
A.添加下游对象(nil, B)   

//A 将下游对象C 更换为B,B被标记为灰色
A.添加下游对象(C, B)

Delete Barrier (pseudo‑code) :

添加下游对象(当前下游对象slot, 新下游对象ptr) {
  //1
  if (当前下游对象slot是灰色 || 当前下游对象slot是白色) {
        //slot为被删除对象, 标记为灰色
        标记灰色(当前下游对象slot)     
  }
  //2
  当前下游对象slot = 新下游对象ptr
}

Usage example:

//A对象,删除B对象的引用。B被A删除,被标记为灰(如果B之前为白)
A.  添加下游对象(B, nil)   

//A对象,更换下游B变成C。B被A删除,被标记为灰(如果B之前为白)
A.  添加下游对象(B, C)

While both barriers improve concurrency, they still require an additional STW to rescan the stack (insert barrier) or suffer from delayed reclamation (delete barrier).

Go 1.8 – Hybrid Write Barrier : Combines the advantages of both barriers and eliminates the need for a stack rescan. Rules include:

All stack objects are scanned and marked black at GC start (no second scan).

New stack objects are black.

Objects removed or added are marked gray.

Hybrid barrier pseudo‑code:

添加下游对象(当前下游对象slot, 新下游对象ptr) {
        //1 
        标记灰色(当前下游对象slot)   //只要当前下游对象被移走,就标记灰色
        //2 
        标记灰色(新下游对象ptr)
        //3
        当前下游对象slot = 新下游对象ptr
}

The article walks through four illustrative scenarios (heap‑to‑stack, stack‑to‑stack, heap‑to‑heap, stack‑to‑heap) showing how the hybrid barrier protects objects without additional STW pauses, while still guaranteeing safety via gray‑object protection.

In conclusion, Go's GC has evolved from a simple stop‑the‑world mark‑sweep to a sophisticated tri‑color concurrent collector with hybrid write barriers, dramatically reducing pause times and improving overall runtime performance.

GoruntimeGarbage CollectionSTWMark and Sweeptri-color markingwrite barrier
Xueersi Online School Tech Team
Written by

Xueersi Online School Tech Team

The Xueersi Online School Tech Team, dedicated to innovating and promoting internet education technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.