Skip to content

weak: Pointer may get tiny-allocated and grouped with a cleanup argument or closure, causing a leak #76007

@rogpeppe

Description

@rogpeppe

Go version

go version go1.26-devel_29d43df8ab Tue Oct 21 14:49:13 2025 -0700 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/rogpeppe/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/rogpeppe/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3305958583=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/rogpeppe/src/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/rogpeppe/src/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/rogpeppe/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/rogpeppe/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/rogpeppe/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.26-devel_29d43df8ab Tue Oct 21 14:49:13 2025 -0700'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I ran this program for a while and then stopped it: https://go.dev/play/p/3mmIoiV7wbJ before it could use all my memory.

What did you see happen?

The resulting CSV file is here: https://gist.github.com/rogpeppe/e5b6a0723f65cbc80bfd4861b593e17e

Here's a visualization:
Image

What did you expect to see?

I'd expect to see the GC kicking in and the number of calls to the cleanup function keeping up with the GC, or at least somewhat.

As it is, the memory usage appears to increase unboundedly and the cleanup calls never seem to happen.

Memory use seems to grow indefinitely without AddCleanup too (with only weak.Make) and with just the AddCleanup but without the weak.Make.

As is, this seems to imply that both weak pointers and AddCleanup aren't really fit for purpose.

Note that it makes no difference if we call runtime.GC in the loop.

Metadata

Metadata

Assignees

Labels

compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions