run debug.FreeOSMemory when forcing GC (#16)

This commit is contained in:
Marten Seemann 2021-12-13 16:19:07 +04:00 committed by GitHub
parent bee183a29f
commit 60d0a53755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -36,6 +36,11 @@ var (
// Clock can be used to inject a mock clock for testing.
Clock = clock.New()
// ForcedGCFunc specifies the function to call when forced GC is necessary.
// Its default value is runtime.GC, but it can be set to debug.FreeOSMemory
// to force the release of memory to the OS.
ForcedGCFunc = runtime.GC
// NotifyGC, if non-nil, will be called when a GC has happened.
// Deprecated: use RegisterPostGCNotifee instead.
NotifyGC func() = func() {}
@ -370,7 +375,7 @@ func forceGC(memstats *runtime.MemStats) {
// finalizers are run in the sweep phase.
start := time.Now()
notificationsTook := start.Sub(startNotify)
runtime.GC()
ForcedGCFunc()
took := time.Since(start)
memstatsFn(memstats)