leaktest

package
v1.16.5 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: BSD-3-Clause Imports: 11 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 leaktest checks that a scenario does not leak memory: it runs a step many times, and compares the live heap and the goroutines after N steps and after 4N steps (SPEC §15, "Memory over time"). It is imported by tests only. It imports "testing", so no program may link it (step 29.16 keeps "testing" out of programs).

A scenario leaks when what survives two garbage collections grows from N to 4N steps by more than a small slack (the runtime's own caches settle slowly), or when the number of goroutines grows at all. It does not compare with an absolute bound, so it is not flaky on a machine with another heap.

Set LEAKTEST_SCALE to a number to multiply every N by it, for a longer soak than the default test run.

Set LEAKTEST_REPORT to a file name to append one tab-separated line for each scenario (name, N, then the live heap in bytes, the heap in use, the live objects and the goroutines at the base, after N and after 4N steps). Set LEAKTEST_PROFILE to a directory to write the heap profile of every scenario at the three points (<name>.base.pb.gz, <name>.n.pb.gz, <name>.4n.pb.gz); run the test binary with -memprofile and -memprofilerate=1 to make the profiles exact, and name the site of a leak with

go tool pprof -sample_index=inuse_space -base <name>.n.pb.gz <test binary> <name>.4n.pb.gz

Flutter: no counterpart

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opts

type Opts struct {
	// N is the number of steps of the first run; the second run adds 3N. The default is 200.
	N int
	// Warmup is the number of steps before the base is taken, so that the caches that fill once
	// are full. The default is 20.
	Warmup int
	// SlackBytes is the growth of the live heap that is not a leak; the default is 256 KB, and
	// 2% of the base is added.
	SlackBytes uint64
	// Counters are other numbers that must not grow, such as the resources of a GPU device or the
	// blocks of C memory that are not freed: each is read with the heap at the base, after N and
	// after 4N steps, and may grow by at most CounterSlack from N to 4N.
	Counters map[string]func() int
	// CounterSlack is the growth of a counter that is not a leak; the default is none.
	CounterSlack int
	// SlackObjects is the growth of the number of live objects that is not a leak; the default is
	// 300.
	SlackObjects uint64
}

Opts are the optional parameters of Run.

Flutter: no counterpart

type Result

type Result struct {
	Name                  string
	N                     int
	Base, AfterN, After4N Stats
	// Counters are the values of Opts.Counters at the base, after N and after 4N steps.
	Counters map[string][3]int
}

Result is the numbers of a scenario.

Flutter: no counterpart

func Run

func Run(tb testing.TB, name string, setup func() (step func()), opts ...Opts) Result

Run runs a scenario. setup prepares it (it may use tb for cleanups) and returns the step that runs once for each unit of N; the step must leave the app as it found it (a route pushed is popped). Run fails tb when the scenario leaks, and returns its numbers.

Flutter: no counterpart

type Stats

type Stats struct {
	// HeapAlloc is the bytes of live objects, HeapInuse the bytes of the spans that hold them.
	HeapAlloc, HeapInuse uint64
	// HeapObjects is the number of live objects.
	HeapObjects uint64
	// Goroutines is runtime.NumGoroutine.
	Goroutines int
}

Stats is what the runtime reports after two garbage collections.

Flutter: no counterpart

func Snapshot

func Snapshot() Stats

Snapshot collects garbage twice and reads the statistics. The goroutines that are finishing get a moment to be gone.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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