From f55781f64372a485e447655807bf6d4364eacd2d Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 5 Mar 2023 12:34:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BD=AF=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=80=BC=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/utils/system.go | 6 ++++++ internal/utils/system_1.19.go | 23 +++++++++++++++++++++++ internal/utils/system_before_1.19.go | 9 +++++++++ 3 files changed, 38 insertions(+) create mode 100644 internal/utils/system_1.19.go create mode 100644 internal/utils/system_before_1.19.go diff --git a/internal/utils/system.go b/internal/utils/system.go index fa040e5..0e05ece 100644 --- a/internal/utils/system.go +++ b/internal/utils/system.go @@ -23,5 +23,11 @@ func SystemMemoryGB() int { } systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024) + if systemTotalMemory <= 0 { + systemTotalMemory = 1 + } + + setMaxMemory(systemTotalMemory) + return systemTotalMemory } diff --git a/internal/utils/system_1.19.go b/internal/utils/system_1.19.go new file mode 100644 index 0000000..8ada108 --- /dev/null +++ b/internal/utils/system_1.19.go @@ -0,0 +1,23 @@ +// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . +//go:build go1.19 + +package utils + +import ( + "runtime/debug" +) + +// 设置软内存最大值 +func setMaxMemory(memoryGB int) { + if memoryGB <= 0 { + memoryGB = 1 + } + var maxMemoryBytes int64 = 0 + if memoryGB > 10 { + maxMemoryBytes = int64(memoryGB-2) << 30 // 超过10G内存的允许剩余2G内存 + } else { + maxMemoryBytes = (int64(memoryGB) << 30) * 100 / 80 // 默认 80% + } + + debug.SetMemoryLimit(maxMemoryBytes) +} diff --git a/internal/utils/system_before_1.19.go b/internal/utils/system_before_1.19.go new file mode 100644 index 0000000..dc8e394 --- /dev/null +++ b/internal/utils/system_before_1.19.go @@ -0,0 +1,9 @@ +// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . +//go:build !go1.19 + +package utils + +// 设置软内存最大值 +func setMaxMemory(memoryGB int) { + +} \ No newline at end of file