I'm running a server written in Go and the RSS is very high and the memory is not even getting freed (back to OS).
I used pprof to check but it seems that there is no memory leak.
I also tried:
GODEBUG=madvdontneed=1 ./memorytest
Please tell me how to use madvdontneed.
OS: CentOS 7 (Linux)
Arch: amd64
Go version: 1.14.2
Code:
package main
import (
"fmt"
"os"
"runtime"
"time"
)
var garr []string
var chn chan int
func main() {
chn = make(chan int, 1)
go Alloc()
<-chn
}
func Alloc() {
for {
arr := make([]string, 100000000)
//copy(garr,arr)
garr = arr
print_heap_info()
time.Sleep(5 * time.Second)
}
}
func print_heap_info() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("env:%v, heapsys:%d,heapalloc:%d,heapidel:%d,heapreleased:%d,heapinuse:%d\n",
os.Getenv("GODEBUG"), m.HeapSys, m.HeapAlloc, m.HeapIdle, m.HeapReleased, m.HeapInuse)
}
Output:
env:madvdontneed=1, heapsys:1677262848,heapalloc:1600074304,heapidel:76955648,heapreleased:76914688,heapinuse:1600307200
env:madvdontneed=1, heapsys:3287875584,heapalloc:3200081392,heapidel:87556096,heapreleased:87506944,heapinuse:3200319488
env:madvdontneed=1, heapsys:4898619392,heapalloc:4800086512,heapidel:98295808,heapreleased:98115584,heapinuse:4800323584
env:madvdontneed=1, heapsys:6509101056,heapalloc:6400090640,heapidel:108773376,heapreleased:108724224,heapinuse:6400327680
env:madvdontneed=1, heapsys:6509232128,heapalloc:4800086176,heapidel:1708908544,heapreleased:108724224,heapinuse:4800323584
env:madvdontneed=1, heapsys:6509002752,heapalloc:6400090304,heapidel:108675072,heapreleased:108560384,heapinuse:6400327680
env:madvdontneed=1, heapsys:6509199360,heapalloc:4800086712,heapidel:1708875776,heapreleased:108560384,heapinuse:4800323584
env:madvdontneed=1, heapsys:6509068288,heapalloc:6400090744,heapidel:108740608,heapreleased:108560384,heapinuse:6400327680
env:madvdontneed=1, heapsys:6509199360,heapalloc:4800086712,heapidel:1708875776,heapreleased:108560384,heapinuse:4800323584
env:madvdontneed=1, heapsys:6509068288,heapalloc:6400090840,heapidel:108740608,heapreleased:108462080,heapinuse:6400327680
Optimize your memory allocations to lower the peak.
See also: debug.FreeOSMemory()
Your formatted output is:
Which is (for the last line):
sys: bytes of heap memory obtained from the OS: 6207 MB
alloc: bytes of allocated heap objects: 6103 MB
idel: bytes in idle (unused) spans: 103 MB
released: bytes of physical memory returned to the OS: 103 MB
inuse: bytes in in-use spans: 6103 MB
It is not a memory leak. Output for a system with total 8GB RAM (system memory monitor):
command:
Output:
Code:
htop:
Output for
vmstat 1 -S MB
command, running two terminals:Output for first
GODEBUG=madvdontneed=1 go run .
command (auto killed):Output for second
GODEBUG=madvdontneed=1 go run .
command:Code:
Command:
Output:
The format of this line is subject to change. Currently, it is:
Note:
go version go1.15.5 linux/amd64
See also:
Go 1.13 RSS keeps on increasing, suspected scavenging issue
https://github.com/golang/go/issues/36398
https://github.com/golang/go/issues/39295
https://go.googlesource.com/proposal/+/master/design/14951-soft-heap-limit.md
https://docs.google.com/document/d/1wmjrocXIWTr1JxU-3EQBI6BK6KgtiFArkG47XK73xIQ/edit#
https://blog.cloudflare.com/go-dont-collect-my-garbage/