From 9545482a4495cc19d7e15e295ab987763aa82194 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 27 Sep 2025 11:24:10 +0800 Subject: [PATCH] refactor(pprof): remove memory profiling settings and enhance GC logging --- cmd/pprof_prof.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/pprof_prof.go b/cmd/pprof_prof.go index ae03cfea..3e581f8b 100644 --- a/cmd/pprof_prof.go +++ b/cmd/pprof_prof.go @@ -13,13 +13,7 @@ import ( "github.com/yusing/godoxy/internal/utils/strutils" ) -const mb = 1024 * 1024 - func initProfiling() { - debug.SetGCPercent(-1) - debug.SetMemoryLimit(50 * mb) - debug.SetMaxStack(4 * mb) - go func() { log.Info().Msgf("pprof server started at http://localhost:7777/debug/pprof/") log.Error().Err(http.ListenAndServe(":7777", nil)).Msg("pprof server failed") @@ -27,9 +21,14 @@ func initProfiling() { go func() { ticker := time.NewTicker(time.Second * 10) defer ticker.Stop() + + var m runtime.MemStats + var gcStats debug.GCStats + for range ticker.C { - var m runtime.MemStats runtime.ReadMemStats(&m) + debug.ReadGCStats(&gcStats) + log.Info().Msgf("-----------------------------------------------------") log.Info().Msgf("Timestamp: %s", time.Now().Format(time.RFC3339)) log.Info().Msgf(" Go Heap - In Use (Alloc/HeapAlloc): %s", strutils.FormatByteSize(m.Alloc)) @@ -37,8 +36,12 @@ func initProfiling() { log.Info().Msgf(" Go Stacks - In Use (StackInuse): %s", strutils.FormatByteSize(m.StackInuse)) log.Info().Msgf(" Go Runtime - Other Sys (MSpanInuse, MCacheInuse, BuckHashSys, GCSys, OtherSys): %s", strutils.FormatByteSize(m.MSpanInuse+m.MCacheInuse+m.BuckHashSys+m.GCSys+m.OtherSys)) log.Info().Msgf(" Go Runtime - Total from OS (Sys): %s", strutils.FormatByteSize(m.Sys)) + log.Info().Msgf(" Go Runtime - Freed from OS (HeapReleased): %s", strutils.FormatByteSize(m.HeapReleased)) log.Info().Msgf(" Number of Goroutines: %d", runtime.NumGoroutine()) - log.Info().Msgf(" Number of GCs: %d", m.NumGC) + log.Info().Msgf(" Number of completed GC cycles: %d", m.NumGC) + log.Info().Msgf(" Number of GCs: %d", gcStats.NumGC) + log.Info().Msgf(" Total GC time: %s", gcStats.PauseTotal) + log.Info().Msgf(" Last GC time: %s", gcStats.LastGC.Format(time.DateTime)) log.Info().Msg("-----------------------------------------------------") } }()