akai
akai

Reputation: 2052

How to increase Go's GC CPU usage to more than 25%?

According to some articles (like this and this), Go's garbage collector can only take up to 25% of available CPUs, and it seems that this number is actually the result of the golang developers hard work - they're trying to make this number rather small.

However, since in my case I want to run GC at specific timings (by runtime.GC()) and I also want the GC finish as soon as possible, I wonder if it's possible to make Go's GC use up to, say, 100% of available CPUs, so that the GC finishes earlier.

Is this possible?

Context

My code has deterministic busy and idle times. Since the busy part should run very fast (it's too slow if GC is fired), I want to run GC within the idle times, which are also relatively short and so the GC should also run fast. I'm not going to non-GC languages because hard real-time isn't required and I'm not so smart to manage memory correctly.

Upvotes: 1

Views: 1798

Answers (2)

Ashutosh Pandey
Ashutosh Pandey

Reputation: 243

Have you tried tweaking the GOGC parameter. If your server has more memory with the help of GOGC you can control when to run GC. typically, GC triggers when heap memory gets doubled against the heap memory which was at the last GC run. if you increase GOGC memory will increase but CPU utilisation reduces and vice-versa.

Refer - https://tip.golang.org/doc/gc-guide

Upvotes: 1

akai
akai

Reputation: 2052

I found it. If I set GODEBUG=gcstoptheworld=1 or GODEBUG=gcstoptheworld=2, then the manual GC utilizes all the available CPUs, although this apparently disables concurrent GC. Ref: https://golang.org/pkg/runtime/

In my case this was enough. But I actually want to know how to utilize more CPUs when the concurrent GC works. It seems that simply increasing the values of gcGoalUtilization and gcBackgroundUtilization in runtime/mgc.go and buiding go from source does not work. Hmm...

EDIT

This definitely achieved better CPU utilizations, but in fact I could not see a noticeable speedup (according to go tool trace). Maybe I'm missing something.

Upvotes: 3

Related Questions