Python Memory Usage using __slots__
Saturday, 28 November 2009
I have been trying to reduce memory usage on our application server, and experimented with using __slots__ to reduce memory usage.
The memory overhead of using __dict__ vs __slots__ turned out to be 147 bytes per object.
Here are my results
| Test | Total Memory | Bytes per object |
|---|---|---|
| slots | 40880K | 41 |
| dicts | 183840K | 188 |
Using the following test script
class C(object):
#__slots__ = ('abc', 'defg')
pass
a = []
for i in xrange(1000000):
c = C()
c.abc = 1
c.defg = 2
a.append(c)
That’s quite promising. Now, if I can make that work with our in-house ORM.
You should follow me on twitter here
No. 1 — November 28th, 2009 at 4:53 am
The big question for me is *how do you measure memory consumption*?
No. 2 — November 28th, 2009 at 8:03 pm
Good old Task Manager on Windows.
No. 3 — November 29th, 2009 at 4:56 am
Python-2.6 has a new method for determining the size of an object; sys.getsizeof()
No. 4 — November 29th, 2009 at 5:49 am
Thanks John. Know of any heap inspectors that doesn’t crash a Python process?