Consume 50% Less Memory with Your Python Objects
Python, Performance, Memory Optimization
By default, in Python, objects have a __dict__ attribute. It's a dictionary used to access the object variables per key.
It is useful to allow dynamic variable creation.
However, this flexibility can lead to creation of new object variables when misspelled. Python will simply create a new variable with the given name.
Enter __slots__
With slots, we can specifically declare data variables. Then Python will allocate space for them in memory and skip the creation of the __dict__ attribute.
It also forbids the creation of any object variable which is not declared in the __slots__ attribute.
By using slots, you decrease the memory used by your class instances.
Slots can also be used in dataclasses if you're using Python 3.10 or higher. Simply add slots=True to the decorator.
It makes a huge difference if you're creating lots of objects.
Example
from dataclasses import dataclass
# https://pypi.org/project/Pympler/
from pympler.asizeof import asizeof
# Dataclass with slots
@dataclass(frozen=True, slots=True)
class SmallObjectWithDataclass:
first_name: str
last_name: str
# Class with slots
class SmallObject:
__slots__ = ["first_name", "last_name"]
def __init__(self, first_name, last_name) -> None:
self.first_name: str = first_name
self.last_name: str = last_name
# Class with no slots
class BiggerObject:
def __init__(self, first_name, last_name) -> None:
self.first_name: str = first_name
self.last_name: str = last_name
p = SmallObjectWithDataclass("Jerome", "K")
print(asizeof(p)) # Output: 160 Bytes
p2 = SmallObject("Jerome", "K")
print(asizeof(p2)) # Output: 160 Bytes
p3 = BiggerObject("Jerome", "K")
print(asizeof(p3)) # Output: 392 Bytes
Both slotted versions use 160 bytes while the regular class uses 392 bytes — that's roughly 59% less memory just by declaring your attributes upfront.
Hope this helps and have a great day.