Let's take a look at some of the ways we can effectively use this memory and reduce the size of objects.
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:
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.
Tuples are ideal for storing immutable data values, and are also efficient enough compared to a dictionary to reduce usage memory:
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:
By placing code inside classes, we can significantly reduce memory consumption compared to a dictionary and a tuple.
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.
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
recordclass to see if it can help further reduce memory footprint.
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:
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.
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.