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?
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.
The Go GC, however, is not fully stop-the-world and does most of its work concurrently with the application.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With