ACTUALIZACIÓN: Aquí es another, tal vez la receta más completa para estimar el tamaño de un objeto Python.
Aquí es una thread hacer frente a una pregunta similar
La solución propuesta es escribir su propia ... usando algunas estimaciones del tamaño conocido de primitivas, gastos generales objeto de Python, y los tamaños de construido en los tipos de contenedores.
Dado que el código no es tan largo, aquí es una copia directa de la misma:
def sizeof(obj):
"""APPROXIMATE memory taken by some Python objects in
the current 32-bit CPython implementation.
Excludes the space used by items in containers; does not
take into account overhead of memory allocation from the
operating system, or over-allocation by lists and dicts.
"""
T = type(obj)
if T is int:
kind = "fixed"
container = False
size = 4
elif T is list or T is tuple:
kind = "variable"
container = True
size = 4*len(obj)
elif T is dict:
kind = "variable"
container = True
size = 144
if len(obj) > 8:
size += 12*(len(obj)-8)
elif T is str:
kind = "variable"
container = False
size = len(obj) + 1
else:
raise TypeError("don't know about this kind of object")
if kind == "fixed":
overhead = 8
else: # "variable"
overhead = 12
if container:
garbage_collector = 8
else:
garbage_collector = 0
malloc = 8 # in most cases
size = size + overhead + garbage_collector + malloc
# Round to nearest multiple of 8 bytes
x = size % 8
if x != 0:
size += 8-x
size = (size + 8)
return size
Duplicar : http://stackoverflow.com/questions/33978/find-out-how-much-memory-is-being-used-by-an-object-in-python, http://stackoverflow.com/questions/512893/memory-use-in-large-data-structures-manipulation-processing –
También relacionado: http://stackoverflow.com/questions/13566 4/how-many-bytes-per-element-are-there-in-a-python-list-tuple/159844 – Constantin
Lo siento. Solo quiero preguntar si hay alguna implementación de estas características en ipython, o módulo para eso agregando la función "mágica" en ipython (ya que lo uso para probar mucho). – Ross