github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/memstats/memstats.go (about) 1 package main 2 3 import ( 4 "math/rand" 5 "runtime" 6 "time" 7 ) 8 9 func main() { 10 11 ms := runtime.MemStats{} 12 13 for { 14 escapesToHeap() 15 runtime.ReadMemStats(&ms) 16 println("Heap before GC. Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys) 17 runtime.GC() 18 runtime.ReadMemStats(&ms) 19 println("Heap after GC. Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys) 20 time.Sleep(5 * time.Second) 21 } 22 23 } 24 25 func escapesToHeap() { 26 n := rand.Intn(100) 27 println("Doing ", n, " iterations") 28 for i := 0; i < n; i++ { 29 s := make([]byte, i) 30 _ = append(s, 42) 31 } 32 }