限制内存缓存最大容量为系统内存的三分之一

This commit is contained in:
GoEdgeLab
2023-10-16 14:28:07 +08:00
parent 81e5a5ca34
commit 836c92ed24
3 changed files with 24 additions and 9 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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")
}