FAQ
I use the below code to test goroutine memory usage


package main



import (


     "fmt"


     "runtime"


     "time"


)



var ch chan int



const numRoutine = 500000



func init() {


     runtime.GOMAXPROCS(runtime.NumCPU())


     ch = make(chan int, 10)


}



func gene1() int {


     <-ch


     return 1 + 1


}



func closer() {


     var ticker *time.Ticker


     ticker = time.NewTicker(time.Second * 10)


     <-ticker.C


     fmt.Println("Start Close GoRoutine!!!!!!!!!!!!!!")


     for i := 0; i < numRoutine; i++ {


         ch <- i


     }


     fmt.Println(runtime.NumGoroutine())


     fmt.Println("End Close GoRoutine!!!!!!!!!!!!!!")


     runtime.GC()


}



func main() {


     go func() {


         var index = 0


         fmt.Println("Start Create GoRoutine!!!!!!!!!!!!!!")


         for index < numRoutine {


             go gene1() // 开一个goroutine


             index++


         }


         fmt.Println(runtime.NumGoroutine())


         fmt.Println("End Create GoRoutine!!!!!!!!!!!!!!")


         go closer() // this will GC after 10 minutes


     }()


     fmt.Scanf("press enter to exit%s")


}



I run this code on my computer,but after a long time (all goroutines have been run to end), but in my task manager, this process still take 2GB memory. but why? All go routines have been down and I also call runtime.GC in colser(). Who can tell me how to free the garbage memory alloced by golang?


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

  • Dmitry Vyukov at Dec 20, 2013 at 11:21 am
    There is currently no way to free memory occupied by goroutine descriptors
    and stack segments (your case). For normal heap memory you need to call
    runtime/debug.FreeOSMemory(), that will free unused heap memory back to OS.


    On Fri, Dec 20, 2013 at 3:16 PM, 晓强李 wrote:

    I use the below code to test goroutine memory usage


    package main



    import (


    "fmt"


    "runtime"


    "time"


    )



    var ch chan int



    const numRoutine = 500000



    func init() {


    runtime.GOMAXPROCS(runtime.NumCPU())


    ch = make(chan int, 10)


    }



    func gene1() int {


    <-ch


    return 1 + 1


    }



    func closer() {


    var ticker *time.Ticker


    ticker = time.NewTicker(time.Second * 10)


    <-ticker.C


    fmt.Println("Start Close GoRoutine!!!!!!!!!!!!!!")


    for i := 0; i < numRoutine; i++ {


    ch <- i


    }


    fmt.Println(runtime.NumGoroutine())


    fmt.Println("End Close GoRoutine!!!!!!!!!!!!!!")


    runtime.GC()


    }



    func main() {


    go func() {


    var index = 0


    fmt.Println("Start Create GoRoutine!!!!!!!!!!!!!!")


    for index < numRoutine {


    go gene1() // 开一个goroutine


    index++


    }


    fmt.Println(runtime.NumGoroutine())


    fmt.Println("End Create GoRoutine!!!!!!!!!!!!!!")


    go closer() // this will GC after 10 minutes


    }()


    fmt.Scanf("press enter to exit%s")


    }



    I run this code on my computer,but after a long time (all goroutines have been run to end), but in my task manager, this process still take 2GB memory. but why? All go routines have been down and I also call runtime.GC in colser(). Who can tell me how to free the garbage memory alloced by golang?


    --
    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.
  • 晓强李 at Dec 21, 2013 at 4:11 am
    Thanks,And I want to say "What a fuck thing!!!"

    --
    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
postedDec 20, '13 at 11:16a
activeDec 21, '13 at 4:11a
posts3
users2
websitegolang.org

2 users in discussion

晓强李: 2 posts Dmitry Vyukov: 1 post

People

Translate

site design / logo © 2022 Grokbase