Skip to content

runtime/debug: FreeOSMemory() not working on Android 9 #37569

Closed
@YouROK

Description

@YouROK

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

Activity

changed the title [-]debug.FreeOSMemory() not working on android 9[/-] [+]runtime/debug: FreeOSMemory() not working on Android 9[/+] on Mar 1, 2020
odeke-em

odeke-em commented on Mar 1, 2020

@odeke-em
Member

Thank you for the report @YouROK and welcome to the Go project!

/cc @mknyszek @aclements @eliasnaur @cherrymui

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Mar 1, 2020
mknyszek

mknyszek commented on Jul 1, 2020

@mknyszek
Contributor

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

elgatito commented on Aug 18, 2020

@elgatito

@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

networkimprov commented on Aug 18, 2020

@networkimprov

Filed #40870

aclements

aclements commented on Sep 4, 2020

@aclements
Member

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?

added
WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
on Nov 1, 2020
aclements

aclements commented on Nov 1, 2020

@aclements
Member

Ping @YouROK . Could you try the GODEBUG=madvdontneed=1 environment variable? And can you post the code that demonstrates "allocating memory several times"?

gopherbot

gopherbot commented on Nov 2, 2020

@gopherbot
Contributor

Change https://golang.org/cl/267100 mentions this issue: runtime: default to MADV_DONTNEED on Linux

gopherbot

gopherbot commented on Dec 1, 2020

@gopherbot
Contributor

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.)

removed
WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
on Dec 2, 2020
odeke-em

odeke-em commented on Dec 2, 2020

@odeke-em
Member

Re-opening this issue.

aclements

aclements commented on Dec 2, 2020

@aclements
Member

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

YouROK commented on Mar 3, 2021

@YouROK
Author

@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

networkimprov commented on Mar 3, 2021

@networkimprov

@gopherbot add OS-Android

EDIT: @odeke-em could you tag with os-android? Gopherbot can't label closed issues...

aclements

aclements commented on Jul 20, 2021

@aclements
Member

@YouROK, Go 1.16 defaults to GODEBUG=madvdontneed=1, so setting that environment in Go 1.16 should not have any effect.

locked and limited conversation to collaborators on Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Android

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @networkimprov@elgatito@mknyszek@aclements@YouROK

        Issue actions

          runtime/debug: FreeOSMemory() not working on Android 9 · Issue #37569 · golang/go