add command line option for memory profiling

This commit is contained in:
Gokcehan 2018-02-17 19:22:40 +03:00
parent ae779ae25d
commit 4ce6980098

20
main.go
View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"runtime/pprof" "runtime/pprof"
) )
@ -59,7 +60,12 @@ func main() {
cpuprofile := flag.String( cpuprofile := flag.String(
"cpuprofile", "cpuprofile",
"", "",
"path to the file to write the cpu profile") "path to the file to write the CPU profile")
memprofile := flag.String(
"memprofile",
"",
"path to the file to write the memory profile")
flag.StringVar(&gLastDirPath, flag.StringVar(&gLastDirPath,
"last-dir-path", "last-dir-path",
@ -125,4 +131,16 @@ func main() {
run() run()
} }
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
} }