golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.02k stars 17.54k forks source link

x/mobile: potential gomobile memory leak #59511

Open NightBlaze opened 1 year ago

NightBlaze commented 1 year ago

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

$ go version go1.20.3 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="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/nightblaze/Library/Caches/go-build"
GOENV="/Users/nightblaze/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/nightblaze/go/pkg/mod"
GONOPROXY="bitbucket.org//*"
GONOSUMDB="bitbucket.org//*"
GOOS="darwin"
GOPATH="/Users/nightblaze/go"
GOPRIVATE="bitbucket.org//*"
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.20.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/37/jv5g6pm104l9ty_8gtnrd54h0000gn/T/go-build4126383988=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I use gomobile bind to build shared logic for iOS and Android and it seems that sometimes gomobile doesn't break retain cycles that leads to memory leaks. Probably I use wrong architecture but in any case the behaviour is strange: in one case all created objects was dealloced and in another there was a memory leak. To reproduce you can check demo project https://github.com/NightBlaze/GomobileDemo

What did you expect to see?

Memory doesn't leak

What did you see instead?

Memory leak

dr2chase commented 1 year ago

Does the amount of leaked memory become large enough to matter, or did you just notice this as an anomaly and the leak is a manageable size? The Go garbage collector doesn't have problems with cycles, but it can be thwarted by stray pointers, especially in the youngest call frame (which should not be a problem here because your example calls debug.FreeOSMemory).

NightBlaze commented 1 year ago

The amount of leaked memory can vary. In the example project, it is very small, but if we need to hold some images or other big data, then the amount of leaked memory can become unmanageable.

prattmic commented 1 year ago

cc @hyangah

NightBlaze commented 1 year ago

If I add

go func() {
    ticker := time.NewTicker(1 * time.Minute)
    for range ticker.C {
        debug.FreeOSMemory()
    }
}()

then "leaked" objects will become dealloced on the ticker tick.

I think it's ok to close the issue.