Skip to content

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

License

Notifications You must be signed in to change notification settings

KimMachineGun/automemlimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

automemlimit

Go Reference Go Report Card Test

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

See more details about GOMEMLIMIT here.

Notice

Version v0.5.0 introduces a fallback to system memory limits as an experimental feature when cgroup limits are unavailable. Activate this by setting AUTOMEMLIMIT_EXPERIMENT=system. You can also use system memory limits via memlimit.FromSystem provider directly.

This feature is under evaluation and might become a default or be removed based on user feedback. If you have any feedback about this feature, please open an issue.

Installation

go get github.com/KimMachineGun/automemlimit@latest

Usage

package main

// By default, it sets `GOMEMLIMIT` to 90% of cgroup's memory limit.
// This is equivalent to `memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.Default()))`
// To disable logging, use `memlimit.SetGoMemLimitWithOpts` directly.
import _ "github.com/KimMachineGun/automemlimit"

or

package main

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

func init() {
	memlimit.SetGoMemLimitWithOpts(
		memlimit.WithRatio(0.9),
		memlimit.WithProvider(memlimit.FromCgroup),
		memlimit.WithLogger(slog.Default()),
	)
	memlimit.SetGoMemLimitWithOpts(
		memlimit.WithRatio(0.9),
		memlimit.WithProvider(
			memlimit.ApplyFallback(
				memlimit.FromCgroup,
				memlimit.FromSystem,
			),
		),
	)
	memlimit.SetGoMemLimit(0.9)
	memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9)
	memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroup, 0.9)
	memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupV1, 0.9)
	memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupHybrid, 0.9)
	memlimit.SetGoMemLimitWithProvider(memlimit.FromCgroupV2, 0.9)
}