feat: Run garbage collection before profiling heap

- This avoids returning that some memory is allocated for something
that's actually no longer in used. Go's standard library also does this
for testing and benchmarking when returning memory profiles. A canonical
link that this recommended is the example "To add equivalent profiling
support to a standalone program" in https://pkg.go.dev/runtime/pprof
This commit is contained in:
Gusted 2025-01-03 09:33:09 +01:00
parent 3f44b97b5f
commit ebe6ebe3f3
No known key found for this signature in database
GPG key ID: FD821B732837125F

View file

@ -6,6 +6,7 @@ package admin
import (
"archive/zip"
"fmt"
"runtime"
"runtime/pprof"
"runtime/trace"
"time"
@ -73,5 +74,8 @@ func MonitorDiagnosis(ctx *context.Context) {
ctx.ServerError("Failed to create zip file", err)
return
}
// To avoid showing memory that actually can be cleaned, run the garbage
// collector.
runtime.GC()
_ = pprof.Lookup("heap").WriteTo(f, 0)
}