How Python’s __slots__ Can Slash Memory Usage by Up to 7×
This article explains why Python objects consume more memory than expected, demonstrates how to measure the true size of objects with a recursive get_size function, and shows that adding __slots__ to a class can dramatically reduce memory consumption while slightly improving performance.
Memory shortage is a frequent problem in projects that handle large dynamic lists. The author describes a case where a simple code change solved the issue.
First, a DataItem class is defined:
class DataItem(object):
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = addressUsing sys.getsizeof on an instance returns only 56 bytes, which is misleading because Python stores additional hidden data for each object. For example, an empty string occupies 33 bytes and an integer 24 bytes; an empty dict already uses 272 bytes.
To see the hidden attributes, a dump helper is provided:
def dump(obj):
for attr in dir(obj):
print(" obj.%s = %r" % (attr, getattr(obj, attr)))A recursive get_size function (adapted from goshippo.com) accurately measures the real memory footprint of any Python object:
def get_size(obj, seen=None):
# Recursively finds size of objects
size = sys.getsizeof(obj)
if seen is None:
seen = set()
obj_id = id(obj)
if obj_id in seen:
return 0
seen.add(obj_id)
if isinstance(obj, dict):
size += sum(get_size(v, seen) for v in obj.values())
size += sum(get_size(k, seen) for k in obj.keys())
elif hasattr(obj, '__dict__'):
size += get_size(obj.__dict__, seen)
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
size += sum(get_size(i, seen) for i in obj)
return sizeTesting shows that a simple DataItem instance occupies about 460 bytes, and a slightly larger one 484 bytes. Placing objects in a list yields sizes that reflect shared references, e.g., get_size([d1]) → 532 bytes, get_size([d1, d2]) → 863 bytes, and get_size([d1, d2, d1]) → 871 bytes.
To reduce memory consumption, the class can be rewritten with __slots__:
class DataItem(object):
__slots__ = ['name', 'age', 'address']
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = addressWith slots, the same instance size drops to roughly 64 bytes (about a 7× reduction) and object creation becomes ~20 % faster.
A stress test creating 100 000 DataItem objects shows total memory of 16.8 MB without slots versus 6.9 MB with slots.
Slots have drawbacks: they remove the instance __dict__, so generic JSON serialization via json.dumps(self.__dict__) no longer works. A custom serializer that iterates over __slots__ can be used instead.
def toJSON(self):
data = {}
for var in self.__slots__:
data[var] = getattr(self, var)
return json.dumps(data)Adding new attributes at runtime is also impossible, but this limitation was acceptable for the author’s project.
In conclusion, Python’s flexibility comes with memory overhead, but using __slots__ or external libraries like NumPy can dramatically reduce that overhead when performance and efficiency are critical.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
