mihalis's picture

The Go Garbage Collector (GC)

Garbage Collection is the process of freeing memory space that is not being used. In other words, the garbage collector sees which objects are out of scope and cannot be referenced any more and frees the memory space they consume. This process happens in a concurrent way while a Go program is running, not before or after the execution of a Go program. The operation of the Go GC is based on the tricolor algorithm.

Strictly speaking the official name for the algorithm used in Go is tricolor mark-and-sweep algorithm, can work concurrently with the program and uses a write barrier. This means that when a Go program runs, the Go scheduler is responsible for the scheduling of the application and the garbage collector as if the Go scheduler had to deal with a regular application with multiple goroutines!

The core idea behind this algorithm belongs to Edsger W. Dijkstra, Leslie Lamport, A. J. Martin, C. S. Scholten and E. F. M. Steffens and was first illustrated on a paper named On-the-fly garbage collection: an exercise in cooperation.

The primary principle behind the tricolor mark-and-sweep algorithm is that it divides the objects of the heap into three different sets according to their color, which is assigned by the algorithm. The objects of the black set are guaranteed to have no pointers to any object of the while set. However, an object of the white set can have a pointer to an object of the black set because this has no effect on the operation of the GC! The objects of the grey set might have pointers to some objects of the while set. Last, the objects of the white set are the candidates for garbage collection.

So, when the garbage collection begins, all objects are white and the garbage collector visits all the root objects and colors them grey – the roots are the objects that can be directly accessed by the application, which includes global variables and other things on the stack – these objects mostly depend on the Go code of a particular program. After that, the garbage collector picks a grey object, makes it black and starts searching if that object has pointers to other objects of the white set. This means that when a grey object is being scanned for pointers to other objects, it is colored black. If that scan discovers that this particular object has one or more pointers to a white object, it puts that white object to the grey set. This process keeps going for as long as there exist objects in the grey set. After that, the objects in the white set are unreachable and their memory space can be reused. Therefore, at this point the elements of the white set is said to be garbage collected.

Go allows you to manually initiate a garbage collection by putting a runtime.GC() statement in your Go code. However, have in mind that runtime.GC() will block the caller and it might block the entire program, especially if you are running a very busy Go program with many objects. This mainly happens because you cannot perform garbage collections while everything else is rapidly changing as this will not give the garbage collector the opportunity to clearly identify the members of the while, black and grey sets! This garbage collection status is also called garbage collection safe-point.

Want to learn more about the Go Garbage Collector? Get my book Mastering Go from Packt,,, or any other Amazon store.
Want to start writing UNIX system tools? Get my book Go Systems Programming from Packt,,, or any other Amazon store.

mihalis's picture

Top 20 of words in my Go code

I wanted to find out the most popular words in my Go code, so I have decided to use the UNIX shell to do so.

$ find dir -name "*.go" -exec cat {} \; | tr ' ' '\n' | sed '/^$/d' | sed 's/[ \t]*//' | sed 's/ //g' | sort | uniq -c | sort -rn | head -20

At the time of writing this, the output of the previous command was the following:

1815 {
1368 :=
831 }
628 err
608 }
573 =
552 func
488 if
434 nil
368 !=
298 package
298 import
295 )
292 (
290 main()
290 main
284 "fmt"
267 ==
251 }
241 for

So, the most popular word is err, which makes perfect sense! Then it was func, if, nil as well as package and import.

After that I used Excel to make a simple graph.

mihalis's picture

Go Systems Programming book FAQ

1. Does the book have a web page?

2. Is it a book for amateur programmers?
If you already know how to program, then you will not have any difficulties reading the book.

3. Which Go version should I have?
Any Go version will do. However, there are some specific Go features that need a recent Go version (1.8 or newer).

4. Where can I find the code for the book?

5. Is it for UNIX only?
The book is mainly for UNIX machines, which includes Linux, macOS, Mac OS X, *BSD, etc. However, most of the code should work fine on Windows machines.

6. Where can I buy it?
Everywhere including, and

7. Is it possible to contact you about the book?
Yes – use the contact form ( or Twitter (

8. What are you doing now?
I am currently writing another book on Go!

9. Have more questions?
Contact me using

mihalis's picture

Using tar

This blog post will try to show you how to use the tar utility.

Look at the contents of the code directory:

$ ls -l code/
total 24
-rw-r--r--@ 1 mtsouk staff 50 Nov 16 18:12 generatePassword.go
-rw-r--r--@ 1 mtsouk staff 50 Nov 16 18:12 randomNumbers.go
-rw-r--r--@ 1 mtsouk staff 50 Nov 16 18:12 stack.go

You can create a new tar file as follows:

$ tar zcvf code.tar.gz code
a code
a code/generatePassword.go
a code/randomNumbers.go
a code/stack.go
$ ls -l code.tar.gz
-rw-r--r-- 1 mtsouk staff 494 Nov 24 22:38 code.tar.gz

The preceding file is also compressed using the gzip utility because of the z option.
You can list the contents of an existing tar file (t) as follows:

$ tar ztvf code.tar.gz
drwxr-xr-x 0 mtsouk staff 0 Nov 10 20:32 code/
-rw-r--r-- 0 mtsouk staff 50 Nov 16 18:12 code/generatePassword.go
-rw-r--r-- 0 mtsouk staff 50 Nov 16 18:12 code/randomNumbers.go
-rw-r--r-- 0 mtsouk staff 50 Nov 16 18:12 code/stack.go

You can extract (x) an existing tar file as follows:

$ tar zxvf code.tar.gz
x code/
x code/generatePassword.go
x code/randomNumbers.go
x code/stack.go

mihalis's picture

Why I write

As you might have noticed, I write a lot and there are many reasons that I do so:

  • I like the writing process!
  • I enjoy writing, especially when I finish writing about a topic.
  • When I write about something, I learn more things about it.
  • I like helping other people learn new things.
  • Although writing is not an easy task, It feels natural to me after so many years. However, it was not that easy in the beginning.

One more thing that I forgot to mention: from time to time I read my writings!



Subscribe to RSS - blogs