RIP gosigar

This commit is contained in:
vyzo 2022-01-09 10:32:45 +02:00
parent 488f08cb1d
commit d22a48d4b0

View File

@ -1,10 +1,9 @@
//go:build cgo && !ios
// +build cgo,!ios
package rcmgr package rcmgr
import ( import (
"github.com/elastic/gosigar" "runtime"
"github.com/pbnjay/memory"
) )
// DynamicLimit is a limit with dynamic memory values, based on available (free) memory // DynamicLimit is a limit with dynamic memory values, based on available (free) memory
@ -23,12 +22,17 @@ type DynamicLimit struct {
var _ Limit = (*DynamicLimit)(nil) var _ Limit = (*DynamicLimit)(nil)
func (l *DynamicLimit) GetMemoryLimit() int64 { func (l *DynamicLimit) GetMemoryLimit() int64 {
var mem gosigar.Mem freemem := memory.FreeMemory()
if err := mem.Get(); err != nil {
panic(err)
}
limit := int64(float64(mem.ActualFree) * l.MemoryFraction) // account for memory retained by the runtime that is actually free
// HeapInuse - HeapAlloc is the memory available in allocator spans
// HeapIdle - HeapReleased is memory held by the runtime that could be returned to the OS
var memstat runtime.MemStats
runtime.ReadMemStats(&memstat)
freemem += (memstat.HeapInuse - memstat.HeapAlloc) + (memstat.HeapIdle - memstat.HeapReleased)
limit := int64(float64(freemem) * l.MemoryFraction)
if limit < l.MinMemory { if limit < l.MinMemory {
limit = l.MinMemory limit = l.MinMemory
} else if limit > l.MaxMemory { } else if limit > l.MaxMemory {