Embedding Python: estimate size of dict/list

C

Chris Angelico

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top