func InitMem(OD string) { log.Println("memory profile enabled") AtExit(func() { fname := OD + "mem.pprof" f, err := os.Create(fname) defer f.Close() util.LogErr(err, "memory profile") // during cleanup, should not panic/exit log.Println("writing memory profile to", fname) util.LogErr(pprof.WriteHeapProfile(f), "memory profile") me := procSelfExe() outfile := fname + ".svg" saveCmdOutput(outfile, "go", "tool", "pprof", "-svg", "--inuse_objects", me, fname) }) }
// Exec command and write output to outfile. func saveCmdOutput(outfile string, cmd string, args ...string) { log.Println("exec:", cmd, args, ">", outfile) out, err := exec.Command(cmd, args...).Output() // TODO: stderr is ignored if err != nil { log.Printf("exec %v %v: %v: %v", cmd, args, err, string(out)) } // on error: write anyway, clobbers output file. e := ioutil.WriteFile(outfile, out, 0666) util.LogErr(e, "writing", outfile) }