-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
runtime: Memory not returned to system on linux #37585
Comments
Does using What method are you using to measure RSS? A common confusion is that while Go releases memory back to the OS, if the OS has no other demand for that memory it won't actually take the pages back. Only if there are other demands for that memory will the Go process's RSS go down. Please don't post screenshots of code or logs. It makes it hard for us to reproduce your results. Just copy-paste the text directly. |
update Golang to 1.14 doesn't help the code I'm using for test
Compiled by Golang 1.11.13
|
@TimmyOVO Are you on a relatively recent Linux? If so, what does this show for the PID of your process:
From /proc man page:
From Go 1.12 release notes:
(On mobile, so brief). |
I updated @TimmyOVO's code from above (#37585 (comment)) to periodically output RSS memory values as reported by Full update is on playground, but the primary addition is this quick & dirty function: reportMem := func(header string) {
log.Println("---", header, "---")
pid := os.Getpid()
shell("ps rss: ", `ps -o rss= -p %d`, pid)
shell("top res: ", `top -b -n 1 | awk '/^[ ]*%d[ ]/ {print $6}'`, pid)
shell("LazyFree:", `cat /proc/%d/smaps | awk '/LazyFree/ {sum+= $2} END {print sum}'`, pid)
} I think the results are as expected for Go 1.11, 1.13, and 1.14, at least as far as I understand things in this general area:
Go 1.11
Go 1.13
Go 1.14
This is on Ubuntu:
|
If you run your binary with |
Change https://golang.org/cl/267100 mentions this issue: |
In Go 1.12, we changed the runtime to use MADV_FREE when available on Linux (falling back to MADV_DONTNEED) in CL 135395 to address issue #23687. While MADV_FREE is somewhat faster than MADV_DONTNEED, it doesn't affect many of the statistics that MADV_DONTNEED does until the memory is actually reclaimed under OS memory pressure. This generally leads to poor user experience, like confusing stats in top and other monitoring tools; and bad integration with management systems that respond to memory usage. We've seen numerous issues about this user experience, including #41818, #39295, #37585, #33376, and #30904, many questions on Go mailing lists, and requests for mechanisms to change this behavior at run-time, such as #40870. There are also issues that may be a result of this, but root-causing it can be difficult, such as #41444 and #39174. And there's some evidence it may even be incompatible with Android's process management in #37569. This CL changes the default to prefer MADV_DONTNEED over MADV_FREE, to favor user-friendliness and minimal surprise over performance. I think it's become clear that Linux's implementation of MADV_FREE ultimately doesn't meet our needs. We've also made many improvements to the scavenger since Go 1.12. In particular, it is now far more prompt and it is self-paced, so it will simply trickle memory back to the system a little more slowly with this change. This can still be overridden by setting GODEBUG=madvdontneed=0. Fixes #42330 (meta-issue). Fixes #41818, #39295, #37585, #33376, #30904 (many of which were already closed as "working as intended"). Change-Id: Ib6aa7f2dc8419b32516cc5a5fc402faf576c92e4 Reviewed-on: https://go-review.googlesource.com/c/go/+/267100 Trust: Austin Clements <austin@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Because 05e6d28 is landed on the master, this issue can be closed. |
Thank you @changkun for the notice, and @aclements for the fix, also happy New Year everyone! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
memory should be returned to system
What did you see instead?
On macos the memory be returned to system after about 3 minutes, but after waiting for 20 minutes, the memory still being use by the application on linux
The text was updated successfully, but these errors were encountered: