diff --git a/16-slices/exercises/22-adv-ops-practice/main.go b/16-slices/exercises/22-adv-ops-practice/main.go index 90b7dde..a79d85d 100644 --- a/16-slices/exercises/22-adv-ops-practice/main.go +++ b/16-slices/exercises/22-adv-ops-practice/main.go @@ -16,18 +16,6 @@ import ( // --------------------------------------------------------- func main() { - // - // Use the prettyslice package for printing the slices, - // or do it with your own Printf that matches the output - // of the prettyslice package. - // - // This allows you to see the backing array of the slices. - s.PrintBacking = true - // Shows 10 slice elements per line - s.MaxPerLine = 10 - // Prints 60 character per line - s.Width = 60 - // ######################################################## // // #1: Create a string slice: `names` with a length and @@ -127,3 +115,15 @@ func main() { // ... // s.Show("6th step", clone, sliced) } + +// +// Don't mind about this function. +// +// For printing the slices: You can either use the +// prettyslice package or `fmt.Printf`. +// +func init() { + s.PrintBacking = true // prints the backing array + s.MaxPerLine = 10 // prints 10 slice elements per line + s.Width = 60 // prints 60 character per line +} diff --git a/16-slices/exercises/22-adv-ops-practice/solution/main.go b/16-slices/exercises/22-adv-ops-practice/solution/main.go index a7ea9f4..59c3df1 100644 --- a/16-slices/exercises/22-adv-ops-practice/solution/main.go +++ b/16-slices/exercises/22-adv-ops-practice/solution/main.go @@ -12,10 +12,6 @@ import ( ) func main() { - s.PrintBacking = true - s.MaxPerLine = 10 - s.Width = 60 - // ######################################################## // // #1: Create a string slice: `names` with a length and @@ -115,3 +111,15 @@ func main() { s.Show("6th step", clone, sliced) } + +// +// Don't mind about this function. +// +// For printing the slices: You can either use the +// prettyslice package or `fmt.Printf`. +// +func init() { + s.PrintBacking = true // prints the backing array + s.MaxPerLine = 10 // prints 10 slice elements per line + s.Width = 60 // prints 60 character per line +} diff --git a/16-slices/exercises/24-fix-the-memory-leak/api/api.go b/16-slices/exercises/24-fix-the-memory-leak/api/api.go index 7798f03..041bc5c 100644 --- a/16-slices/exercises/24-fix-the-memory-leak/api/api.go +++ b/16-slices/exercises/24-fix-the-memory-leak/api/api.go @@ -6,9 +6,7 @@ import ( "runtime" ) -// DO NOT TOUCH THE FOLLOWING CODE -// THIS IS THE IMAGINARY API CODE -// YOU CANNOT CONTROL IT! +// DO NOT TOUCH THIS FILE BUT YOU CAN READ IT // Read returns a huge slice (allocates ~65 MB of memory) func Read() []int { diff --git a/16-slices/exercises/24-fix-the-memory-leak/main.go b/16-slices/exercises/24-fix-the-memory-leak/main.go index cfcdd0f..87624fe 100644 --- a/16-slices/exercises/24-fix-the-memory-leak/main.go +++ b/16-slices/exercises/24-fix-the-memory-leak/main.go @@ -17,43 +17,94 @@ import ( // --------------------------------------------------------- // EXERCISE: Fix the memory leak // -// WARNING! This is a very difficult exercise. You need to -// do some research on your own to solve it. Please don't -// get discouraged if you can't solve it yet. +// WARNING // -// Imagine that you receive millions of temperature data -// points but you only need the last 10 data points -// (temperatures). -// -// Problem: There is a memory leak in your program. -// Please find the leak and fix it. -// -// Memory leak means: Your program uses computer memory -// unnecessarily. See this: https://en.wikipedia.org/wiki/Memory_leak +// This is a very difficult exercise. You need to +// do some research on your own to solve it. Please don't +// get discouraged if you can't solve it yet. // // +// GOAL +// +// In this exercise, your goal is to reduce the memory +// usage. To do that, you need to find and fix the memory +// leak within `main()`. +// +// +// PROBLEM +// +// `main()` calls `api.Report()`. It reports the current +// memory usage. +// +// After that, it calls `api.Read()`. `api.Read()` returns +// a slice with 10 million of elements. But you only need +// the last 10 elements of the returned slice. +// +// +// WHAT YOU NEED TO DO +// +// You only need to change the code in `main()`. Please +// do not touch the code in `api/api.go`. +// + // CURRENT OUTPUT // // > Memory Usage: 113 KB +// +// Last 10 elements: [...] +// // > Memory Usage: 65651 KB // +// + Before `api.Read()` call: It uses 113 KB of memory. +// +// + After `api.Read()` call : It uses 65 MB of memory. +// +// + This means that, `main()` never releases the memory. +// This is the leak. +// +// + Your goal is to release the unused memory. +// // // EXPECTED OUTPUT // // > Memory Usage: 116 KB +// +// Last 10 elements: [...] +// // > Memory Usage: 118 KB // +// + In the expected output, `main()` releases the memory. // -// EXPECTED OUTPUT EXPLANATION +// It no longer uses 65 MB of memory. Instead, it only +// uses 118 KB of memory. That's why the second +// `api.Report()` call reports only 118 KB. // -// In the current program, because of the memory leak, -// the difference is huge: about ~60 MB. Run the program and, -// see it yourself. // -// Your goal is reducing the memory usage. +// ADDITIONAL NOTE // -// See the code in api/api.go to see how it allocates -// a huge memory. +// Memory leak means: Your program is using unnecessary +// computer memory. It doesn't release memory that is +// no longer needed. +// See this: https://en.wikipedia.org/wiki/Memory_leak +// +// +// HINTS +// +// Only read this if you get stuck. +// +// + `millions` slice's backing array uses 65 MB of memory. +// +// + Make a new slice with 10 elements, and copy the last +// 10 elements of the `millions` slice to it. This will +// create a new backing array for the new slice only +// with 10 elements. +// +// Then overwrite the `millions` slice by simply +// assigning `last10` slice to it. +// +// Remember: slice = pointer to a backing array. +// If you overwrite the slice, it will lose that +// pointer. So Go can collect the unused memory. // // --------------------------------------------------------- @@ -61,20 +112,22 @@ func main() { // reports the initial memory usage api.Report() - // reads 65 MB of temperature data into memory! - temps := api.Read() + // returns a slice with 10 million elements. + // it allocates 65 MB of memory space. + millions := api.Read() // ----------------------------------------------------- - // ✪ ONLY ADD YOUR CODE INSIDE THIS BOX ✪ - // + // ✪ ONLY CHANGE THE CODE IN THIS AREA ✪ - // - // ✪ ONLY ADD YOUR CODE INSIDE THIS BOX ✪ + last10 := millions[len(millions)-10:] + + fmt.Printf("\nLast 10 elements: %d\n\n", last10) + + // ✪ ONLY CHANGE THE CODE IN THIS AREA ✪ // ----------------------------------------------------- - // dont touch this code. api.Report() - // don't worry about this code yet. - fmt.Fprintln(ioutil.Discard, temps[0]) + // don't worry about this code. + fmt.Fprintln(ioutil.Discard, millions[0]) } diff --git a/16-slices/exercises/24-fix-the-memory-leak/solution/api/api.go b/16-slices/exercises/24-fix-the-memory-leak/solution/api/api.go index 7798f03..041bc5c 100644 --- a/16-slices/exercises/24-fix-the-memory-leak/solution/api/api.go +++ b/16-slices/exercises/24-fix-the-memory-leak/solution/api/api.go @@ -6,9 +6,7 @@ import ( "runtime" ) -// DO NOT TOUCH THE FOLLOWING CODE -// THIS IS THE IMAGINARY API CODE -// YOU CANNOT CONTROL IT! +// DO NOT TOUCH THIS FILE BUT YOU CAN READ IT // Read returns a huge slice (allocates ~65 MB of memory) func Read() []int { diff --git a/16-slices/exercises/24-fix-the-memory-leak/solution/main.go b/16-slices/exercises/24-fix-the-memory-leak/solution/main.go index 97bccac..6bec313 100644 --- a/16-slices/exercises/24-fix-the-memory-leak/solution/main.go +++ b/16-slices/exercises/24-fix-the-memory-leak/solution/main.go @@ -18,39 +18,35 @@ func main() { // reports the initial memory usage api.Report() - // reads 65 MB of temperature data into the memory! - temps := api.Read() + // returns a slice with 10 million elements. + // it allocates 65 MB of memory space. + millions := api.Read() // ------------------------------------------------------ + // SOLUTION #1: - // ------------------------------------------------------ + // Copy the last 10 elements of the returned slice + // to a new slice. This will create a new backing array + // only with 10 elements. + last10 := make([]int, 10) + copy(last10, millions[len(millions)-10:]) - // - // Copy the last 10 elements of the returned temperatures - // to a new slice. - // - // This will create a new backing array. - // - need := make([]int, 10) - copy(need, temps[len(temps)-10:]) + // Make the millions slice lose reference to its backing array + // so that its backing array can be cleaned up from memory. + millions = last10 - // - // Make the temp slice lose reference to its backing array - // so that its backing array can be cleaned from the memory. - // - temps = need - - // ------------------------------------------------------ // SOLUTION #2: - // ------------------------------------------------------ - // Similar to the 1st solution. It does the same thing. // But this code is more concise. Use this one. - // temps = append([]int(nil), temps[len(temps)-10:]...) + // millions = append([]int(nil), millions[len(millions)-10:]...) + + fmt.Printf("\nLast 10 elements: %d\n\n", last10) // ------------------------------------------------------ - // don't worry about this code yet. + api.Report() - fmt.Fprintln(ioutil.Discard, temps[0]) + + // don't worry about this code yet. + fmt.Fprintln(ioutil.Discard, millions[0]) }