This shows you the differences between two versions of the page.
ep:labs:02:contents:tasks:ex1 [2021/10/11 21:29] gheorghe.petre2608 [01. [10p] Memory usage] |
ep:labs:02:contents:tasks:ex1 [2025/02/11 23:22] (current) cezar.craciunoiu created |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 01. [10p] Memory usage ==== | + | ==== Google Colab Notebook ==== |
- | Open {{:ep:labs:ep2017_l2_ex02.txt|ex01.py}} and take a look at the code. What is the difference between the methods //do_append()// and //do_allocate()//? | ||
- | Use //vmstat// to monitor the memory usage while performing the following experiments. In the main method, call: | + | For this lab, we will use Google Colab for exploring pandas and seaborn. Please solve your tasks [[https://github.com/cosmaadrian/ml-environment/blob/master/EP_Plotting_II.ipynb|here]] by clicking "**Open in Colaboratory**". |
- | * The //do_append// method on it's own (see //Experiment 1//). | + | |
- | * The //do_allocate// method on it's own (see //Experiment 2//). | + | |
- | * Both methods as shown in the //Experiment 3// area in the code. | + | |
- | * Both methods as shown in the //Experiment 4// area in the code. | + | |
- | Offer an interpretation for the obtained results and change the code in //do_append()// so that the memory usage will be the same when performing experiment 3 or experiment 4. \\ | + | You can then export this python notebook as a PDF (**File -> Print**) and upload it to **Moodle**. |
- | + | ||
- | HINTS: Python GC; Reference cycle | + | |
- | + | ||
- | <solution -hidden> | + | |
- | * **Experiment 1,2:** There is not much difference in terms of memory usage between Experiment 1 and 2. | + | |
- | * **Experiment 3:** The //do_append// method starts using memory. After it finished running, since there is no reference for the generated array, the allocated memory is unallocated (reference counting garbage collector). Then the next method, //do_allocate//, starts running and using memory. | + | |
- | * **Experiment 4:** After the first method ends, since in this case there is a reference for the generated array, the memory is not unallocated. When the second method starts running, the amount of memory that it uses is added to the amount already used by the first method. If the used memory is taking up almost all the available free memory of the system, you can notice: swpd increasing, buff decreasing, cache decreasing, and si, so, bi, bo activity. | + | |
- | + | ||
- | Notice the increased number of interrupts and context switches. These are caused by time.sleep(). | + | |
- | + | ||
- | </solution> | + | |
- | + | ||
- | <solution -hidden> | + | |
- | <code python> | + | |
- | def do_append(size): | + | |
- | result = [] | + | |
- | result.append(result) | + | |
- | for i in range(size): | + | |
- | message= "some unique object %d" % ( i, ) * 1000 | + | |
- | result.append(message) | + | |
- | time.sleep(0.0001) | + | |
- | return result | + | |
- | </code> | + | |
- | </solution> | + |