gctune

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package gctune centralizes the process-wide GC knobs that the rest of the runtime deliberately refuses to touch. It is the single place allowed to flip a global runtime setting (GOGC, the soft memory limit) so the dangerous mutation lives behind a small, audited API rather than scattered `runtime/debug` calls.

It complements compiler/debug, which only *reads* GC/heap stats and explicitly leaves the mutating knobs "at the caller's risk". gctune is that caller.

Two consumers use it:

  • cmd (host-side perf flags: -gc-percent, -mem-limit), applied once at startup via Apply.
  • the Lua `collectgarbage` builtin (vm/stdlib.go), which maps Lua's option strings onto Collect/Stop/Restart/SetPercent/HeapBytes.

All mutating entry points are guarded by a single mutex and share package state, matching the fact that Go has exactly one collector per process.

Index

Constants

View Source
const DefaultPercent = 100

DefaultPercent mirrors Go's default GOGC. Restart and the "remembered" percent fall back to it when GC has never been explicitly configured.

Variables

This section is empty.

Functions

func Apply

func Apply(o Options)

Apply installs the requested knobs. Intended to be called once, early, from the host (cmd) before any script runs.

func Collect

func Collect()

Collect forces a full GC cycle. Backs collectgarbage("collect").

func HeapBytes

func HeapBytes() uint64

HeapBytes returns the bytes of live heap objects currently allocated. Backs collectgarbage("count"). Note: ReadMemStats briefly stops the world, so this is a reporting call, not a hot-path metric.

func IsRunning

func IsRunning() bool

IsRunning reports whether the collector is currently enabled. Backs collectgarbage("isrunning").

func Restart

func Restart()

Restart re-enables the collector at the last enabled percent. Backs collectgarbage("restart").

func SetMemoryLimit

func SetMemoryLimit(bytes int64) int64

SetMemoryLimit sets the soft heap limit in bytes and returns the previous limit. Passing a negative value queries the current limit without changing it (the runtime/debug contract).

func SetPercent

func SetPercent(pct int) int

SetPercent sets GOGC and returns the previous value. A negative pct disables the GC (and is not remembered as the restart target). Backs collectgarbage("setpause", n).

func Stop

func Stop()

Stop disables the collector, remembering the current percent so Restart can restore it. Backs collectgarbage("stop").

Types

type Options

type Options struct {
	// Percent sets GOGC. Zero leaves it at the current value; a negative value
	// disables the GC entirely (equivalent to collectgarbage("stop")).
	Percent int
	// MemoryLimit sets the soft heap limit in bytes. Zero leaves the current
	// limit untouched.
	MemoryLimit int64
}

Options configures the process GC at startup. A zero field leaves that knob untouched, so callers can pass only what they want to override.

Jump to

Keyboard shortcuts

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