FAQ
The heap is freed, you can check this using runtime.ReadMemStats(),
but the processes virtual address space does not shrink -- ie, your
program will not return memory to the operating system. On Unix based
platforms we use a system call to tell the operating system that it
can reclaim unused parts of the heap, this facility is not available
on Windows platforms.
On Fri, Apr 12, 2013 at 12:01 PM, junyi sun wrote:
Hi all,

I am studying Go, and find that it is very interesting and "battery"
included.

In this mail, I want to ask something about the garbage collection rules of
Go.

For example, in the following program.

package main

import (
"math/rand"
"time"
"runtime"
)


func DoSomeThing(){
a := make([][]interface{},10000)
for i:=0;i<len(a);i++{
a[i] = make([]interface{},10000)
for j:=0;j<len(a[i]);j++{
a[i][j] = rand.Float64()
}
}
}


func main() {
DoSomeThing()
for {
println("idle")
runtime.GC()
time.Sleep(2*time.Second)
}
}


It will allocate 1.6 GB memory on my windows x64 machine with go1.1 beta2.

I am confused that why the memory allocated by DoSometThing() is not freed?



Thanks

Junyi


--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Apr 12, 2013 at 5:39 am
    No, calling runtime.GC() is unnecessary, and also does not do what you want.
    On Fri, Apr 12, 2013 at 3:37 PM, junyi sun wrote:
    So, do we always need to call runtime.GC() to free memory ? I found that if
    I comment the "runtime.GC()", the m.Alloc would be still 1.6G.

    Thanks



    On Fri, Apr 12, 2013 at 1:36 PM, junyi sun wrote:

    Thanks, it is true.

    In windows,

    ~~before calling runtime.GC()

    2013/04/12 13:35:12 Memory Acquired: 2018261160
    2013/04/12 13:35:12 Memory Used : 1638886696

    ~~after calling runtime.GC()
    2013/04/12 13:35:14 Memory Acquired: 2018261160
    2013/04/12 13:35:14 Memory Used : 270344


    package main

    import (
    "math/rand"
    "time"
    "runtime"
    "log"
    )


    func DoSomeThing(){
    a := make([][]interface{},10000)
    for i:=0;i<len(a);i++{
    a[i] = make([]interface{},10000)
    for j:=0;j<len(a[i]);j++{
    a[i][j] = rand.Float64()
    }
    }
    }


    func GoRuntimeStats() {
    m := &runtime.MemStats{}

    log.Println("# goroutines: ", runtime.NumGoroutine())
    runtime.ReadMemStats(m)
    log.Println("Memory Acquired: ", m.Sys)
    log.Println("Memory Used : ", m.Alloc)
    //runtime.GC()
    }

    func main() {
    DoSomeThing()
    for {
    println("idle")
    GoRuntimeStats()
    time.Sleep(2*time.Second)
    }
    }



    On Fri, Apr 12, 2013 at 12:12 PM, Dave Cheney wrote:

    The heap is freed, you can check this using runtime.ReadMemStats(),
    but the processes virtual address space does not shrink -- ie, your
    program will not return memory to the operating system. On Unix based
    platforms we use a system call to tell the operating system that it
    can reclaim unused parts of the heap, this facility is not available
    on Windows platforms.
    On Fri, Apr 12, 2013 at 12:01 PM, junyi sun wrote:
    Hi all,

    I am studying Go, and find that it is very interesting and "battery"
    included.

    In this mail, I want to ask something about the garbage collection
    rules of
    Go.

    For example, in the following program.

    package main

    import (
    "math/rand"
    "time"
    "runtime"
    )


    func DoSomeThing(){
    a := make([][]interface{},10000)
    for i:=0;i<len(a);i++{
    a[i] = make([]interface{},10000)
    for j:=0;j<len(a[i]);j++{
    a[i][j] = rand.Float64()
    }
    }
    }


    func main() {
    DoSomeThing()
    for {
    println("idle")
    runtime.GC()
    time.Sleep(2*time.Second)
    }
    }


    It will allocate 1.6 GB memory on my windows x64 machine with go1.1
    beta2.

    I am confused that why the memory allocated by DoSometThing() is not
    freed?



    Thanks

    Junyi


    --
    You received this message because you are subscribed to the Google
    Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Cobo at Apr 12, 2013 at 12:43 pm
    Have you tried using the new http://tip.golang.org/pkg/runtime/debug/#FreeOSMemory?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Apr 12, 2013 at 2:13 pm

    On Fri, Apr 12, 2013 at 8:43 PM, Carlos Cobo wrote:
    Have you tried using the new http://tip.golang.org/pkg/runtime/debug/#FreeOSMemory?
    The returning memory to OS functionality is not implemented on Windows
    and Plan 9
    (for Go 1.1 at least.)

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Cobo at Apr 12, 2013 at 5:27 pm
    Sad to hear that. Would it solve OPs problem if it was implemented?

    El viernes, 12 de abril de 2013 16:13:02 UTC+2, minux escribió:
    On Fri, Apr 12, 2013 at 8:43 PM, Carlos Cobo wrote:
    Have you tried using the new
    http://tip.golang.org/pkg/runtime/debug/#FreeOSMemory?
    The returning memory to OS functionality is not implemented on Windows
    and Plan 9
    (for Go 1.1 at least.)
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Apr 12, 2013 at 6:22 pm

    On Sat, Apr 13, 2013 at 1:27 AM, Carlos Cobo wrote:
    Sad to hear that. Would it solve OPs problem if it was implemented? yes.
    El viernes, 12 de abril de 2013 16:13:02 UTC+2, minux escribió:
    On Fri, Apr 12, 2013 at 8:43 PM, Carlos Cobo wrote:
    Have you tried using the new
    http://tip.golang.org/pkg/runtime/debug/#FreeOSMemory?
    The returning memory to OS functionality is not implemented on Windows
    and Plan 9
    (for Go 1.1 at least.)
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 12, '13 at 4:12a
activeApr 12, '13 at 6:22p
posts6
users3
websitegolang.org

3 users in discussion

Carlos Cobo: 2 posts Minux: 2 posts Dave Cheney: 2 posts

People

Translate

site design / logo © 2022 Grokbase