memory

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memory coordinates process-wide cache trimming and pressure sweeps without replacing Go's runtime garbage collection policy or setting GOMEMLIMIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Governor

type Governor struct {
	// contains filtered or unexported fields
}

Governor tracks process-wide reclaimable caches and coordinates periodic idle sweeps alongside heap-pressure trimming.

func Default

func Default() *Governor

Default returns the process-wide memory Governor.

func NewGovernor

func NewGovernor() *Governor

NewGovernor constructs an isolated Governor. Tests use dedicated instances to exercise registration and pressure triggers without disturbing Default.

func (*Governor) CheckPressure

func (g *Governor) CheckPressure() bool

CheckPressure samples Go heap object and live-heap metrics and runs Trim(Pressure) when retained memory crosses its high-water mark.

func (*Governor) Register

func (g *Governor) Register(name string, trim func(Level)) func()

Register adds a trim callback to the Governor and returns an idempotent unregister function. Callbacks run without holding the Governor lock.

func (*Governor) Snapshot

func (g *Governor) Snapshot() Snapshot

Snapshot returns a copy of the Governor's current diagnostics.

func (*Governor) Start

func (g *Governor) Start() func()

Start reference-counts the background idle/pressure sweep goroutine and returns an idempotent stop function that waits for the worker when the last reference is released.

func (*Governor) Trim

func (g *Governor) Trim(level Level)

Trim invokes every registered callback at the requested reclamation level.

type Level

type Level int

Level describes how aggressively registered caches should reclaim memory.

const (
	// Idle runs on periodic background ticks to evict expired or inactive entries.
	Idle Level = iota
	// Pressure runs when Go heap metrics cross high-water thresholds.
	Pressure
)

type Snapshot

type Snapshot struct {
	Registered    int
	LastLevel     Level
	LastTrim      time.Time
	LastHeapBytes uint64
	LastLiveBytes uint64
}

Snapshot reports the latest state observed by a Governor.

Jump to

Keyboard shortcuts

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