blog

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 at https://www.packtpub.com/networking-and-servers/mastering-go.
Want to start writing UNIX system tools? Get my book Go Systems Programming at https://www.packtpub.com/networking-and-servers/go-systems-programming or from Amazon.com (https://www.amazon.com/Go-Systems-Programming-Master-programming/dp/1787...).

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

Tags: 
mihalis's picture

2017 Resolutions

.
.
.
.

  • Read more books.
  • Take more risks.
  • Study CS related research subjects.
  • Write a book!
  • Spend less time on the Internet.
  • Take more photographs.
  • Print more photographs.
  • Run faster!
mihalis's picture

The Baby Elephant Syndrome

"When an elephant living in captivity is still a baby, it is tied to a tree with a strong rope or a chain every night. Because it is the nature of elephants to roam free, the baby elephant instinctively tries with all its might to break the rope. But it isn’t yet strong enough to do so. Realizing its efforts are of no use, it finally gives up and stops struggling. The baby elephant tries and fails many times, it will never try again for the rest of its life.
Later, when the elephant is fully grown, it can be tied to a small tree with a thin rope. It could then easily free itself by uprooting the tree or breaking the rope. But because its mind has been conditioned by its prior experiences, it doesn’t make the slightest attempt to break free. The powerfully gigantic elephant has limited its present abilities by the limitations of the past—-hence, the Baby Elephant Syndrome."

Tags: 
mihalis's picture

MongoDB: WiredTiger or MMAPv1?

Although it is easy to find out whether you are using WiredTiger or MMAPv1 by the filenames in the data directory, there is a better way to discover the Storage Engine your MongoDB database is using.
The next command returns the Storage Engine a MongoDB server is using:

> db.serverStatus().storageEngine
{ "name" : "mmapv1", "supportsCommittedReads" : false }

If you are using WiredTiger, the output would have been similar to the following:

> db.serverStatus().storageEngine
{ "name" : "wiredTiger", "supportsCommittedReads" : true }
> db.serverStatus().storageEngine.name
wiredTiger

If you are using WiredTiger, the following command will give you even more information:

> db.serverStatus().wiredTiger

Pages

Subscribe to RSS - blog