You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please check this source code. The memory is not deallocated.
package main
import (
"fmt"
"runtime"
)
var version = "3456"
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func main() {
var version = "3456"
//viper.Set("version", version);
N := 2000000
PrintMemUsage()
for i := 1; i <= N; i++ {
m := make(map[string]interface{})
m["version"] = version
delete(m, "version")
m2 := make(map[string]string)
m2["version"] = string(version)
}
fmt.Println("N: ", N, "version: ", version)
PrintMemUsage()
}
What version of Go are you using (go version)?
1.14.1
$ go version
Does this issue reproduce with the latest release?
1.14.1
What operating system and processor architecture are you using (go env)?
Tested on Windows and Debian
go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\ADMIN\AppData\Local\go-build
set GOENV=C:\Users\ADMIN\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\ADMIN\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMIN\AppData\Local\Temp\go-build969542346=/tmp/go-build -gno-record-gcc-switche
randall77
changed the title
Memory leak when assign parameter for map[string]interface{}
runtime: memory leak when assign parameter for map[string]interface{}
Apr 2, 2020
Yes. Storing a string value in a variable of interface type requires an allocation.
By the usual definition of memory leak, this is not a leak. The garbage collector is aware of the allocation and will collect it.
If you want more discussion about the exact meaning of the fields set by runtime.ReadMemStats I recommend using a forum. See https://golang.org/wiki/Questions. Thanks.
Please check this source code. The memory is not deallocated.
What version of Go are you using (
go version
)?1.14.1
Does this issue reproduce with the latest release?
1.14.1
What operating system and processor architecture are you using (
go env
)?Tested on Windows and Debian
go env
OutputWhat did you do?
Run the source code on top
What did you expect to see?
Alloc = 0 MiB TotalAlloc = 0 MiB Sys = 5 MiB NumGC = 0
N: 2000000 version: 3456
Alloc = 0 MiB TotalAlloc = 0 MiB Sys = 5 MiB NumGC = 0
What did you see instead?
Alloc = 0 MiB TotalAlloc = 0 MiB Sys = 5 MiB NumGC = 0
N: 2000000 version: 3456
Alloc = 3 MiB TotalAlloc = 30 MiB Sys = 12 MiB NumGC = 7
The text was updated successfully, but these errors were encountered: