| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | package main | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"fmt" | 
					
						
							|  |  |  |  | 	"runtime" | 
					
						
							|  |  |  |  | 	"runtime/debug" | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // --------------------------------------------------------- | 
					
						
							|  |  |  |  | // EXERCISE: Observe the memory allocations | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | //  In this exercise, your goal is to observe the memory allocation | 
					
						
							|  |  |  |  | //  differences between arrays and slices. | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | //  You will create, assign arrays and slices then you will print | 
					
						
							|  |  |  |  | //  the memory usage of your program on each step. | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | //  Please follow the instructions inside the code. | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | // EXPECTED OUTPUT | 
					
						
							|  |  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | //  Note that, your memory usage numbers may vary. However, the size of the | 
					
						
							|  |  |  |  | //  arrays and slices should be the same on your own system as well | 
					
						
							|  |  |  |  | //  (if you're on a 64-bit machine). | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | // | 
					
						
							|  |  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  [initial memory usage] | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | //          > Memory Usage: 104 KB | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  [after declaring an array] | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | //          > Memory Usage: 78235 KB | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  [after copying the array] | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | //          > Memory Usage: 156365 KB | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  [inside passArray] | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | //          > Memory Usage: 234495 KB | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  [after slicings] | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | //          > Memory Usage: 234497 KB | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  [inside passSlice] | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | //          > Memory Usage: 234497 KB | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | //  Array's size : 80000000 bytes. | 
					
						
							|  |  |  |  | //  Array2's size: 80000000 bytes. | 
					
						
							|  |  |  |  | //  Slice1's size: 24 bytes. | 
					
						
							|  |  |  |  | //  Slice2's size: 24 bytes. | 
					
						
							|  |  |  |  | //  Slice3's size: 24 bytes. | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | // HINTS | 
					
						
							|  |  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //  I've declared a few functions to help you. | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //    report function: | 
					
						
							|  |  |  |  | //    - Prints the memory usage. | 
					
						
							|  |  |  |  | //    - Just call it with a message that matches to the expected output. | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //    passArray function: | 
					
						
							|  |  |  |  | //    - It automatically prints the memory usage. | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | //    - Accepts a [size]int array, so you can pass your array to it. | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | //    passSlice function: | 
					
						
							|  |  |  |  | //    - It automatically prints the memory usage. | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | //    - Accepts an int slice, so you can pass it one of your slices. | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | // | 
					
						
							|  |  |  |  | // --------------------------------------------------------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | const size = 1e7 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 	// don't worry about this code. | 
					
						
							|  |  |  |  | 	// it stops the garbage collector: prevents cleaning up the memory. | 
					
						
							|  |  |  |  | 	// see the link if you're curious: | 
					
						
							|  |  |  |  | 	// https://en.wikipedia.org/wiki/Garbage_collection_(computer_science) | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 	debug.SetGCPercent(-1) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 	// run the program to see the initial memory usage. | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 	report("initial memory usage") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 1. allocate an array with 10 million int elements | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 	//    the array's size will be equal to ~80MB | 
					
						
							|  |  |  |  | 	//    hint: use the `size` constant above. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 2. print the memory usage (use the report func). | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 3. copy the array to a new array. | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 4. print the memory usage | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | 	// 5. pass the array to the passArray function | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 	// 6. convert one of the arrays to a slice | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 	// 7. slice only the first 1000 elements of the array | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 	// 8. slice only the elements of the array between 1000 and 10000 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 9. print the memory usage (report func) | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | 	// 10. pass the one of the slices to the passSlice function | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 11. print the sizes of the arrays and slices | 
					
						
							|  |  |  |  | 	//     hint: use the unsafe.Sizeof function | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | 	//     see more here: https://golang.org/pkg/unsafe/#Sizeof | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // passes [size]int array — about 80MB! | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | // | 
					
						
							|  |  |  |  | // observe that passing an array to a function (or assigning it to a variable) | 
					
						
							|  |  |  |  | // affects the memory usage dramatically | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | func passArray(items [size]int) { | 
					
						
							|  |  |  |  | 	items[0] = 100 | 
					
						
							|  |  |  |  | 	report("inside passArray") | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // only passes 24-bytes of slice header | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | // | 
					
						
							|  |  |  |  | // observe that passing a slice doesn't affect the memory usage | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | func passSlice(items []int) { | 
					
						
							|  |  |  |  | 	items[0] = 100 | 
					
						
							|  |  |  |  | 	report("inside passSlice") | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:32:48 +03:00
										 |  |  |  | // reports the current memory usage | 
					
						
							|  |  |  |  | // don't worry about this code | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | func report(msg string) { | 
					
						
							|  |  |  |  | 	var m runtime.MemStats | 
					
						
							|  |  |  |  | 	runtime.ReadMemStats(&m) | 
					
						
							| 
									
										
										
										
											2019-02-08 14:10:56 +03:00
										 |  |  |  | 	fmt.Printf("[%s]\n", msg) | 
					
						
							| 
									
										
										
										
											2019-02-08 13:10:55 +03:00
										 |  |  |  | 	fmt.Printf("\t> Memory Usage: %v KB\n", m.Alloc/1024) | 
					
						
							|  |  |  |  | } |