diff --git a/cmd/server-rlimit-nix.go b/cmd/server-rlimit-nix.go index 03341a6b1..b58720cbe 100644 --- a/cmd/server-rlimit-nix.go +++ b/cmd/server-rlimit-nix.go @@ -59,25 +59,35 @@ func setMaxMemory() error { if err != nil { return err } + err = syscall.Getrlimit(syscall.RLIMIT_AS, &rLimit) if err != nil { return err } - // Validate if rlimit memory is set to lower - // than max cache size. Then we should use such value. - if uint64(rLimit.Cur) < globalMaxCacheSize { - globalMaxCacheSize = uint64(float64(50*rLimit.Cur) / 100) + // If current rlimit is less than minRAMSize, do not set globalMaxCacheSize. + if rLimit.Cur < minRAMSize { + return nil } - // Make sure globalMaxCacheSize is less than RAM size. + // Get total RAM. stats, err := sys.GetStats() - if err != nil && err != sys.ErrNotImplemented { + if err != nil { + // Ignore sys.ErrNotImplemented error. + if err == sys.ErrNotImplemented { + err = nil + } + return err } - // If TotalRAM is >= minRAMSize we proceed to enable cache. - // cache is always 50% of the totalRAM. - if err == nil && stats.TotalRAM >= minRAMSize { - globalMaxCacheSize = uint64(float64(50*stats.TotalRAM) / 100) + + // Set 50% of current rlimit or total RAM to globalMaxCacheSize. + if stats.TotalRAM >= minRAMSize { + if rLimit.Cur < stats.TotalRAM { + globalMaxCacheSize = rLimit.Cur / 2 + } else { + globalMaxCacheSize = stats.TotalRAM / 2 + } } + return nil } diff --git a/cmd/server-rlimit-openbsd.go b/cmd/server-rlimit-openbsd.go index 16030447e..79cd7c8cb 100644 --- a/cmd/server-rlimit-openbsd.go +++ b/cmd/server-rlimit-openbsd.go @@ -59,25 +59,35 @@ func setMaxMemory() error { if err != nil { return err } + err = syscall.Getrlimit(syscall.RLIMIT_DATA, &rLimit) if err != nil { return err } - // Validate if rlimit memory is set to lower - // than max cache size. Then we should use such value. - if uint64(rLimit.Cur) < globalMaxCacheSize { - globalMaxCacheSize = uint64(float64(50*rLimit.Cur) / 100) + // If current rlimit is less than minRAMSize, do not set globalMaxCacheSize. + if rLimit.Cur < minRAMSize { + return nil } - // Make sure globalMaxCacheSize is less than RAM size. + // Get total RAM. stats, err := sys.GetStats() - if err != nil && err != sys.ErrNotImplemented { + if err != nil { + // Ignore sys.ErrNotImplemented error. + if err == sys.ErrNotImplemented { + err = nil + } + return err } - // If TotalRAM is >= minRAMSize we proceed to enable cache. - // cache is always 50% of the totalRAM. - if err == nil && stats.TotalRAM >= minRAMSize { - globalMaxCacheSize = uint64(float64(50*stats.TotalRAM) / 100) + + // Set 50% of current rlimit or total RAM to globalMaxCacheSize. + if stats.TotalRAM >= minRAMSize { + if rLimit.Cur < stats.TotalRAM { + globalMaxCacheSize = rLimit.Cur / 2 + } else { + globalMaxCacheSize = stats.TotalRAM / 2 + } } + return nil } diff --git a/cmd/server-rlimit-win.go b/cmd/server-rlimit-win.go index 954e0f734..b153392be 100644 --- a/cmd/server-rlimit-win.go +++ b/cmd/server-rlimit-win.go @@ -28,15 +28,21 @@ func setMaxOpenFiles() error { } func setMaxMemory() error { - // Make sure globalMaxCacheSize is less than RAM size. + // Get total RAM. stats, err := sys.GetStats() - if err != nil && err != sys.ErrNotImplemented { + if err != nil { + // Ignore sys.ErrNotImplemented error. + if err == sys.ErrNotImplemented { + err = nil + } + return err } - // If TotalRAM is <= minRAMSize we proceed to enable cache. - // cache is always 50% of the totalRAM. - if err == nil && stats.TotalRAM >= minRAMSize { - globalMaxCacheSize = uint64(float64(50*stats.TotalRAM) / 100) + + // Set 50% of total RAM to globalMaxCacheSize. + if stats.TotalRAM >= minRAMSize { + globalMaxCacheSize = stats.TotalRAM / 2 } + return nil }