简体   繁体   中英

Python - Find current objects in memory

Is there a way to find the objects that are currently in memory including their names, where they are located and module names etc?

My process Python.exe before the main() method in Task Manager has a memory footprint of 15MB.

After the main method completes its first iteration, the process Python.exe memory size is 250MB.

I want to understand which objects are still in memory so that I can delete them

while True:
 # print current object details
 main() 
 # print current object details

No. There is no way to find all objects in Python. Also, most objects don't have names, and object "locations" don't work the way it sounds like you think they work.

The closest thing to what you're looking for is gc.get_objects , which returns a list of all GC-tracked objects. This is not a list of all objects, and it doesn't tell you why objects are still alive. You can get an object's GC-tracked referrers with gc.get_referrers , but not all references are known to the GC.

Even if you make sure objects you no longer need are unreachable, and even if their memory gets reclaimed, that still doesn't mean Python will actually return memory to the OS. Your memory usage might still be 250 MB at the end of all this.

Getting currently loaded variables

The function dir() will list all loaded environment variables, such as:

a = 2
b = 3
c = 4
print(dir())

will return

['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c']

Find below what the documentation of dir says:

dir(...) dir([object]) -> list of strings

If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. If the object supplies a method named __dir__, it will be used; otherwise the default dir() logic is used and returns: for a module object: the module's attributes. for a class object: its attributes, and recursively the attributes of its bases. for any other object: its attributes, its class's attributes, and recursively the attributes of its class's base classes.

Getting variable methods and attributes

You can also use dir() to list the methods and attributes associated with an object, for that you shall use: dir(<name of object>)

Getting size of variables currently loaded

If you wish to evaluate the size of your loaded variables/objects you can use sys.getsizeof() , as such:

sys.getsizef(a)
sys.getsizof(<name of variable>)

sys.getsizeof() gets you the size of an object in bytes (see this post for more on it)

Wrapping up

You could combine this functionality in some sort of loop like such

import sys
a =2
b = 3
c = 4
d = 'John'
e = {'Name': 'Matt', 'Age': 32}

for var in dir():
    print(var, type(eval(var)), eval(var), sys.getsizeof(eval(var)))

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM