Memory usage per top 10x usage per heapy

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Sep 25 18:55:59 EDT 2012


On 25 September 2012 23:10, Tim Chase <python.list at tim.thechases.com> wrote:

> On 09/25/12 16:17, Oscar Benjamin wrote:
> > I don't know whether it would be better or worse but it might be
> > worth seeing what happens if you replace the FileContext objects
> > with tuples.
>
> If tuples provide a savings but you find them opaque, you might also
> consider named-tuples for clarity.
>

Do they have the same memory usage?

Since tuples don't have a per-instance __dict__, I'd expect them to be a
lot lighter. I'm not sure if I'm interpreting the results below properly
but they seem to suggest that a namedtuple can have a memory consumption
several times larger than an ordinary tuple.

>>> import sys
>>> import collections
>>> A = collections.namedtuple('A', ['x', 'y'])
>>> sys.getsizeof(a)
72
>>> sys.getsizeof(A(1, 2))
72
>>> sys.getsizeof((1, 2))
72
>>> sys.getsizeof(A(1, 2).__dict__)
280
>>> A(1, 2).__dict__
OrderedDict([('x', 1), ('y', 2)])
>>> sys.getsizeof((1, 2).__dict__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute '__dict__'
>>> A(1, 2).__dict__ is A(3, 4).__dict__
False

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120925/78537eeb/attachment.html>


More information about the Python-list mailing list