Files

45 lines
1.3 KiB
Go
Raw Permalink Normal View History

2019-10-30 19:34:44 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
2019-03-05 23:32:32 +03:00
package api
import (
"fmt"
"math/rand"
"runtime"
)
2019-08-22 20:55:47 +03:00
// DO NOT TOUCH THIS FILE BUT YOU CAN READ IT
2019-03-05 23:32:32 +03:00
// Read returns a huge slice (allocates ~65 MB of memory)
func Read() []int {
2019-08-18 13:29:12 +03:00
// 2 << 22 means 2^(22 + 1)
// See this: https://en.wikipedia.org/wiki/Arithmetic_shift
// Perm function returns a slice with random integers in it.
// Here it returns a slice with random integers that contains
// 8,388,608 elements. One int value is 8 bytes.
// So: 8,388,608 * 8 = ~65MB
2019-03-05 23:32:32 +03:00
return rand.Perm(2 << 22)
}
// Report cleans the memory and prints the current memory usage
2019-08-18 13:29:12 +03:00
// Don't worry about this code. You don't need to understand it.
//
// However, if you're curious, read on.
//
// The following code runs the garbage collector to clean
// up the allocated resources, and then it reads the current
// memory statistics into the m variable.
2019-03-05 23:32:32 +03:00
func Report() {
var m runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m)
fmt.Printf(" > Memory Usage: %v KB\n", m.Alloc/1024)
}