Skip to content

Instantly share code, notes, and snippets.

@j33ty
Created May 22, 2019 20:54
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save j33ty/79e8b736141be19687f565ea4c6f4226 to your computer and use it in GitHub Desktop.
Save j33ty/79e8b736141be19687f565ea4c6f4226 to your computer and use it in GitHub Desktop.
Go print current memory
package main
import (
"runtime"
"fmt"
"time"
)
func main() {
// Print our starting memory usage (should be around 0mb)
PrintMemUsage()
var overall [][]int
for i := 0; i<4; i++ {
// Allocate memory using make() and append to overall (so it doesn't get
// garbage collected). This is to create an ever increasing memory usage
// which we can track. We're just using []int as an example.
a := make([]int, 0, 999999)
overall = append(overall, a)
// Print our memory usage at each interval
PrintMemUsage()
time.Sleep(time.Second)
}
// Clear our memory and print usage, unless the GC has run 'Alloc' will remain the same
overall = nil
PrintMemUsage()
// Force GC to clear up, should see a memory drop
runtime.GC()
PrintMemUsage()
}
// PrintMemUsage outputs the current, total and OS memory being used. As well as the number
// of garage collection cycles completed.
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
@j33ty
Copy link
Author

j33ty commented May 22, 2019

  1. Alloc uint64
    • Alloc is bytes of allocated heap objects.
    • "Allocated" heap objects include all reachable objects, as well as unreachable objects that the garbage collector has not yet freed.
    • Specifically, Alloc increases as heap objects are allocated and decreases as the heap is swept and unreachable objects are freed.
    • Sweeping occurs incrementally between GC cycles, so these two processes occur simultaneously, and as a result Alloc tends to change smoothly (in contrast with the sawtooth that is typical of stop-the-world garbage collectors).
  2. TotalAlloc uint64
    • 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.
  3. Sys uint64
    • 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.
  4. NumGC uint32
    • NumGC is the number of completed GC cycles.

@Arc-Jung
Copy link

Arc-Jung commented Dec 29, 2021

@j33ty
This code is good to use easily.

@lenta-platform
Copy link

@j33ty thank you, nice helper for developing stage :)

@u4rang
Copy link

u4rang commented Oct 5, 2022

Wonderful : )

I understand easily ~ Thanks

@hyungi
Copy link

hyungi commented Nov 23, 2022

@j33ty Quite useful!

@yayayahei
Copy link

@j33ty This script helped me resolve the OOM error. Appreciate!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment