http://blog.csdn.net/erlib 作者Sunface


这篇文章会陆续总结一些内存、Gc度量的方法

首先是通过

stats := &runtime.MemStats{}
runtime.ReadMemStats(stats)
这个来获取一个stat,里面包含了内存和垃圾回收的信息:GC时间分布slice,GC总时间,GC次数等等
type Garbage struct{ a int }

func notify(f *Garbage) {
    stats := &runtime.MemStats{}
    runtime.ReadMemStats(stats)

    fmt.Println("Last GC was:", stats.LastGC)

    go ProduceFinalizedGarbage()
}

func ProduceFinalizedGarbage() {
    x := &Garbage{}
    runtime.SetFinalizer(x, notify)
}

func main() {
    go ProduceFinalizedGarbage()

    for {
        runtime.GC()
        time.Sleep(30 * time.Second) // Give GC time to run
    }
}


(未完待续)