from: https://golang.org/pkg/runtime/#MemStats
Alloc uint64 // bytes allocated and not yet freed
Sys uint64 // bytes obtained from system
In my long running go program I'm printing out:
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
fmt.Println("%d, %d", mem.Alloc/1048576, mem.Sys/1048576)
And I see values like:
200, 800
350, 910
198, 910
i.e. the first number mem.Alloc goes up and down. The 2nd number mem.Sys only goes up never down.
When I look at top -p pid for the process I see the Resident memory is always in between the two numbers. Another way to say that, top always says the memory is a little more than what mem.Alloc reports but way less than mem.Sys. Sometimes the Sys number gets rather large:
800, 4590
and my program is in a bad state. 4.5 gigs of used memroy? But top and Alloc say it's only using about 1 gig. The bad state is I can't run:
cmd := exec.Command(path, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = []*os.File{file}
err := cmd.Start()
because I get the error "fork/exec /path/to/exec: cannot allocate memory"
How can I get that mem.Sys number to go down and be able to run cmd := exec.Command, and then cmd.Start()?
Update: see How can I get linux to reduce its VIRT memory allocation for a golang process?