KimMachineGun/automemlimit

Automatically set GOMEMLIMIT to match Linux cgroups(7) memory limit.

Go

566

76 commits

updated Sep 2, 2026

See the code

README

automemlimit

Go Reference Test

Automatically set GOMEMLIMIT based on the Linux cgroups(7) memory limit.

See more details about GOMEMLIMIT here.

Notice

Version v1.0.0 simplifies the public API and improves cgroup memory limit detection.

Key changes from v0.x:

  • memlimit.Set replaces the memlimit.SetGoMemLimit* functions
  • memlimit.Set returns the previous GOMEMLIMIT instead of 0 when configuration is skipped or an error occurs
  • memlimit.Set sets GOMEMLIMIT to math.MaxInt64 when the provider returns memlimit.ErrNoLimit
  • memlimit.FromCgroup replaces the version-specific cgroup providers
  • memlimit.WithEnv, AUTOMEMLIMIT_EXPERIMENT, and AUTOMEMLIMIT_DEBUG were removed
    • Use memlimit.FromSystem for system memory fallback
  • memlimit.WithRefreshInterval now takes a context.Context for cancellation
  • memlimit.WithMin was added to set a lower bound for GOMEMLIMIT

Installation

go get github.com/KimMachineGun/automemlimit@latest

Usage

package main

// Importing this package sets GOMEMLIMIT automatically from the cgroup memory limit.
// By default, it sets GOMEMLIMIT to 90% of the cgroup memory limit.
// Set the AUTOMEMLIMIT environment variable to a ratio in (0.0, 1.0], or "off".
import _ "github.com/KimMachineGun/automemlimit"

For more control, use memlimit.Set directly:

package main

import (
	"log"

	"github.com/KimMachineGun/automemlimit/memlimit"
)

func main() {
	_, err := memlimit.Set(
		memlimit.WithRatio(0.9),
		memlimit.WithMin(100*1024*1024),
		memlimit.WithProvider(
			memlimit.ApplyFallback(
				memlimit.FromCgroup,
				memlimit.FromSystem,
			),
		),
	)
	if err != nil {
		log.Fatal(err)
	}
}
golang
golang-gc

Significant stargazers

Felix Geisendörfer

4,235 followers · starred Jan 2023

Shunsuke Suzuki

610 followers · starred Apr 2024

dongjiang

72 followers · starred Jan 2025

Pranshu Srivastava

151 followers · starred Aug 2024

KimMachineGun/automemlimit

Automatically set GOMEMLIMIT to match Linux cgroups(7) memory limit.

Go

566

76 commits

updated Sep 2, 2026

See the code

README

automemlimit

Go Reference Test

Automatically set GOMEMLIMIT based on the Linux cgroups(7) memory limit.

See more details about GOMEMLIMIT here.

Notice

Version v1.0.0 simplifies the public API and improves cgroup memory limit detection.

Key changes from v0.x:

  • memlimit.Set replaces the memlimit.SetGoMemLimit* functions
  • memlimit.Set returns the previous GOMEMLIMIT instead of 0 when configuration is skipped or an error occurs
  • memlimit.Set sets GOMEMLIMIT to math.MaxInt64 when the provider returns memlimit.ErrNoLimit
  • memlimit.FromCgroup replaces the version-specific cgroup providers
  • memlimit.WithEnv, AUTOMEMLIMIT_EXPERIMENT, and AUTOMEMLIMIT_DEBUG were removed
    • Use memlimit.FromSystem for system memory fallback
  • memlimit.WithRefreshInterval now takes a context.Context for cancellation
  • memlimit.WithMin was added to set a lower bound for GOMEMLIMIT

Installation

go get github.com/KimMachineGun/automemlimit@latest

Usage

package main

// Importing this package sets GOMEMLIMIT automatically from the cgroup memory limit.
// By default, it sets GOMEMLIMIT to 90% of the cgroup memory limit.
// Set the AUTOMEMLIMIT environment variable to a ratio in (0.0, 1.0], or "off".
import _ "github.com/KimMachineGun/automemlimit"

For more control, use memlimit.Set directly:

package main

import (
	"log"

	"github.com/KimMachineGun/automemlimit/memlimit"
)

func main() {
	_, err := memlimit.Set(
		memlimit.WithRatio(0.9),
		memlimit.WithMin(100*1024*1024),
		memlimit.WithProvider(
			memlimit.ApplyFallback(
				memlimit.FromCgroup,
				memlimit.FromSystem,
			),
		),
	)
	if err != nil {
		log.Fatal(err)
	}
}
golang
golang-gc

Significant stargazers

Felix Geisendörfer

4,235 followers · starred Jan 2023

Shunsuke Suzuki

610 followers · starred Apr 2024

dongjiang

72 followers · starred Jan 2025

Pranshu Srivastava

151 followers · starred Aug 2024