Skip to content

port MaxRAMPercentage to Golang, adjust GC parameters(SetGCPercent/SetMemoryLimit) based on the target memory usage percentage, optimize the GC overhead of Go runtime.

License

Notifications You must be signed in to change notification settings

fangwentong/gogctuner

Repository files navigation

gogctuner: GC Tuner for Go Applications

gogctuner is a Go library designed to automatically adjust the garbage collection (GC) parameters to optimize CPU usage based on the target memory usage percentage.

How it works

  • Pre Go1.19 Versions: Utilizes a tuning strategy inspired by Uber's article, which adjusts the GOGC parameter based on the live heap size and target memory limit.
  • Go1.19 and Later: Takes advantage of the soft memory limit feature introduced in Go 1.19, offering more granular control over GC behavior.

Features

  • Memory Usage Based Tuning: Automatically adjusts GC parameters to maintain a specified percentage of memory usage.
  • Cross-Version Compatibility: Compatible with Go versions below 1.19 and leverages the SetMemoryLimit features in Go 1.19 and above.
  • cgroups Support: Works seamlessly with both cgroups and cgroupsv2 for enhanced resource management.
  • Cross-OS Compatibility: Ensures functionality across multiple operating systems.

Usage

Static Configuration

Use static configuration by setting the MaxRAMPercentage at the initialization of your application:

package main

import (
  "github.com/fangwentong/gogctuner"
)

func main() {
  gogctuner.EnableGCTuner(
    gogctuner.WithStaticConfig(gogctuner.Config{MaxRAMPercentage: 90}),
  )
  // Your application code here
}

Dynamic Configuration

For dynamic configuration that allows runtime updates, you can use a configurator:

package main

import (
  "github.com/fangwentong/gogctuner"
)

func main() {
  configurator := gogctuner.NewGcConfigurator()

  // Integrate with your dynamic config implementation here:
  conf := readFromYourConfigCenter("your_config_key")
  configurator.SetConfig(conf) // set initial config
  // register config updates callback
  registerConfigUpdateCallback("your_config_key", func(conf gogctuner.Config) {
    configurator.SetConfig(conf)
  })

  gogctuner.EnableGCTuner(
    gogctuner.WithConfigurator(configurator),
  )
  // Your application code here
}

Reference

License

This project is licensed under the MIT License, see the LICENSE file for details.

About

port MaxRAMPercentage to Golang, adjust GC parameters(SetGCPercent/SetMemoryLimit) based on the target memory usage percentage, optimize the GC overhead of Go runtime.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages