Documentation
¶
Overview ¶
Package memlimit sets Go's soft memory limit (GOMEMLIMIT) from the container's cgroup memory limit, so the garbage collector applies back-pressure before the process hits the cgroup ceiling and gets OOM-killed.
By default Go does not know its container's memory limit: the GC paces itself against heap growth, not against the cgroup cap. Under memory pressure that leads to OOM kills and restarts. Calling SetFromCgroup once at startup teaches the runtime the limit (defaulting to 90% of it, leaving headroom for non-heap allocations the GC cannot reclaim).
It is a safe no-op when there is no cgroup memory limit, on non-Linux platforms, or when the limit is too small to be sane.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DetectCgroupLimit ¶
DetectCgroupLimit returns the raw cgroup memory limit in bytes (before any ratio is applied) and whether a finite limit was found. It performs no side effects, so it is useful for inspecting or comparing what SetFromCgroup would act on. Returns (0, false) on non-Linux or when there is no limit.
func SetFromCgroup ¶
SetFromCgroup reads the current cgroup memory limit and, if found, sets GOMEMLIMIT to that limit multiplied by the configured ratio.
It returns the number of bytes GOMEMLIMIT was set to, or 0 if it was left unchanged (no cgroup limit, non-Linux, unlimited, or below the minimum). A non-nil error is returned only for invalid options.
Types ¶
type Option ¶
type Option func(*config)
Option customizes SetFromCgroup.
func WithLogger ¶
WithLogger enables logging of the outcome via the given slog.Logger. By default nothing is logged.
func WithMinBytes ¶
WithMinBytes sets the smallest computed limit worth applying. If the computed value is below this, GOMEMLIMIT is left unchanged. The default is 16 MiB.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command example demonstrates and verifies the end-to-end effect of memlimit: it applies GOMEMLIMIT from the cgroup, then reads the runtime's current value back via debug.SetMemoryLimit(-1) to confirm it was actually applied.
|
Command example demonstrates and verifies the end-to-end effect of memlimit: it applies GOMEMLIMIT from the cgroup, then reads the runtime's current value back via debug.SetMemoryLimit(-1) to confirm it was actually applied. |