go-watchdog icon indicating copy to clipboard operation
go-watchdog copied to clipboard

Consider using debug.FreeOsMemory() instead of runtime.GC()

Open ribasushi opened this issue 4 years ago • 2 comments

go-watchdog currently uses runtime.GC(), which does force an internal GC run but does not immediately force OS-visible memory release. Replacing that line with debug.FreeOsMemory() is likely to lead to less frequent OOM-killer sprees and to better observability by monitoring tools etc.

ribasushi avatar Mar 07 '21 18:03 ribasushi

Go 1.16 uses MADV_DONTNEED by default, which should (in theory) returned memory quicker to the OS. That said, I'm happy putting this behind an env var feature flag, because it can cause memory fragmentation in the long run, so it should be used with care.

raulk avatar Mar 12 '21 17:03 raulk

I don't have the bandwidth for this right now, so feel free to submit a PR. What I would do is:

  • add a global var ReleaseFn func() = runtime.GC
  • in the init logic, override that with debug.FreeOSMemory if env HEAPDOG_FREEOSMEMORY=1.
  • replace all calls to runtime.GC() with ReleaseFn().
  • unfortunately the heap-driven watchdog delegates GC to the runtime by dynamically adjusting GOGC based on the utilisation and the policy, so we won't be able to effect this case. Document the exception instead.

raulk avatar Mar 12 '21 17:03 raulk