status-go/profiling/heap.go

35 lines
592 B
Go
Raw Permalink Normal View History

package profiling
import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
)
// MemFilename is a filename in which the memory profiling is stored.
const MemFilename = "status_mem.prof"
var memFile *os.File
// WriteHeapFile writes heap memory to the file.
func WriteHeapFile(dataDir string) error {
var err error
if memFile == nil {
memFile, err = os.Create(filepath.Join(dataDir, MemFilename))
if err != nil {
return err
}
defer func() {
memFile.Close() //nolint: errcheck
memFile = nil
}()
}
runtime.GC()
err = pprof.WriteHeapProfile(memFile)
return err
}