Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List memory usage

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?

like image 346
user16168 Avatar asked Dec 25 '13 09:12

user16168


People also ask

How can I see memory usage history?

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.

How do I see memory usage on Linux?

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.

What causes 100% memory usage?

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.


1 Answers

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.

like image 189
falsetru Avatar answered Oct 04 '22 17:10

falsetru