python. get size of an object

python

Solution

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..."

Problem

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.

Original source