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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.