mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			49 lines
		
	
	
		
			857 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			857 B
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
						|
 | 
						|
package memutils
 | 
						|
 | 
						|
import (
 | 
						|
	teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
 | 
						|
	"github.com/shirou/gopsutil/v3/mem"
 | 
						|
)
 | 
						|
 | 
						|
var systemTotalMemory = -1
 | 
						|
var systemMemoryBytes uint64
 | 
						|
 | 
						|
func init() {
 | 
						|
	if !teaconst.IsMain {
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	_ = SystemMemoryGB()
 | 
						|
}
 | 
						|
 | 
						|
// SystemMemoryGB 系统内存GB数量
 | 
						|
// 必须保证不小于1
 | 
						|
func SystemMemoryGB() int {
 | 
						|
	if systemTotalMemory > 0 {
 | 
						|
		return systemTotalMemory
 | 
						|
	}
 | 
						|
 | 
						|
	stat, err := mem.VirtualMemory()
 | 
						|
	if err != nil {
 | 
						|
		return 1
 | 
						|
	}
 | 
						|
 | 
						|
	systemMemoryBytes = stat.Total
 | 
						|
 | 
						|
	systemTotalMemory = int(stat.Total / (1 << 30))
 | 
						|
	if systemTotalMemory <= 0 {
 | 
						|
		systemTotalMemory = 1
 | 
						|
	}
 | 
						|
 | 
						|
	setMaxMemory(systemTotalMemory)
 | 
						|
 | 
						|
	return systemTotalMemory
 | 
						|
}
 | 
						|
 | 
						|
// SystemMemoryBytes 系统内存总字节数
 | 
						|
func SystemMemoryBytes() uint64 {
 | 
						|
	return systemMemoryBytes
 | 
						|
}
 |