简体   繁体   中英

How to stop the golang gc and trigger it manually?

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?

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.

To programmatically disable the GC, you need two steps:

  • Set the GC percentage to a negative value
  • Set the memory limit to a very high value

Both facilities are provided by package "runtime/debug"

debug.SetGCPercent(-1)
debug.SetMemoryLimit(math.MaxInt64)

Both functions return their previous settings, so if you want to just temporarily disable the GC, one way to do it is capture these values and then reset them when you want to enable it again.


gcpercent := debug.SetGCPercent(-1)
memlimit := debug.SetMemoryLimit(math.MaxInt64)

{
    // ... do something ...
}

debug.SetGCPercent(gcpercent)
debug.SetMemoryLimit(memlimit)

Note that just setting the GC percentage is not enough to stop the GC, because it will still be triggered when you exceed the memory limit.

As per the docs:

A negative percentage effectively disables garbage collection, unless the memory limit is reached. See SetMemoryLimit for more details.

The documentation for SetMemoryLimit says:

The memory limit is always respected by the Go runtime, so to effectively disable this behavior, set the limit very high. math.MaxInt64 is the canonical value for disabling the limit, but values much greater than the available memory on the underlying system work just as well.

Now, in practicle you might be able to get away without setting the memory limit, because the default initial vlaue is indeed math.MaxInt64 which effectively disables it.

The initial setting is math.MaxInt64 unless the GOMEMLIMIT environment variable is set, in which case it provides the initial setting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM