golang time.After资源泄露描述

转自:https://groups.google.com/forum/#!topic/golang-nuts/cCdm0Ixwi9A
资源泄露是指,未到指定的时间,内存资源不释放

package main

import (
    "time"
    "fmt"
    "runtime"
)

func main() {

    var ms runtime.MemStats
    runtime.ReadMemStats(&ms)
    fmt.Println("before, have", runtime.NumGoroutine(), "goroutines,", ms.Alloc, "bytes allocated")
    for i := 0; i < 1000000; i++ {
        time.After(time.Hour)
    }
    runtime.GC()
    runtime.ReadMemStats(&ms)
    fmt.Println("after, have", runtime.NumGoroutine(), "goroutines,", ms.Alloc, "bytes allocated")

    time.Sleep(time.Minute) // uncomment this if you want to check memory usage externally, like with top
}

猜你喜欢

转载自blog.csdn.net/x_i_y_u_e/article/details/81167766