Files
EdgeNode/internal/utils/mem/system.go

49 lines
857 B
Go
Raw Normal View History

// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
2024-03-28 17:17:34 +08:00
package memutils
import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
2022-03-12 19:11:29 +08:00
"github.com/shirou/gopsutil/v3/mem"
)
var systemTotalMemory = -1
var systemMemoryBytes uint64
func init() {
if !teaconst.IsMain {
return
}
_ = SystemMemoryGB()
}
2023-04-25 17:24:05 +08:00
// SystemMemoryGB 系统内存GB数量
// 必须保证不小于1
func SystemMemoryGB() int {
if systemTotalMemory > 0 {
return systemTotalMemory
}
stat, err := mem.VirtualMemory()
if err != nil {
2023-04-25 17:24:05 +08:00
return 1
}
systemMemoryBytes = stat.Total
systemTotalMemory = int(stat.Total / (1 << 30))
2023-03-05 12:34:46 +08:00
if systemTotalMemory <= 0 {
systemTotalMemory = 1
}
setMaxMemory(systemTotalMemory)
return systemTotalMemory
}
// SystemMemoryBytes 系统内存总字节数
func SystemMemoryBytes() uint64 {
return systemMemoryBytes
}