Go

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, Amazon.com, Amazon.co.uk, or any other Amazon store.
Want to start writing UNIX system tools? Get my book Go Systems Programming from Packt, Amazon.com, Amazon.co.uk, or any other Amazon store.

Tags: 
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.

Image: 
Tags: 
mihalis's picture

Go Systems Programming book FAQ

1. Does the book have a web page?
Yes: https://www.packtpub.com/networking-and-servers/go-systems-programming

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?
https://github.com/PacktPublishing/Go-Systems-Programming

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 https://www.packtpub.com/networking-and-servers/go-systems-programming, https://www.amazon.co.uk/Go-Systems-Programming-Master-programming/dp/17... and https://www.amazon.com/Go-Systems-Programming-Master-programming/dp/1787....

7. Is it possible to contact you about the book?
Yes – use the contact form (http://www.mtsoukalos.eu/contact) or Twitter (https://twitter.com/mactsouk).

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

9. Have more questions?
Contact me using http://www.mtsoukalos.eu/contact.

mihalis's picture

Go Systems Programming TOC

1: GETTING STARTED WITH GO AND UNIX SYSTEMS PROGRAMMING
2: WRITING PROGRAMS IN GO
3: ADVANCED GO FEATURES
4: GO PACKAGES, ALGORITHMS, AND DATA STRUCTURES
5: FILES AND DIRECTORIES
6: FILE INPUT AND OUTPUT
7: WORKING WITH SYSTEM FILES
8: PROCESSES AND SIGNALS
9: GOROUTINES - BASIC FEATURES
10: GOROUTINES - ADVANCED FEATURES
11: WRITING WEB APPLICATIONS IN GO
12: NETWORK PROGRAMMING

You can get an even more detailed TOC at https://www.packtpub.com/networking-and-servers/go-systems-programming

mihalis's picture

Source code of Go Systems Programming

You can find the Go code of the "Go Systems Programming" book at https://github.com/PacktPublishing/Go-Systems-Programming

Image: 

Pages

Subscribe to RSS - Go