We all know a very common drawback of Python when compared to programming languages such as C or C++. It is significantly slower and isn’t quite suitable to perform memory-intensive tasks as Python objects consume a lot of memory. This can result in memory problems when dealing with certain tasks. When the RAM becomes overloaded with tasks during execution and programs start freezing or behaving unnaturally, we call it a Memory problem.
Let’s look at some ways in which we can use this memory effectively and reduce the size of objects.
Using built-in Dictionaries:
We all are very familiar with dictionary data type in Python. It’s a way of storing data in form of keys and values. But when it comes to memory management, dictionary isn’t the best. In fact, it’s the worst. Let’s see this with an example:
We see that one instance of the data type dictionary takes 288 bytes. Hence it will consume ample amount of memory when we will have many instances:
So, we conclude that dictionary is not suitable when dealing with memory-efficient programs.
Tuples are perfect for storing immutable data values and is also quite efficient as compared to dictionary in reducing memory usage:
For simplicity, we assumed that the indices 0, 1, 2 represent x, y, z respectively. So from 288 bytes, we came down to 72 bytes by just using tuple instead of dictionary. Still it’s not very efficient. If we have large number of instances, we would still require large memory:
By arranging the code inside classes, we can significantly reduce memory consumption as compared to using dictionary and tuple.
We see that the same program now requires 56 bytes instead of the previous 72 bytes. The variables x, y and z consume 8 bytes each while the rest 32 bytes are consumed by the inner codes of Python. If we have a larger number of instances, we have the following distribution –
So we conclude that classes have an upper-hand than dictionary and tuple when it comes to memory saving.
Side Note : Function sys.getsizeof(object[, default]) specification says: “Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.”
So in your example:
the effective memory usage of object Coordinates is:
= 56 + 28 + 24 + 28 =
Please refer https://docs.python.org/3/library/sys.html.
Recordclass is a fairly new Python library. It comes with the support to record types which isn’t in-built in Python. Since
recordclass is a third-party module licensed by MIT, we need to install it first by typing this into the terminal:
pip install recordclass
recordclass to see if it further helps in reducing memory size.
So the use of
recordclass further reduced the memory required of one instance from 56 bytes to 48 bytes. This will be the distribution if we have large number of instances:
In previous example, while using
recordclass, even the garbage values are collected thus wasting unnecessary memory. This means that there is still a scope of optimization. That’s exactly were dataobjects come in use. The dataobject functionality comes under the recordclass module with a specialty that it does not contribute towards any garbage values.
Finally, we see a size reduction from 48 bytes per instance to 40 bytes per instance. Hence, we see that dataobjects are the most efficient way to organize our code when it comes to least memory utilization.
- How to find size of an object in Python?
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Optimization Tips for Python Code
- Beginner Tips for Learning Python
- reduce() in Python
- 10 Essential Python Tips And Tricks For Programmers
- Python lambda (Anonymous Functions) | filter, map, reduce
- Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
- Python - Read blob object in python using wand library
- Python object
- Python | os.confstr_names object
- Python Object Comparison : "is" vs "=="
- Python | os.supports_dir_fd object
- Python | os.supports_follow_symlinks object
- Python | os.supports_fd object
- Python | os.sysconf_names object
- Python | object() method
- Python | os.supports_bytes_environ object
- Python | os.supports_bytes_environ object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : danieleb89