Created
July 21, 2017 11:23
-
-
Save PumpkinSeed/70e7b85dd77f3e105c1c84caf9e744fa to your computer and use it in GitHub Desktop.
Visual example how golang GC and memory usage working.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"runtime" | |
"time" | |
) | |
var counter int | |
func main() { | |
start := time.Now() | |
runtime.GOMAXPROCS(8) | |
counter = 1 | |
go memoryUsage() | |
for { | |
go fib(counter) | |
counter++ | |
time.Sleep(1 * time.Millisecond) | |
if counter == 100000 { | |
break | |
} | |
} | |
var m runtime.MemStats | |
runtime.ReadMemStats(&m) | |
since := time.Since(start) | |
log.Printf("Alloc = %v TotalAlloc = %v Sys = %v NumGC = %v Fibs = %d Spent: %dms\n", m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC, counter, int64(since/time.Millisecond)) | |
} | |
func memoryUsage() { | |
for { | |
var m runtime.MemStats | |
runtime.ReadMemStats(&m) | |
log.Printf("Alloc = %v TotalAlloc = %v Sys = %v NumGC = %v Fibs = %d\n", m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC, counter) | |
time.Sleep(200 * time.Millisecond) | |
} | |
} | |
func fib(counter int) { | |
first := 0 | |
second := 1 | |
for { | |
swap := first | |
first = second | |
second = swap + first | |
if second == 99194853094755497 { | |
break | |
} | |
time.Sleep(1 * time.Millisecond) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment