我在一个小程序中有内存泄漏。为了找到泄漏,我想使用pprof
.
我是这样设置的:
func main() {
f, _ := os.Create("my_pprof.pprof")
pprof.StartCPUProfile(f)
go func() {
main2()
}()
time.Sleep(1*time.Minute)
pprof.StopCPUProfile()
f.Close()
panic("QUIT")
}
当我想生成图表时,出现错误:
$ go tool pprof --pdf my_prog my_pprof.pprof > callgraph.pdf
No nodes to print
为什么无法获取节点?
更新: 该程序将重现该问题。
package main
import (
"fmt"
"os/exec"
"time"
"os"
"log"
"runtime/pprof"
)
func pollExec() {
ticker := time.NewTicker(5 * time.Millisecond)
quit := make(chan struct{})
for {
select {
case <-ticker.C:
const command = "cat"
const arg= "my_project/src/probe/test_pprof.go"
log.Printf("Execute command: " + command + " " + arg)
cmd := exec.Command(command, arg)
out, err := cmd.Output()
if err != nil {
panic("Can't execute command: " + command)
}
fmt.Printf("Output: %s\n", out)
case <-quit:
ticker.Stop()
return
}
}
}
func main() {
const pprofFileName = "my_project/src/probe/test.pprof"
f, err := os.Create(pprofFileName)
if err != nil {
panic("Can't open file: " + pprofFileName)
}
pprof.StartCPUProfile(f)
go func() {
pollExec()
}()
time.Sleep(1*time.Minute)
pprof.StopCPUProfile()
f.Close()
panic("QUIT")
}
我运行它:
$ uname -a
Linux ubuntu 3.13.0-34-generic #60-Ubuntu SMP Wed Aug 13 15:45:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
全局上下文是我有一个非常小的程序基于 Unix 命令结果推送信息。程序大小约为 3MB,我只有 40MB 内存来 24/7 运行它。当我让示例运行时,大小会越来越大。我不知道泄漏在哪里,我很不舒服,go
因为这是我用这种语言编写的第一个程序。对于本文中提供的示例,内存消耗随时间增加:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16723 paralle+ 20 0 6408 1700 816 S 2.0 0.2 0:00.52 test_pprof
After few minutes…
16723 paralle+ 20 0 6408 1960 832 S 1.7 0.2 0:04.76 test_pprof