Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use map to insert and delete data can case memory leak #23156

Closed
FengtuWang opened this issue Dec 16, 2017 · 1 comment
Closed

Use map to insert and delete data can case memory leak #23156

FengtuWang opened this issue Dec 16, 2017 · 1 comment

Comments

@FengtuWang
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Yes, latest release download from https://golang.org/dl/

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build363981589=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Use map to insert and delete data, code like this:

package main

import (
	"fmt"
	"runtime"
	"runtime/debug"
)

func main() {
	a := make(map[int]int)

	stats := new(runtime.MemStats)
	runtime.ReadMemStats(stats)
	fmt.Printf("Before: %5.2fk\n", float64(stats.Alloc)/1024)
	previous := stats.Alloc

	for i := 1000000; i > 0; i-- {
		a[i] = 0
	}

	for k := range a {
		delete(a, k)
	}

	debug.FreeOSMemory()
	fmt.Printf("len(a)=%v\n", len(a))

	runtime.ReadMemStats(stats)
	fmt.Printf("After: %5.2fk    Added: %5.2fk\n", float64(stats.Alloc)/1024, float64(stats.Alloc-previous)/1024)

	return
}

What did you expect to see?

Memory not increased obviously

What did you see instead?

Memory increased obviously, output like this:

Before: 42.54k
len(a)=0
After: 39252.77k    Added: 39210.23k

I do not know why so many memory still exist after i delete all elements in map.

@mvdan
Copy link
Member

mvdan commented Dec 16, 2017

Dup of #20135.

@mvdan mvdan closed this as completed Dec 16, 2017
@golang golang locked and limited conversation to collaborators Dec 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants