github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/MasterGO/Chap02-GoInternals/gColl.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"time"
     7  )
     8  func printStats(mem runtime.MemStats) {
     9  	runtime.ReadMemStats(&mem)
    10  
    11  	fmt.Println("mem.Alloc: ", mem.Alloc)
    12  	fmt.Println("mem.TotalAlloc: ", mem.TotalAlloc)
    13  	fmt.Println("mem.HeapAlloc: ",mem.HeapAlloc)
    14  	fmt.Println("mem.NumGC: ", mem.NumGC)
    15  	fmt.Println("-----------")
    16  }
    17  
    18  
    19  func main() {
    20  
    21  	var mem runtime.MemStats
    22  
    23  	printStats(mem)
    24  
    25  	for i:= 0; i < 10; i++ {
    26  		s := make([]byte, 50000000)
    27  
    28  		if s == nil {
    29  			fmt.Println("Operation failed!")
    30  		}
    31  	}
    32  
    33  	printStats(mem)
    34  
    35  	for i := 0; i < 10; i++ {
    36  		s := make([]byte, 100000000)
    37  		if s == nil {
    38  			fmt.Println("Operation failed!")
    39  		}
    40  		time.Sleep(5 * time.Second)
    41  	}
    42  	printStats(mem)
    43  }