Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to get size of all objects in current namespace?

I have some code that I am running from my own package and the program is using a lot more memory (60GB) than it should be. How can I print the size of all objects (in bytes) in the current namespace in order to attempt to work out where this memory is being used?

I attempted something like

from pympler import asizeof

for objname in dir():
    print(asizeof.asizeof(thing)/1024) # print size in kb

But it doesn't work as it just prints the size of the string containing the name of the object in the namespace. Is there a way to get an object reference to everything in the namespace in order to use this method or is there a better method for working out what is using the memory?

like image 242
SomeRandomPhysicist Avatar asked Dec 18 '22 03:12

SomeRandomPhysicist


2 Answers

dir() returns only the names present in the local scope. Use the locals() function to get the local scope as a dictionary:

for obj in locals().values():
        print(asizeof.asizeof(obj) / 1024)

Note that outside of functions, locals() is the same mapping as globals().

If asizeof() is in the dictionary, you want to filter it out:

for name, obj in locals().items():
    if name != 'asizeof':
        print(asizeof.asizeof(obj) / 1024)

dir() without arguments is functionally equivalent to sorted(locals()) (a sorted list of the keys of the local namespace).

like image 102
Martijn Pieters Avatar answered Dec 20 '22 19:12

Martijn Pieters


If you prefer to use a standard library and also want them sorted by size:

import sys
objects=[]
for name,obj in locals().items():
   objects.append([name,sys.getsizeof(obj)])
sorted(objects,key=lambda x: x[1],reverse=True)
like image 31
otocan Avatar answered Dec 20 '22 19:12

otocan