Why the size of object or array always coming same?

I am trying to implement my own Dynamic array data structure using python i did something like below now I want to check the size of my array how could i do it.

import ctypes, sys

class MeraList:
    def __init__(self):
        self.size = 1  # max items that can be stored
        self.present = 0  # number of items present

        # create a c type array with size = self.size
        self.myArray = self.__make_array(self.size)

    def __make_array(self, capacity):
        """It creates a c type static & referential array with size capacity"""
        return (capacity * ctypes.py_object)()

    def __len__(self):
        return self.present

    def append(self, item):
        if self.present == self.size:
            # array is already full need to resize
            new_size = self.size + 16
            newArray = self.__make_array(new_size)
            for i in range(self.present):
                newArray[i] = self.myArray[i]
            self.myArray = newArray
            self.size = new_size

        # append
        self.myArray[self.present] = item
        self.present += 1


    def __resize(self, newCapacity):
        self.myArray = self.myArray[:self.present] + [None] * (newCapacity - self.present)
        self.size = newCapacity
    
    def __str__(self):
        return f"[{', '.join(map(str, self.myArray[:self.present]))}]"



L = MeraList()
for i in range(20):
    L.append(i)
    print(sys.getsizeof(L.myArray), sys.getsizeof(L))

all the outputs are same i.e. 128, 56 what should i do ? to check how much space my array is using. Even I tried to increase the range to 2000 but the answer is same

Did you read the documentation of the function you’re using (sys.getsizeof)? The answer is right there.

No. I don’t know how the sys.getsizeof works internally but I know that it is used to get the memory size of an object.

Well then do read it now…

this is written in the documentation of the sys.getsizeof method

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.

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

If given, default will be returned if the object does not provide means to retrieve the size. Otherwise a TypeError will be raised.

getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

See recursive sizeof recipe for an example of using (I even tried :point_left: but didn’t get anything) getsizeof() recursively to find the size of containers and all their contents.

Do you have any workaround to get the size of object or the array made by it.

I think it is telling you to implement __sizeof__ to return whatever you want it to be.

I’m not familiar with ctypes but it looks like (capacity * ctypes.py_object)() creates an object that you could ask, then add your overhead.

1 Like

Thanks !! I got the solution

The sys.getsizeof() function in Python returns the size of an object in bytes. However, it doesn’t account for the full memory footprint of an object. For instance, when I called sys.getsizeof() on MeraList object, it’s returning the constant size of the ctypes.py_object array reference, not the actual data stored in the array.

To get the actual memory usage of object, I would need to iterate over the elements in the array and sum up their sizes. However, this can be tricky because the size of an object in Python is not always straightforward due to things like shared references.

Here’s the version of __sizeof__ method that calculates the total size of the array plus the size of all the objects stored in the array:

def __sizeof__(self) -> int:
        total = sys.getsizeof(self.myArray)
        for i in range(self.present):
            total += sys.getsizeof(self.myArray[i])
        
        return total
2 Likes

If you’re familiar with C, it’s basically the same idea as sizeof there. If you sizeof something that happens to be a pointer, you get the size of the pointer and not the pointed to object.

In the python case you get the wrapped pyobject size but not the size of the memory pointed to and used by it.

4 Likes