Recently I did a simple test in Python, wondering what is the memory impact of huge dictionaries (~ 10 million keys) and how to empty them efficiently (not all keys at once). I use the clear()
method as reference, and I'm looking for the amount of memory still in use after all keys have been removed.
In my tests, the clear()
method is very good at deleting and giving back the memory to the os; whereas when I use del
or pop
the memory consumed after all keys are removed it's still quite large. To measure the memory used by an object I use a function get_size
found online, present at the beginning of my source code (also available here).
How can the clear
method be so efficient compared to pop
or del
?
Code of my tests can be found here as well as the tests result.