I am trying to improve the memory usage of my script in python, therefore I need to know what's RAM usage of my list. I measure the memory usage with
print str(sys.getsizeof(my_list)/1024/1024)
which hopefully would give me the size of the list in RAM in Mb.
it outputs 12 Mb, however in top
command I see that my script uses 70% of RAM of 4G laptop when running.
In addition this list should contain a content from file of ~500Mb.
So 12Mb is unrealistic.
How can I measure the real memory usage?
Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.
Checking Memory Usage in Linux using the GUINavigate to Show Applications. Enter System Monitor in the search bar and access the application. Select the Resources tab. A graphical overview of your memory consumption in real time, including historical information is displayed.
Close Unnecessary Programs and Applications However, the high memory usage problem is mainly due to the overcrowding of many internal processes. Therefore, it helps to stop the unnecessary programs and applications that are running. Open the Task Manager and check any extra programs you aren't using.
sys.getsizeof
only take account of the list itself, not items it contains.
According to sys.getsizeof
documentation:
... Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to. ...
Use Pympler:
>>> import sys
>>> from pympler.asizeof import asizeof
>>>
>>> obj = [1, 2, (3, 4), 'text']
>>> sys.getsizeof(obj)
48
>>> asizeof(obj)
176
Note: The size is in bytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With