Closed
Description
What version of Go are you using (go version
)?
GO 1.12 - 1.14
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env
)?
On android 9, on other all works
What did you do?
func main() {
bigArr := make([]byte, 250*1024*1024)
for i := 0; i < len(bigArr); i++ {
bigArr[i] = byte(i)
}
bigArr = nil
runtime.GC()
debug.FreeOSMemory()
for true {
//Some thing works in program
time.Sleep(time.Second)
}
}
What did you expect to see?
I expected to see the memory in the system, but it remains in the program. If you allocate memory several times, then the program is killed by the system, but should clear it
In version 1.11.X all works in android 9, memory returning to os, but not execute on android 10. On android 5,6,7,8 all works of new golang version
Compile cmd: GOARM=7 GOOS=linux GOARCH=arm go build -o test main
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]debug.FreeOSMemory() not working on android 9[/-][+]runtime/debug: FreeOSMemory() not working on Android 9[/+]odeke-em commentedon Mar 1, 2020
Thank you for the report @YouROK and welcome to the Go project!
/cc @mknyszek @aclements @eliasnaur @cherrymui
mknyszek commentedon Jul 1, 2020
Considering the combination of Android version and Go version here (Android 10+, Go 1.12+), I suspect this is another case of
MADV_FREE
not updating the kernel's per-process RSS numbers. Android 10's minimum Linux kernel version is 4.9 whereas Android 9's minimum is 4.4.MADV_FREE
was introduced in Linux 4.5.Sorry for the very late reply on this. Could you try running your code with
GODEBUG=madvdontneed=1
? I'm not sure how difficult that is to do on Android.elgatito commentedon Aug 18, 2020
@mknyszek It is great we can use GODEBUG flags from environment variables. But it would be alto great to be able to hardcode that into the code, or define, depending on the build parameters, which is needed, if you cannot control how the binary is ran by user.
networkimprov commentedon Aug 18, 2020
Filed #40870
aclements commentedon Sep 4, 2020
It's really disappointing if Android kills applications for memory use without first trying to clean up their freed pages... If that is the case, then I would consider
MADV_FREE
quite broken on Android and we shouldn't use it.@YouROK, your example code is likely working as expected, but you mention "If you allocate memory several times, then the program is killed by the system". That sounds like the real issue here. Could you post code that reproduces that?
aclements commentedon Nov 1, 2020
Ping @YouROK . Could you try the
GODEBUG=madvdontneed=1
environment variable? And can you post the code that demonstrates "allocating memory several times"?gopherbot commentedon Nov 2, 2020
Change https://golang.org/cl/267100 mentions this issue:
runtime: default to MADV_DONTNEED on Linux
runtime: default to MADV_DONTNEED on Linux
gopherbot commentedon Dec 1, 2020
Timed out in state WaitingForInfo. Closing.
(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)
odeke-em commentedon Dec 2, 2020
Re-opening this issue.
aclements commentedon Dec 2, 2020
I think gopherbot actually did the right thing here. We haven't heard back from @YouROK, so there's not anything more we can do here, and whatever the details of this issue are, it's probably fixed by CL 267100. So I'm going to re-close this issue, but feel free to reopen it with more information, @YouROK .
YouROK commentedon Mar 3, 2021
@aclements with flag GODEBUG=madvdontneed=1 all work good.
I have been looking for the problem on different devices for several months.
I can say for sure that the problem exists on almost all Linux devices, but the memory grows only up to a certain point, which is not critical on a regular PC with Linux. But on android, Linux and Android builds behave the same way, and when a certain point is reached, the Android OS closes the application.
I can suggest one solution to enable the build flag with GODEBUG=madvdontneed=1 to not add it at startup in env
Last test be in go 1.16
networkimprov commentedon Mar 3, 2021
@gopherbot add OS-Android
EDIT: @odeke-em could you tag with os-android? Gopherbot can't label closed issues...
aclements commentedon Jul 20, 2021
@YouROK, Go 1.16 defaults to GODEBUG=madvdontneed=1, so setting that environment in Go 1.16 should not have any effect.