Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the golang gc and trigger it manually?

Tags:

Currently I'm supporting big table join on a database written in golang. But the gc costs too much time. I want to close the go gc and trigger it manually. How to config the go build args?

like image 971
Han Fei Avatar asked Aug 16 '16 10:08

Han Fei


People also ask

How do I stop garbage collection in Go?

You can disable GC and control when the GC has to be called in the runtime, by setting the environmental variable 'GOGC'. eg: GOGC=off disables the garbage collection.

Is Golang GC stop the world?

The Go GC, however, is not fully stop-the-world and does most of its work concurrently with the application.

Does Golang have automatic garbage collection?

Go has all goroutines reach a garbage collection safe point with a process called stop the world. This temporarily stops the program from running and turns a write barrier on to maintain data integrity on the heap. This allows for concurrency by allowing goroutines and the collector to run simultaneously.

How often does Go GC run?

It's a heuristic. The Go developers probably observed that starting a GC approximately every two minutes was a reasonable trade-off between the overhead of a GC cycle and memory used.


1 Answers

Package documentation of runtime contains all the details you need:

The GOGC variable sets the initial garbage collection target percentage. A collection is triggered when the ratio of freshly allocated data to live data remaining after the previous collection reaches this percentage. The default is GOGC=100. Setting GOGC=off disables the garbage collector entirely. The runtime/debug package's SetGCPercent function allows changing this percentage at run time. See https://golang.org/pkg/runtime/debug/#SetGCPercent.

So you may set the environment variable GOGC to a percent which is the ratio of freshly allocated data to live data remaining after the previous collection.

When the above data ratio reaches the value of GOGC, a (garbage) collection is initiated. The initial setting is taken from the GOGC env variable, or 100 if the variable is not set. The value off disables garbage collection.

At runtime you can change the GOGC ratio by calling debug.SetGCPercent(), pass a negative value to disable it:

debug.SetGCPercent(-1) 

You may trigger a garbage collection "manually" with runtime.GC().

Completely disabling GC might not be what you want though. Read the complete package doc of runtime where you find details about how to fine-tune GC and how to trace GC runs. Analyze them and act accordingly.

Also note that Go 1.7 was released today with improved garbage collector:

Programs should run a bit faster due to speedups in the garbage collector and optimizations in the standard library. Programs with many idle goroutines will experience much shorter garbage collection pauses than in Go 1.6.

If you haven't, first test your application compiled with Go 1.7 before taking any further action.

like image 131
icza Avatar answered Oct 18 '22 19:10

icza