diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index ce2cca0..cd446c6 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -584,13 +584,19 @@ func (this *MemoryStorage) flushItem(key string) { } func (this *MemoryStorage) memoryCapacityBytes() int64 { + var maxSystemBytes = int64(utils.SystemMemoryBytes()) / 3 // 1/3 of the system memory + if this.policy == nil { - return 0 + return maxSystemBytes } if SharedManager.MaxMemoryCapacity != nil { var capacityBytes = SharedManager.MaxMemoryCapacity.Bytes() if capacityBytes > 0 { + if capacityBytes > maxSystemBytes { + return maxSystemBytes + } + return capacityBytes } } @@ -599,17 +605,15 @@ func (this *MemoryStorage) memoryCapacityBytes() int64 { if capacity != nil { var capacityBytes = capacity.Bytes() if capacityBytes > 0 { + if capacityBytes > maxSystemBytes { + return maxSystemBytes + } return capacityBytes } } - // half of the system memory - var memoryGB = utils.SystemMemoryGB() - if memoryGB < 1 { - memoryGB = 1 - } - - return int64(memoryGB) << 30 / 2 + // 1/4 of the system memory + return maxSystemBytes } func (this *MemoryStorage) deleteWithoutLocker(key string) error { diff --git a/internal/utils/system.go b/internal/utils/system.go index aec07e5..9d69967 100644 --- a/internal/utils/system.go +++ b/internal/utils/system.go @@ -8,6 +8,7 @@ import ( ) var systemTotalMemory = -1 +var systemMemoryBytes uint64 func init() { if !teaconst.IsMain { @@ -29,7 +30,9 @@ func SystemMemoryGB() int { return 1 } - systemTotalMemory = int(stat.Total / (1<<30)) + systemMemoryBytes = stat.Total + + systemTotalMemory = int(stat.Total / (1 << 30)) if systemTotalMemory <= 0 { systemTotalMemory = 1 } @@ -38,3 +41,8 @@ func SystemMemoryGB() int { return systemTotalMemory } + +// SystemMemoryBytes 系统内存总字节数 +func SystemMemoryBytes() uint64 { + return systemMemoryBytes +} diff --git a/internal/utils/system_test.go b/internal/utils/system_test.go index df15ca1..7d81878 100644 --- a/internal/utils/system_test.go +++ b/internal/utils/system_test.go @@ -8,4 +8,7 @@ func TestSystemMemoryGB(t *testing.T) { t.Log(SystemMemoryGB()) t.Log(SystemMemoryGB()) t.Log(SystemMemoryGB()) + t.Log(SystemMemoryBytes()) + t.Log(SystemMemoryBytes()) + t.Log(SystemMemoryBytes()>>30, "GB") }