See Gabriel's answer in your other thread "Measure the memory cost in Python".
And please don't post duplicate threads asking essentially the same
question, especially when you were given a perfectly acceptable answer
and not that much time has past since you started the other thread.
Cheers,
Chris
--
http://blog.rebertia.com
sys.getsizeof() [a suggested solution] isn't platform-specific.
Depending on what you need and the O/S you are using, this recipe may
help
<http://code.activestate.com/recipes/286222/>
That recipe also appeared in the 2nd edition of the Python Cookbook,
see
<http://books.google.com/books?
id=Q0s6Vgb98CQC&printsec=frontcover&dq=editions:ISBN0596001673#PPA334,M1>
/Jean Brouwers
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?
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).
/Jean Brouwers
PS) The asizeof(obj) function from this recipe <http://
code.activestate.com/recipes/546530> does size the object plus its
references, recursively.
Correction, the last sentence should be: The asizeof(obj) ... plus its
referents, recursively.
I will admit, I have *no idea* what that code is doing, but in looking
through the gc module documentation, I'm seeing the gc.get_objects
function. Would it be equivalent to what the OP is asking to track the
size of every element returned by that?
Perhaps. gc.get_objects() only returns objects tracked by GC (i.e.
containers). You would need to also check all the object references
held by the containers.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
"Typing is cheap. Thinking is expensive." --Roy Smith
Thanks again,
Geremy Condra