How to save memory in python code

I just experienced a memory leakage in python code and discovered that the memory is not released after a function finished. An example of the problem I show below. There is just a class that save a long list of image and a function that print the len of this list.


Even when I delete all the variable related, the memory usage is still high

I found a way that overcome this problem is by running the function in a process.
Screenshot_2020-03-08 multiprocess_vs_function - Jupyter Notebook(1)

The problem is not related to jupyter because when I ran it in pure python code, the memory shown is the same.


Do we have an explanation of that and what is the proper way you guys used when facing the memory leakage ?

Thank you