Print The Current Memory Usage

· 354 words · 2 minutes read

The program below is designed to print out the current state of how much memory is being used, how much has been used and how much the system has reserved. You only really need the PrintMemUsage() function to do this, the rest of the main() is there to illustrate it working (with a gif showing this at the end of this post).

The most important figure if often the Alloc which is the amount allocated heap objects. Our example also illustrates that the Alloc memory is not reduced necessarily when a variable is cleared, but when the garbage collector runs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
    "runtime"
    "fmt"
    "time"
)

func main() {
    // Below is an example of using our PrintMemUsage() function
    // 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: /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
}

Example:

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!