Search code examples
pythonmemory-managementmemory-leakssizeintrospection

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?


Solution

  • 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).