How to measure the memory cost in Python?

CTO debatem1 at gmail.com
Fri May 1 13:56:06 EDT 2009


> 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

get_current_size(vars())

and discount the weight of the interpreter?



More information about the Python-list mailing list