ashim ashim - 1 year ago 232
Python Question

python. get size of an object

I need to know size of objects in my python program.
I have

for
loop in which I need to know size of objects.
If I use
sys.getsizeof
then memory does not free instantly
and I cannot see actual size. Is there way to do it?

My objects are json and string. In the worst case I can
save them to file and look at file sizes. But how can I look
at file size from python code?

Edit: size of serialised objects is more important for me.
so second part of the question is essential.

Thanks.

Answer Source

Considering that type(json.dumps(something))==str you should be able to literally just use len.

Consider the following

obj = {'content' : 'something goes here'}
json_obj = json.dumps(obj)
json_size = len(json_obj)

serialized_size = len(serialized_object)

if json_size < serialized_size:
  print "I'd use the JSON with this..."
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download