GO PPROF 高内存占用分析

编写源码

package main

import (
    "fmt"
    "net/http"
    _ "net/http/pprof"
    "runtime"
    "time"
)

func main() {
    runtime.GOMAXPROCS(1)
    runtime.SetMutexProfileFraction(1)
    runtime.SetBlockProfileRate(1)
    registerProf()
    UseHeap{}.UseHeap()
}

func registerProf() {
    port := ":8060"
    go func() {
        err := http.ListenAndServe(port, nil)
        if err != nil {
            panic("pprof start error" + err.Error())
        }
    }()
}

type UseHeap struct {
}

func (c UseHeap) UseHeap() {
    var M = 1024 * 1024
    var G = M * 1024
    buf := make([]byte, 0)
    var i = 0
    for len(buf) < G {
        bytes := make([]byte, M)
        buf = append(buf, bytes...)
        i++
    }
    fmt.Print(i)
    fmt.Println(len(buf))
    time.Sleep(time.Minute * 10)
}

执行分析命令

go tool pprof -http=:8000 http://127.0.0.1:8060/debug/pprof/heap

http://127.0.0.1:8060/debug/pprof/heap?debug=1

分析结果

heap

results matching ""

    No results matching ""