Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asizeof() gives incorrect values for the memory consumption of named tuples. #10

Closed
ceridwen opened this issue Aug 3, 2014 · 1 comment

Comments

@ceridwen
Copy link

ceridwen commented Aug 3, 2014

From the documentation for the collections module:

"Named tuple instances do not have per-instance dictionaries, so they are lightweight and require no more memory than regular tuples."

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pympler.asizeof
>>> import collections
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22)
>>> p
Point(x=11, y=22)
>>> pympler.asizeof.asizeof(p)
1440
>>> t = 11, 22
>>> t
(11, 22)
>>> pympler.asizeof.asizeof(t)
128

I haven't been able to figure out the reason for the discrepancy.

@Pankrat
Copy link
Member

Pankrat commented Aug 6, 2014

I believe Pympler assumes that the __dict__ property is part of the object and includes it in the given size:

>>> print(asizeof.asized(p, detail=2).format())
Point(x=11, y=22) size=728 flat=32
    __dict__ size=672 flat=576
        [K] x size=32 flat=32
        [K] y size=32 flat=32
        [V] x: 11 size=16 flat=16
        [V] y: 22 size=16 flat=16
    __slots__ size=24 flat=24
    __class__ size=0 flat=0
>>> p.__dict__
OrderedDict([('x', 11), ('y', 22)])

I'd have to check the namedtuple implementation in Python but it really sounds like a bug in Pympler's recursive sizing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants