Add a global option to print memory usage

This option, -print-memory-usage, will print memory usage every second while
the program is running.
This commit is contained in:
Gilbert Chen
2021-10-15 20:45:53 -04:00
parent cacf6618d2
commit 68b60499d7
2 changed files with 22 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"time"
"runtime"
"github.com/gilbertchen/gopass"
"golang.org/x/crypto/pbkdf2"
@@ -460,3 +461,16 @@ func AtoSize(sizeString string) int {
return size
}
func PrintMemoryUsage() {
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
LOG_INFO("MEMORY_STATS", "Currently allocated: %s, total allocated: %s, system memory: %s, number of GCs: %d",
PrettySize(int64(m.Alloc)), PrettySize(int64(m.TotalAlloc)), PrettySize(int64(m.Sys)), m.NumGC)
time.Sleep(time.Second)
}
}