How to measure the memory cost in Python?
MrJean1 at gmail.com
Fri May 1 21:50:55 CEST 2009
On May 1, 10:56 am, CTO <debat... at gmail.com> wrote:
> > sys.getsizeof() [a suggested solution] isn't platform-specific.
> So, to answer the OP's question, you'd just do something like
> def get_totalsize(obj):
> total_size = sys.getsizeof(obj)
> for value in vars(obj).values():
> try: total_size += get_total_size(value)
> except: total_size += sys.getsizeof(value)
> return totalSize
> def get_current_size(env):
> size = 0
> for value in env.values():
> try: size += get_total_size(value)
> except: pass
> return size
> and discount the weight of the interpreter?
Keep in mind, sys.getsizeof(obj) returns only the size of the given
object. Any referenced objects are not included. You can get the
latter from gc.get_referents(obj).
PS) The asizeof(obj) function from this recipe <http://
code.activestate.com/recipes/546530> does size the object plus its
More information about the Python-list