In this section, we will briefly talk about how Go deals with GC, which tries to free unused memory efficiently. The Go code of garbageCol.go can be presented in two parts.
The first part is as follows:
package main import ( "fmt" "runtime" "time" ) func printStats(mem runtime.MemStats) { runtime.ReadMemStats(&mem) fmt.Println("mem.Alloc:", mem.Alloc) fmt.Println("mem.TotalAlloc:", mem.TotalAlloc) fmt.Println("mem.HeapAlloc:", mem.HeapAlloc) fmt.Println("mem.NumGC:", mem.NumGC) fmt.Println("-----") }
Every time you want to read the latest memory statistics, you should make a call to the runtime.ReadMemStats() function.
The second part, which contains the implementation of the main() function, has the following Go code:
func main()...