| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "runtime" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | func humanizeBytes(bytes uint64) string { |
| 10 | const ( |
| 11 | _ = iota |
| 12 | kB uint64 = 1 << (10 * iota) |
| 13 | mB |
| 14 | gB |
| 15 | tB |
| 16 | pB |
| 17 | ) |
| 18 | |
| 19 | switch { |
| 20 | case bytes < kB: |
| 21 | return fmt.Sprintf("%dB", bytes) |
| 22 | case bytes < mB: |
| 23 | return fmt.Sprintf("%.2fKB", float64(bytes)/float64(kB)) |
| 24 | case bytes < gB: |
| 25 | return fmt.Sprintf("%.2fMB", float64(bytes)/float64(mB)) |
| 26 | case bytes < tB: |
| 27 | return fmt.Sprintf("%.2fGB", float64(bytes)/float64(gB)) |
| 28 | case bytes < pB: |
| 29 | return fmt.Sprintf("%.2fTB", float64(bytes)/float64(tB)) |
| 30 | default: |
| 31 | return fmt.Sprintf("%dB", bytes) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func main() { |
| 36 | i := 0 |
| 37 | wg := sync.WaitGroup{} |
| 38 | |
| 39 | for { |
| 40 | var m runtime.MemStats |
| 41 | runtime.ReadMemStats(&m) |
| 42 | fmt.Printf("#%d: alloc = %s, routines = %d, gc = %d\n", i, humanizeBytes(m.Alloc), runtime.NumGoroutine(), m.NumGC) |
| 43 | |
| 44 | wg.Add(1) |
| 45 | go func() { |
| 46 | wg.Done() |
| 47 | }() |
| 48 | wg.Wait() |
| 49 | i = i + 1 |
| 50 | } |
| 51 | } |