Skip to content
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: FreeOSMemory does not free memory immediately #8019

Closed
dvyukov opened this issue May 18, 2014 · 3 comments
Closed

runtime: FreeOSMemory does not free memory immediately #8019

dvyukov opened this issue May 18, 2014 · 3 comments
Milestone

Comments

@dvyukov
Copy link
Member

dvyukov commented May 18, 2014

Reported on golang-nuts:

FreeOSMemory does a GC and then frees all unused spans. But with concurrent GC, spans
are not updated during GC. So if you do:

1. use lots of memory
2. FreeOSMemory
3. sleep

The memory from 1 won't be released.

---------- Forwarded message ----------
Subject: [go-nuts] Why memory is not garbaged collected here?

Hello everyone,

I've been playing a bit with Go to check the garbage collector, and I found an
unexpected behaviour (unexpected for me). I don't know if it is a bug or something that
I don't really understand.

Everything has been tested on tip version.

Program is here: http://play.golang.org/p/QbfGh5xnhF

package main

import (
    "time"
    //    "runtime/debug"
)

func main() {
    f()
    //    debug.FreeOSMemory()
    //    debug.FreeOSMemory()
    time.Sleep(10 * time.Hour)
}

func f() {
    a := make([]int, 100000000)
    for i := range a {
        a[i] = i
    }
}

The program allocates 763 MB of RAM (on 64-bits architecture) and then sleeps for 10
hours. I've been monitoring the memory used and the GC work by setting GODEBUG=gotrace=1

With the FreeOSMemory() lines commented, the memory is never GC'd nor released back to
the system. I've waited about 30 minutes.

With one single line of FreeOSMemory(), the memory is not freed immediately (as I
expected) but it seems that in the 2-3 next automatic GC runs it is finally found and
freed, and eventually, released back to the OS.

With two FreeOSMemory() lines, the memory is released immediately (I suppose that in the
second call).
@gopherbot
Copy link

Comment 1:

CL https://golang.org/cl/97550043 mentions this issue.

@randall77
Copy link
Contributor

Comment 2:

So is is just that FreeOSMemory needs to sweep every span after it does a gc?

@dvyukov
Copy link
Member Author

dvyukov commented May 19, 2014

Comment 3:

This issue was closed by revision e893acf.

Status changed to Fixed.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants