gctuner

package
v0.0.0-...-b554abd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 11 Imported by: 0

README

gctuner

Introduction

Inspired by How We Saved 70K Cores Across 30 Mission-Critical Services (Large-Scale, Semi-Automated Go GC Tuning @Uber) .

 _______________  => limit: host/cgroup memory hard limit
|               |
|---------------| => gc_trigger: heap_live + heap_live * GCPercent / 100
|               |
|---------------|
|   heap_live   |
|_______________|

Go runtime trigger GC when hit gc_trigger which affected by GCPercent and heap_live.

Assuming that we have stable traffic, our application will always have 100 MB live heap, so the runtime will trigger GC once heap hits 200 MB(by default GOGC=100). The heap size will be changed like: 100MB => 200MB => 100MB => 200MB => ....

But in real production, our application may have 4 GB memory resources, so no need to GC so frequently.

The gctuner helps to change the GOGC(GCPercent) dynamically at runtime, set the appropriate GCPercent according to current memory usage.

How it works
 _______________  => limit: host/cgroup memory hard limit
|               |
|---------------| => threshold: increase GCPercent when gc_trigger < threshold
|               |
|---------------| => gc_trigger: heap_live + heap_live * GCPercent / 100
|               |
|---------------|
|   heap_live   |
|_______________|

threshold = inuse + inuse * (gcPercent / 100)
=> gcPercent = (threshold - inuse) / inuse * 100

if threshold < 2*inuse, so gcPercent < 100, and GC positively to avoid OOM
if threshold > 2*inuse, so gcPercent > 100, and GC negatively to reduce GC times

Usage

The recommended threshold is 70% of the memory limit.


// Get mem limit from the host machine or cgroup file.
limit := 4 * 1024 * 1024 * 1024
threshold := limit * 0.7

gctuner.Tuning(threshold)

// Friendly input
gctuner.TuningWithFromHuman("1g")

// Auto
// There may be problems with multiple services in one pod.
gctuner.TuningWithAuto(false) // Is it a container? Incoming Boolean

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGcPercent

func GetGcPercent() uint32

GetGcPercent 获取当前的GC百分比

func GetMaxGCPercent

func GetMaxGCPercent() uint32

GetMaxGCPercent 获取最大GC百分比值

func GetMinGCPercent

func GetMinGCPercent() uint32

GetMinGCPercent 获取最小GC百分比值

func SetMaxGCPercent

func SetMaxGCPercent(percent uint32) uint32

SetMaxGCPercent 设置新的最大GC百分比值

func SetMinGCPercent

func SetMinGCPercent(percent uint32) uint32

SetMinGCPercent 设置新的最小GC百分比值

func Tuning

func Tuning(threshold uint64)

Tuning 设置GC调优的内存阈值 当进行调优时,环境变量GOGC将不再生效 threshold: 如果为0则禁用调优

func TuningWithAuto

func TuningWithAuto(isContainer bool)

TuningWithAuto 通过自动计算总内存量来设置阈值

func TuningWithFromHuman

func TuningWithFromHuman(threshold string)

TuningWithFromHuman 使用人类可读的字符串格式设置阈值 例如: "b/B", "k/K" "kb/Kb" "mb/Mb", "gb/Gb" "tb/Tb" "pb/Pb"

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL