add: slice adv. ops. exercises
This commit is contained in:
24
16-slices/exercises/24-fix-the-memory-leak/api/api.go
Normal file
24
16-slices/exercises/24-fix-the-memory-leak/api/api.go
Normal file
@ -0,0 +1,24 @@
|
||||
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)
|
||||
}
|
67
16-slices/exercises/24-fix-the-memory-leak/main.go
Normal file
67
16-slices/exercises/24-fix-the-memory-leak/main.go
Normal file
@ -0,0 +1,67 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/inancgumus/learngo/16-slices/exercises/24-fix-the-memory-leak/api"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Fix the memory leak
|
||||
//
|
||||
// You're receiving millions of temperature data points from
|
||||
// an API, but you only need the last 10 data points.
|
||||
//
|
||||
// Currently, there is a memory leak in your program.
|
||||
// Find the leak and fix it.
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
// > Memory Usage: 116 KB
|
||||
// > Memory Usage: 118 KB
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT EXPLANATION
|
||||
//
|
||||
// Your output will be different. Your goal is to reduce the
|
||||
// difference between the two measurements of the memory usage.
|
||||
//
|
||||
// For the expected output above:
|
||||
//
|
||||
// 118 KB - 116 KB = Only 2 KB so that's OK.
|
||||
//
|
||||
// However, in the current program, because of the memory leak,
|
||||
// the difference is huge: about ~60 MB. Run the program and,
|
||||
// see yourself.
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// reports the initial memory usage
|
||||
api.Report()
|
||||
|
||||
// reads 65 MB of temperature data into the memory!
|
||||
temps := api.Read()
|
||||
|
||||
// -----------------------------------------------------
|
||||
// ✪ ONLY ADD YOUR CODE INSIDE THIS BOX ✪
|
||||
//
|
||||
|
||||
//
|
||||
// ✪ ONLY ADD YOUR CODE INSIDE THIS BOX ✪
|
||||
// -----------------------------------------------------
|
||||
|
||||
// fix the problem so that the memory usage stays low
|
||||
// dont touch this code
|
||||
api.Report()
|
||||
fmt.Fprintln(ioutil.Discard, temps[0])
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
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)
|
||||
}
|
45
16-slices/exercises/24-fix-the-memory-leak/solution/main.go
Normal file
45
16-slices/exercises/24-fix-the-memory-leak/solution/main.go
Normal file
@ -0,0 +1,45 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/inancgumus/learngo/16-slices/exercises/24-fix-the-memory-leak/solution/api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// reports the initial memory usage
|
||||
api.Report()
|
||||
|
||||
// reads 65 MB of temperature data into the memory!
|
||||
temps := api.Read()
|
||||
|
||||
//
|
||||
// SOLUTION #1:
|
||||
//
|
||||
|
||||
// clone the last 10 elements of the returned temperatures
|
||||
// into a new slice
|
||||
need := make([]int, 10)
|
||||
copy(need, temps[len(temps)-10:])
|
||||
// make the temp slice lose reference to its backing array
|
||||
// so that it can be cleaned from the memory
|
||||
temps = need
|
||||
|
||||
//
|
||||
// SOLUTION #2:
|
||||
//
|
||||
|
||||
// The code below does the same thing like the code above but in one line.
|
||||
// temps = append([]int(nil), temps[len(temps)-10:]...)
|
||||
|
||||
api.Report()
|
||||
fmt.Fprintln(ioutil.Discard, temps[0])
|
||||
}
|
Reference in New Issue
Block a user