ReadMemStats

The ReadMemStats() function reads memory allocator statistics and populates them into a variable, say, m. The MemStats struct has a lot of really valuable information about in-flight memory utilization. Let's take a deep look into what values it can produce for us. An HTTP handler function that allows us to see the memory utilization of the binary may be helpful as we make more requests in our system and wish to see where our memory allocation is utilized:

  1. First, we can instantiate the program and the function:
package main

import (
"fmt"
"net/http"
"runtime"
)

func memStats(w http.ResponseWriter, r *http.Request) {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
  1. Next, we can print all of the values of the individual memory statistics that the runtime provides us with. Let's start with Alloc, Mallocs, and Frees:
    fmt.Fprintln(w, "Alloc:", memStats.Alloc)
fmt.Fprintln(w, "Total Alloc:", memStats.TotalAlloc)
fmt.Fprintln(w, "Sys:", memStats.Sys)
fmt.Fprintln(w, "Lookups:", memStats.Lookups)
fmt.Fprintln(w, "Mallocs:", memStats.Mallocs)
fmt.Fprintln(w, "Frees:", memStats.Frees)
  1. Now, let's look at heap information:
    fmt.Fprintln(w, "Heap Alloc:", memStats.HeapAlloc)
fmt.Fprintln(w, "Heap Sys:", memStats.HeapSys)
fmt.Fprintln(w, "Heap Idle:", memStats.HeapIdle)
fmt.Fprintln(w, "Heap In Use:", memStats.HeapInuse)
fmt.Fprintln(w, "Heap Released:", memStats.HeapReleased)
fmt.Fprintln(w, "Heap Objects:", memStats.HeapObjects)
  1. Next, we look at stack/span/cache/bucket allocations:
    fmt.Fprintln(w, "Stack In Use:", memStats.StackInuse)
fmt.Fprintln(w, "Stack Sys:", memStats.StackSys)
fmt.Fprintln(w, "MSpanInuse:", memStats.MSpanInuse)
fmt.Fprintln(w, "MSpan Sys:", memStats.MSpanSys)
fmt.Fprintln(w, "MCache In Use:", memStats.MCacheInuse)
fmt.Fprintln(w, "MCache Sys:", memStats.MCacheSys)
fmt.Fprintln(w, "Buck Hash Sys:", memStats.BuckHashSys)
  1. Then, we look at garbage collection information:
    fmt.Fprintln(w, "EnableGC:", memStats.EnableGC)
fmt.Fprintln(w, "GCSys:", memStats.GCSys)
fmt.Fprintln(w, "Other Sys:", memStats.OtherSys)
fmt.Fprintln(w, "Next GC:", memStats.NextGC)
fmt.Fprintln(w, "Last GC:", memStats.LastGC)
fmt.Fprintln(w, "Num GC:", memStats.NumGC)
fmt.Fprintln(w, "Num Forced GC:", memStats.NumForcedGC)
  1. Now, let's look at garbage collection interruption information:
    fmt.Fprintln(w, "Pause Total NS:", memStats.PauseTotalNs)
fmt.Fprintln(w, "Pause Ns:", memStats.PauseNs)
fmt.Fprintln(w, "Pause End:", memStats.PauseEnd)
fmt.Fprintln(w, "GCCPUFraction:", memStats.GCCPUFraction)
fmt.Fprintln(w, "BySize Size:", memStats.BySize)
  1. Next, we instantiate a simple HTTP server:
 func main() {
http.HandleFunc("/", memStats)
http.ListenAndServe(":1234", nil)
}

Here, we can use our Apache bench tool to generate a bit of load on our memory allocator:

ab -n 1000 -c 1000 http://localhost:1234/

Finally, we can see some active HTTP server information, along with a response, by making a request to localhost:1234:

The definitions for all of the MemStats values can be found in the documentation at https://golang.org/pkg/runtime/#MemStats.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.135.80