0

I explored and found sys.getsizeof() to get the size of an object, but it is giving the same size for below objects:

import sys
>>> obj = MyModel.objects.first()
>>> sys.getsizeof(obj)
64
>>> obj.description = 'abcdefghi....xyz'  # entered a very long string here
>>> sys.getsizeof(obj)
64

No matter which model objects I use, the size is always 64, even if the object contains too many fields.

What am I doing wrong, and how to get the actual size of an object in bytes?

Edit: Some of you are suggesting this question How do I determine the size of an object in Python?, but it says to use sys.getsizeof() function, which is giving me unexpected result. PS. I am new to django and python

Keshav Bhatiya
  • 295
  • 3
  • 11
  • I saw the getsizeof() function there only, but the results are not as expected – Keshav Bhatiya Aug 20 '20 at 07:59
  • 1
    **`getsizeof()`** return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, ***but this does not have to hold true for third-party extensions as it is implementation-specific***. Which means, the value of `sys.getsizeof(django_model_instance)` need not be true. – JPG Aug 20 '20 at 08:05
  • So, is there any existing function that may help? – Keshav Bhatiya Aug 20 '20 at 08:07
  • Not specific to Django but maybe profilers can help? (https://github.com/pythonprofilers/memory_profiler) – romaingz Aug 20 '20 at 17:34

0 Answers0