将获取系统内存函数放入到utils中

This commit is contained in:
GoEdgeLab
2021-12-29 10:53:59 +08:00
parent 1a22d2959d
commit 42710c4e36
3 changed files with 12 additions and 7 deletions

27
internal/utils/system.go Normal file
View File

@@ -0,0 +1,27 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package utils
import (
"github.com/shirou/gopsutil/mem"
)
var systemTotalMemory = -1
func init() {
_ = SystemMemoryGB()
}
func SystemMemoryGB() int {
if systemTotalMemory > 0 {
return systemTotalMemory
}
stat, err := mem.VirtualMemory()
if err != nil {
return 0
}
systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024)
return systemTotalMemory
}