o.o
o.o

Reputation: 3751

Pympler get memory usage of object

I need help using Pympler and getting the memory usage of an object. I am using it like this:

from pympler import classtracker

myObj = SomeClass()
tr = classtracker.ClassTracker()
tr.track_object(myObj)

# Do stuff with object here

# Print stats
tr.create_snapshot()
tr.stats.print_summary()

This works fine and I get these results:

---- SUMMARY ------------------------------------------------------------------
                                         active      0     B      average   pct
  SomeClass                                   1    136.45 KB    136.45 KB    0%
-------------------------------------------------------------------------------

My question is how do I get the average number and put it in a variable? I can't find anything in the documentation. I'm running some tests and want to get the average over the lifetime of the tests. If you know of a way to do that or if there is any other package I can use to accomplish what I want, please let me know. Thank you.

Upvotes: 0

Views: 880

Answers (1)

Mark Omo
Mark Omo

Reputation: 990

You can do it with the asizeof function, you don't even need to use classtracker (unless you are using some of the more complex functionality)

from pympler.asizeof import asizeof

class SomeClass:
    def __init__(self):
        self.list = []
    def append(self, i):
        self.list.append(i)

myObj = SomeClass()

myObj.append("Hello")
myObj.append("World")

print 'Sizeof myObj is {}'.format(asizeof(myObj))

Upvotes: 1

Related Questions