From 2acf890b8e9da57b50e5acf89649d308ac996778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 16 Oct 2023 14:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E5=88=B6=E5=86=85=E5=AD=98=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=9C=80=E5=A4=A7=E5=AE=B9=E9=87=8F=E4=B8=BA=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=86=85=E5=AD=98=E7=9A=84=E4=B8=89=E5=88=86=E4=B9=8B?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/storage_memory.go | 20 ++++++++++++-------- internal/utils/system.go | 10 +++++++++- internal/utils/system_test.go | 3 +++ 3 files changed, 24 insertions(+), 9 deletions(-) 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") }