Skip to content

cmd/compile: a slice captured by a closure might leak #48254

@hajimehoshi

Description

@hajimehoshi

What version of Go are you using (go version)?

$ go version
go version go1.17 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hajimehoshi/Library/Caches/go-build"
GOENV="/Users/hajimehoshi/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/hajimehoshi/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/hajimehoshi/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/hajimehoshi/test/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/60/khbk2xqn1c5bml1byjn89dwc0000gn/T/go-build3631899530=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run this and see the memory usage (I saw "Real Private Memory" on Activity on macOS):

package main

import (
        "time"
        "runtime"
)

var f func()

func foo(bs []byte) {
        if f == nil {
                copied := make([]byte, len(bs))
                copy(copied, bs)
                f = func() {
                        foo(copied)
                }
                return
        }

        println(len(bs))
}

func main() {
        bs := make([]byte, 100*1024*1024)
        foo(bs)

        f()
        f = nil
        bs = nil

        for {
                runtime.GC()
                time.Sleep(time.Second)
        }
}

What did you expect to see?

The private memory usage should be the same as this program (less than 10[MB] maybe):

func main() {
        for {
                runtime.GC()
                time.Sleep(time.Second)
        }
}

What did you see instead?

The private memory usage keeps about 200[MB]

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeOS-DarwinWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions