Skip to content Skip to sidebar Skip to footer

Python Script Knows How Much Memory It's Using

How can a python script know the amount of system memory it's currently using? (assuming a unix-based OS)

Solution 1:

If you want to know the total memory that the interpreter uses, on Linux, read /proc/self/statm.

If you want to find out how much memory your objects use, use Pympler.

Solution 2:

Similar question:

Python memory profiler

Looks like there are memory profilers for python.

PySizer seems popular. Heapy is another.

Google: "python memory profiler" for more.

Solution 3:

I've used once snippet I found on ActiveState and it seemed to work well. Actually it's using same method Martin v. Löwis suggested.

Solution 4:

I don't think there's a simple way to do this. As a practical matter, on a Unix OS I'd probably do something with os.getpid() and calling ps or reading entries in /proc.

Python 2.6 adds sys.getsizeof(), which you could use in concert with gc.get_objects() to walk the size of the working set of objects:

>>>printsum([sys.getsizeof(o) for o in gc.get_objects()])
561616

I don't think that'd be a good idea in practice.

Solution 5:

I haven't used it, but you might take a look at heapy (http://guppy-pe.sourceforge.net/#Heapy), which looks to be a memory profiler for python programs.

Post a Comment for "Python Script Knows How Much Memory It's Using"