sizeof in python

This post explains about size of in python. Size in python means the amount of memory of an object it measures the memory of an object, generally measured in bytes.

Introduction

The size represent memory management( Python Memory Management ) in python. In python size refers to the amount of memory that an object consumes it is a measure of the memory footprint of an object. This __size of__ function does not actually tells about size of object it tells us about internal memory size given to object as it should occupy internal memory in python.

_size of_ function in python

In python, we can not tell how much size is require to generator object but we can tell how internal size of object.

  • The size is only expressed in bytes
  • Like, size of(int) is 4 bytes

Suppose there is an array which consists of large number of elements, which is difficult to find its size.

To help this out we use _size of_ function.

let’s see an example

#to find internal size
list=[]
print("Internal memory of list:",list.__sizeof__())
a=[24]
b=[1,2,3]
print("memory size of a",a.__sizeof__())
print("memory size of b",b.__sizeof__())
  • Output
Internal memory of list: 40
memory size of a 48
memory size of b 72

In the above code by using the __size of__  function we can tell the internal memory size of list.

The same goes with array and any other object.

If the object is empty it tells its initial size to enter element in to object.

one more example

my_list = [1, 2, 3, 4, 5]
size_of_list = my_list.__sizeof__()
print(size_of_list)
  • Output:
88

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top