How to Use runtime.ReadMemStats in Go

Use runtime.ReadMemStats to get current memory allocation stats like total allocs and GC counts.

Use runtime.ReadMemStats to populate a runtime.MemStats struct with current memory allocation statistics for your Go program.

import "runtime"

var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("Alloc: %v MB\n", m.Alloc/1024/1024)

This function updates the struct with metrics like total memory allocated, number of allocations, and garbage collection counts.