Files
learngo/16-slices/exercises/24-fix-the-memory-leak/solution/api/api.go

25 lines
448 B
Go
Raw Normal View History

2019-03-05 23:32:32 +03:00
package api
import (
"fmt"
"math/rand"
"runtime"
)
// DO NOT TOUCH THE FOLLOWING CODE
// THIS IS THE API
// YOU CANNOT CONTROL IT! :)
// Read returns a huge slice (allocates ~65 MB of memory)
func Read() []int {
return rand.Perm(2 << 22)
}
// Report cleans the memory and prints the current memory usage
func Report() {
var m runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m)
fmt.Printf(" > Memory Usage: %v KB\n", m.Alloc/1024)
}