Files

125 lines
3.1 KiB
Go
Raw Permalink Normal View History

2019-03-05 23:32:32 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-10-30 19:34:44 +03:00
// 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 main
import (
"fmt"
"io/ioutil"
"github.com/inancgumus/learngo/16-slices/exercises/24-fix-the-memory-leak/api"
)
// ---------------------------------------------------------
// EXERCISE: Fix the memory leak
//
2019-08-22 20:55:47 +03:00
// WARNING
2019-03-05 23:32:32 +03:00
//
2019-08-22 20:55:47 +03:00
// 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.
2019-08-18 13:29:12 +03:00
//
//
2019-08-22 20:55:47 +03:00
// GOAL
2019-08-18 13:29:12 +03:00
//
2019-08-22 20:55:47 +03:00
// 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()`.
2019-08-18 13:29:12 +03:00
//
2019-08-22 20:55:47 +03:00
//
// PROBLEM
//
// `main()` calls `api.Report()` that reports the current
2019-08-22 20:55:47 +03:00
// memory usage.
//
// After that, `main()` calls `api.Read()` that returns
2019-08-22 21:21:26 +03:00
// a slice with 10 millions of elements. But you only need
2019-08-22 20:55:47 +03:00
// the last 10 elements of the returned slice.
//
//
2019-08-22 21:21:26 +03:00
// WHAT YOU NEED TO DO
2019-08-22 20:55:47 +03:00
//
// You only need to change the code in `main()`. Please
// do not touch the code in `api/api.go`.
//
2019-08-18 13:29:12 +03:00
//
2019-08-22 21:21:26 +03:00
// CURRENT OUTPUT
//
// > Memory Usage: 113 KB
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// Last 10 elements: [...]
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// > Memory Usage: 65651 KB
2019-03-05 23:32:32 +03:00
//
2019-08-22 21:21:26 +03:00
// + Before `api.Read()` call: It uses 113 KB of memory.
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// + After `api.Read()` call : It uses 65 MB of memory.
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// + This means that, `main()` never releases the memory.
// This is the leak.
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// + Your goal is to release the unused memory. Remember,
// you only need 10 elements but in the current code
// below you have a slice with 10 millions of elements.
2019-08-22 20:55:47 +03:00
//
2019-03-05 23:32:32 +03:00
//
2019-08-22 21:21:26 +03:00
// EXPECTED OUTPUT
2019-03-05 23:32:32 +03:00
//
2019-08-22 21:21:26 +03:00
// > Memory Usage: 116 KB
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// Last 10 elements: [...]
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// > Memory Usage: 118 KB
2019-03-05 23:32:32 +03:00
//
2019-08-22 21:21:26 +03:00
// + In the expected output, `main()` releases the memory.
2019-08-22 20:55:47 +03:00
//
2019-08-22 21:21:26 +03:00
// 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 118 KB.
2019-08-22 20:55:47 +03:00
//
//
2019-08-22 21:21:26 +03:00
// ADDITIONAL NOTE
2019-03-05 23:32:32 +03:00
//
2019-08-22 20:55:47 +03:00
// Memory leak means: Your program is using unnecessary
// computer memory. It doesn't release memory that is
// no longer needed.
2019-03-05 23:32:32 +03:00
//
// See this for more information:
// https://en.wikipedia.org/wiki/Memory_leak
2019-03-05 23:32:32 +03:00
//
2019-08-22 20:55:47 +03:00
//
// HINTS
2019-08-22 20:55:47 +03:00
//
// Check out `hints.md` file if you get stuck.
2019-03-05 23:32:32 +03:00
//
// ---------------------------------------------------------
func main() {
// reports the initial memory usage
api.Report()
2019-08-22 20:55:47 +03:00
// returns a slice with 10 million elements.
// it allocates 65 MB of memory space.
millions := api.Read()
2019-03-05 23:32:32 +03:00
// -----------------------------------------------------
2019-08-22 20:55:47 +03:00
// ✪ ONLY CHANGE THE CODE IN THIS AREA ✪
last10 := millions[len(millions)-10:]
fmt.Printf("\nLast 10 elements: %d\n\n", last10)
2019-03-05 23:32:32 +03:00
2019-08-22 20:55:47 +03:00
// ✪ ONLY CHANGE THE CODE IN THIS AREA ✪
2019-03-05 23:32:32 +03:00
// -----------------------------------------------------
api.Report()
2019-08-18 13:29:12 +03:00
2019-08-22 20:55:47 +03:00
// don't worry about this code.
fmt.Fprintln(ioutil.Discard, millions[0])
2019-03-05 23:32:32 +03:00
}