代码拉取完成,页面将自动刷新
// Package mem is a library that counts system and process memory usage.
package mem
import (
"fmt"
"runtime"
"github.com/shirou/gopsutil/v3/mem"
)
// System memory information
type System struct {
Total uint64 `json:"total"` // total physical memory capacity, unit(M)
Free uint64 `json:"free"` // free physical memory capacity, unit(M)
UsagePercent float64 `json:"usage_percent"` // memory usage, unit(%)
}
// Process memory information
type Process struct {
Alloc uint64 `json:"alloc"` // allocated memory capacity, unit(M)
TotalAlloc uint64 `json:"total_alloc"` // cumulative allocated memory capacity, unit(M)
Sys uint64 `json:"sys"` // requesting memory capacity from the system, unit(M)
NumGc uint32 `json:"num_gc"` // number of GC cycles
}
// GetSystemMemory get system memory
func GetSystemMemory() *System {
info, err := mem.VirtualMemory()
if err != nil {
fmt.Printf("mem.VirtualMemory error: %v\n", err)
return &System{}
}
return &System{
Total: info.Total >> 20,
Free: info.Free >> 20,
UsagePercent: info.UsedPercent,
}
}
// GetProcessMemory get process memory
func GetProcessMemory() *Process {
info := &runtime.MemStats{}
runtime.ReadMemStats(info)
return &Process{
Alloc: info.Alloc >> 20,
TotalAlloc: info.TotalAlloc >> 20,
Sys: info.Sys >> 20,
NumGc: info.NumGC,
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。