Tips for reducing the size of a Python object

Python Methods and Functions

Let's take a look at some of the ways we can effectively use this memory and reduce the size of objects.

Using built-in dictionaries:

We are all very familiar with data dictionary in Python. It is a way of storing data in the form of keys and values. But when it comes to memory management, vocabulary is not the best. This is actually the worst. Let's see this with an example:

# sys library import

import sys 


Coordinates = { 'x' : 3 , 'y' : 0 , 'z ' : 1 }


print (sys.getsizeof (Coordinates))



We are in we assume that one instance of the data type dictionary occupies 288 bytes. Hence, it will take up enough memory when we have many instances:

So we conclude, that a dictionary is not suitable for working with memory efficient programs.

Using tuples:

Tuples are ideal for storing immutable data values, and are also efficient enough compared to a dictionary to reduce usage memory:

import sys


Coordinates = ( 3 , 0 , 1 )


print (sys.getsizeof (Coordinates))



For For simplicity, we have assumed that the indices 0, 1, 2 represent x, y, z respectively. So from 288 bytes, we have reduced to 72 bytes by simply using a tuple instead of a dictionary. However, this is not very effective. If we have a large number of instances, we still need a lot of memory:

Using the class:

By placing code inside classes, we can significantly reduce memory consumption compared to a dictionary and a tuple.

import sys


class Point:


# definition of coordinate variables

def __ init __ ( self , x, y, z):

  self . x = x

self . y = y

self . z = z


Coordinates = Point ( 3 , 0 , 1 )

print (sys.getsizeof (Coordinates))

Exit :


We see that the same program now requires 56 bytes instead of the previous 72 bytes. The variables x, y and z are 8 bytes each, and the remaining 32 bytes are consumed by internal Python codes. If we have more instances, we have the following distribution — 

So we conclude that classes take precedence over dictionary and tuple when it comes to before saving memory.

Using recordclass:

Recordclass — it is a fairly new Python library. It comes with support for record types that are not built into Python. Since recordclass is a third-party module licensed by MIT, we need to install it first by typing it into the terminal:

 pip install recordclass 

Let's use recordclass to see if it can help further reduce memory footprint.

# import installed library

import sys

from recordclass import recordclass 


Point = recordclass ( ' Point' , ( 'x' , 'y' , 'z' ))


Coordinates = Point ( 3 , 0 , 1 )

print (sys.getsizeof (Coordinates))

Exit :


So using the recordclass further reduced the amount of memory required for a single instance from 56 to 48 bytes. This will be the distribution if we have a large number of instances:

Using dataobjects:

In the previous example, when using recordclass , even garbage values ​​are collected, wasting unnecessary memory. This means that there is still an area of ​​optimization. This is exactly where these objects come into use. The dataobject functionality is part of the recordclass module with a specialization that does not affect garbage values.

import sys

from recordclass import make_dataclass


Position = make_dataclass ( 'Position' , ( 'x' , ' y' , 'z' ))

Coordinates = Position ( 3 < code class = "plain">, 0 , 1 )


print (sys.getsizeof (Coordinates))

Output :


Finally, we see a size reduction from 48 bytes per instance to 40 bytes per instance. Hence, we can see that data objects are the most efficient way to organize our code when it comes to minimum memory usage.