go-utils/stats/memory.go

38 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-08-14 21:19:37 +00:00
package stats
import (
"runtime"
"github.com/MedzikUser/go-utils/utils"
)
type StatMemory struct {
2021-08-19 15:17:58 +00:00
// Alloc is bytes of allocated heap objects.
2021-08-19 15:19:45 +00:00
Alloc string
2021-08-19 15:17:58 +00:00
// TotalAlloc is cumulative bytes allocated for heap objects.
//
// TotalAlloc increases as heap objects are allocated, but unlike Alloc and HeapAlloc, it does not decrease when objects are freed.
2021-08-14 21:19:37 +00:00
TotalAlloc string
2021-08-19 15:17:58 +00:00
// Sys is the total bytes of memory obtained from the OS.
//
// Sys is the sum of the XSys fields below. Sys measures the virtual address space reserved by the Go runtime for the heap, stacks, and other internal data structures. It's likely that not all of the virtual address space is backed by physical memory at any given moment, though in general it all was at some point.
2021-08-19 15:19:45 +00:00
Sys string
2021-08-19 15:17:58 +00:00
// NumGC is the number of completed GC cycles.
2021-08-19 15:19:45 +00:00
NumGC uint32
2021-08-14 21:19:37 +00:00
}
// Get Memory stats
func Memory() StatMemory {
var m runtime.MemStats
runtime.ReadMemStats(&m)
stat := StatMemory{
Alloc: utils.Bytes(m.Alloc),
TotalAlloc: utils.Bytes(m.TotalAlloc),
Sys: utils.Bytes(m.Sys),
NumGC: m.NumGC,
}
return stat
}