转自: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
}
本文通过一个Go语言示例程序展示了如何使用runtime包监测内存使用情况,并通过goroutine数量及分配的字节数来检查潜在的资源泄露问题。
294

被折叠的 条评论
为什么被折叠?



