Garbage Collection
Let’s learn about garbage collection in Go.
We'll cover the following
Garbage collection is the process of freeing up memory space that is not being used. In other words, the GC sees which objects are out of scope and cannot be referenced anymore and frees the memory space they consume. This process happens in a concurrent way while a Go program is running and not before or after the execution of the program. The documentation of the Go GC implementation states the following:
“The GC runs concurrently with mutator threads, is type accurate (also known as precise), and allows multiple GC threads to run in parallel. It is a concurrent mark-and-sweep that uses a write barrier. It is non-generational and non-compacting. Allocation is done using size segregated per P allocation areas to minimize fragmentation while eliminating locks in the common case.”
Coding example
Fortunately, the Go standard library offers functions that allow us to study the operation of the GC and learn more about what the GC covertly does. These functions are illustrated in the gColl.go
utility. The source code of gColl.go
is presented here in chunks.
Get hands-on with 1400+ tech skills courses.