Golang : How to get garbage collection data?
Problem :
You have read the previous tutorial on recovering memory and you want to trigger garbage collection manually with runtime/debug.FreeOSMemory()
or calculate the percentage to use with runtime/debug.SetGCPercent()
function, but you need to figure out what are the parameters to take into account and other information such as the last garbage collection time, available heap size, the number of garbage collection and etc.
Solution :
Use runtime.ReadMemStats()
and runtime/debug.ReadGCStats()
functions to gather the garbage collection data.
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
func main() {
s := new(runtime.MemStats)
runtime.ReadMemStats(s)
fmt.Println("Alloc : ", s.Alloc)
fmt.Println("Total Alloc : ", s.TotalAlloc)
fmt.Println("Sys : ", s.Sys)
fmt.Println("Lookups : ", s.Lookups)
// Garbage Collection data
fmt.Println("HeapAlloc : ", s.HeapAlloc)
fmt.Println("Next Garbage Collection : ", s.NextGC)
fmt.Println("Auto garbage collection will happen when HeapAlloc >= NextGC")
fmt.Println("Last Garbage Collection : ", s.LastGC)
fmt.Println("Total number of GC pause : ", s.PauseTotalNs)
// [256]uint64 array
// we will take the first element for this example...you might want to loop the array
fmt.Println("Most recent pause : ", s.PauseNs[0])
fmt.Println("Recent pause end times : ", s.PauseEnd[0])
fmt.Println("Number of Garbage Collections : ", s.NumGC)
fmt.Println("Is Garbage Collection enabled? : ", s.EnableGC)
fmt.Println("Is Garbage Collection debug enabled? : ", s.DebugGC)
// ---------------------------
gcs := new(debug.GCStats)
debug.ReadGCStats(gcs)
fmt.Println("Last Garbage Collection : ", gcs.LastGC)
fmt.Println("Number of Garbage Collection : ", gcs.NumGC)
fmt.Println("Total pause for all collections : ", gcs.PauseTotal)
// []time.Duration array
fmt.Println("Most recent pause history : ", gcs.Pause)
// []time.Time array
fmt.Println("Most recent pause end times history : ", gcs.PauseEnd)
// []time.Duration array
// Pause Quantiles == values taken at regular interval
fmt.Println("Most recent pause quantiles history : ", gcs.PauseQuantiles)
}
References :
http://golang.org/pkg/runtime/debug/#FreeOSMemory
http://golang.org/pkg/runtime/debug/#ReadGCStats
See also : Golang : Ways to recover memory during run time.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+47.7k Golang : Convert int to byte array([]byte)
+18.4k Golang : Example for RSA package functions
+6.9k How to let Facebook Login button redirect to a particular URL ?
+21.7k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+9.2k Golang : Create and shuffle deck of cards example
+9.6k Golang : Copy map(hash table) example
+24.5k Golang : Change file read or write permission example
+14.6k Golang : GUI with Qt and OpenCV to capture image from camera
+11.3k Golang : How to flush a channel before the end of program?
+9.2k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+17.9k Golang : Login and logout a user after password verification and redirect example