Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to measure the memory cost in Python?

0 views
Skip to first unread message

Chris Rebert

unread,
May 1, 2009, 4:42:02 AM5/1/09
to Li Wang, pytho...@python.org
On Fri, May 1, 2009 at 1:03 AM, Li Wang <li.w...@gmail.com> wrote:
> Hi everyone:
>
> I want to measure the actual memory cost of a particular step in my program
> (Python program), does anyone know if there is some function in Python could
> help me to do this job? Or should I seek other tools to help me?

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

CTO

unread,
May 1, 2009, 10:54:39 AM5/1/09
to
Not OP, but I'd actually like to know if there's an answer to this one
that doesn't involve platform-specific tools.

Chris Rebert

unread,
May 1, 2009, 11:04:16 AM5/1/09
to CTO, pytho...@python.org
On Fri, May 1, 2009 at 7:54 AM, CTO <deba...@gmail.com> wrote:
> Not OP, but I'd actually like to know if there's an answer to this one
> that doesn't involve platform-specific tools.

sys.getsizeof() [a suggested solution] isn't platform-specific.

Jean

unread,
May 1, 2009, 12:37:49 PM5/1/09
to
On May 1, 7:54 am, CTO <debat...@gmail.com> wrote:
> Not OP, but I'd actually like to know if there's an answer to this one
> that doesn't involve platform-specific tools.

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

CTO

unread,
May 1, 2009, 1:56:06 PM5/1/09
to
> 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?

Jean

unread,
May 1, 2009, 3:50:55 PM5/1/09
to

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.

Jean

unread,
May 1, 2009, 8:50:37 PM5/1/09
to

Correction, the last sentence should be: The asizeof(obj) ... plus its
referents, recursively.

CTO

unread,
May 2, 2009, 3:30:42 PM5/2/09
to
> > 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?

Aahz

unread,
May 3, 2009, 12:51:13 PM5/3/09
to
In article <7744f434-dd43-4010...@n7g2000prc.googlegroups.com>,

CTO <deba...@gmail.com> wrote:
>
>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

CTO

unread,
May 3, 2009, 1:29:36 PM5/3/09
to
Alright, it's pretty obvious that I have a lot to learn before I'll be
able
to intelligently address this problem, but if somebody could point me
at
something that would help me figure out the terminology at least I'd
really
appreciate it. From what you're saying, it sounds like a combination
of the
above approaches would do what I'm asking- ie, get all the containers,
then
get the contents of each container- but I don't see why that would
work
unless gc tracks some kind of global container for small values,
which, as
I (poorly) understand it, are tracked separately?

Thanks again,
Geremy Condra

0 new messages