Embedding Python: estimate size of dict/list

Discussion in 'Python' started by Chris Angelico, Mar 29, 2011.

  1. Hi! New to the list, apologies if I'm in breach of protocol.

    I have an application that embeds Python to allow third-party
    scripting. The Python code returns data to the application in the form
    of a list or dictionary, and I'd like to have a quick check on the
    size of the outputted object before my code goes too deep into
    processing it.

    Obviously I could use PyObject_Str() and PyString_Size() to get a
    decent figure, but that seems like overkill.

    What I'm hoping for is some simple function that zips through a
    complex object and sums its approximate memory usage. Is there one?

    It doesn't have to be 100% precise; I just want to distinguish between
    the normal (under a KB, or maybe a couple KB at most) and the
    ridiculous (megs and megs of junk that I'd have to process and send
    over the network only for it to be discarded at the other end). If
    there's some sort of "if this ran out of references now, how much
    memory would be freed" function, that would be awesome.

    My code is in C++, is embedding Python 2.6.6, and is running on Ubuntu
    Linux. If the solution is to move to Python 3.x then this may be an
    option.

    Hoping there's something around!

    Chris Angelico
    aka Rosuav
     
    Chris Angelico, Mar 29, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ethan Furman

    Python 3: dict & dict.keys()

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    4
    Views:
    250
    Steven D'Aprano
    Jul 25, 2013
  2. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    108
    Neil Cerutti
    Jul 24, 2013
  3. Oscar Benjamin

    Re: Python 3: dict & dict.keys()

    Oscar Benjamin, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    119
    Oscar Benjamin
    Jul 24, 2013
  4. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    101
    Peter Otten
    Jul 24, 2013
  5. Skip Montanaro

    Re: Python 3: dict & dict.keys()

    Skip Montanaro, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    115
    Skip Montanaro
    Jul 24, 2013
Loading...

Share This Page