2021-10-04 09:12:17 +08:00
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
2021-12-29 10:53:59 +08:00
|
|
|
package utils
|
2021-10-04 09:12:17 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-03-10 15:14:14 +08:00
|
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
2022-03-12 19:11:29 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
2021-10-04 09:12:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var systemTotalMemory = -1
|
|
|
|
|
|
2021-12-29 10:53:59 +08:00
|
|
|
func init() {
|
2023-03-10 15:14:14 +08:00
|
|
|
if !teaconst.IsMain {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:53:59 +08:00
|
|
|
_ = SystemMemoryGB()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SystemMemoryGB() int {
|
2021-10-04 09:12:17 +08:00
|
|
|
if systemTotalMemory > 0 {
|
|
|
|
|
return systemTotalMemory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stat, err := mem.VirtualMemory()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024)
|
2023-03-05 12:34:46 +08:00
|
|
|
if systemTotalMemory <= 0 {
|
|
|
|
|
systemTotalMemory = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setMaxMemory(systemTotalMemory)
|
|
|
|
|
|
2021-10-04 09:12:17 +08:00
|
|
|
return systemTotalMemory
|
|
|
|
|
}
|