package main

import (
	"fmt"
	"io/ioutil"

	"github.com/aditya43/golang/20-Slices-In-Go/39-Fixing-The-Memory-Leak/api"
)

func main() {
	// reports the initial memory usage
	api.Report()

	// 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:])

	// Make the millions slice lose reference to its backing array
	// so that its backing array can be cleaned up from memory.
	millions = last10

	// SOLUTION #2:
	// Similar to the 1st solution. It does the same thing.
	// But this code is more concise. Use this one.

	// millions = append([]int(nil), millions[len(millions)-10:]...)

	fmt.Printf("\nLast 10 elements: %d\n\n", last10)

	// ------------------------------------------------------

	api.Report()

	// don't worry about this code yet.
	fmt.Fprintln(ioutil.Discard, millions[0])
}
