This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author tim.peters
Recipients tim.peters
Date 2022-03-11.22:37:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1647038240.05.0.997478605012.issue46990@roundup.psfhosted.org>
In-reply-to
Content
When looking into a StackOverflow question about surprisingly high memory use, I stumbled into this (under 3.10.1, Win64):

>>> import sys
>>> s = "1 2 3 4 5".split()
>>> s
['1', '2', '3', '4', '5']
>>> sys.getsizeof(s)
152
>>> _ - sys.getsizeof([])
96
>>> 96 / 8
12.0

That is, we allocated enough space in the list to store 12(!) elements, despite that only 5 are used. Other ways of building a 5-element list I've tried overallocate by at most 3 slots:

>>> sys.getsizeof([ch for ch in "12345"])
120
>>> sys.getsizeof([1, 2, 3, 4, 5])
120

(and 120 - 56 = 64, room for 8 pointers)

Then there's this curiosity, which allocates space for exactly the 5 needed:

>>> sys.getsizeof(list(tuple("1 2 3 4 5".split())))
96

(and 96 - 56 = 40, room for the 5 pointers needed)

I don't expect this to be consistent, but allocating space for 12 when only 5 are needed is unreasonable. Even allocating space for 8 is pushing it ;-)
History
Date User Action Args
2022-03-11 22:37:20tim.peterssetrecipients: + tim.peters
2022-03-11 22:37:20tim.peterssetmessageid: <1647038240.05.0.997478605012.issue46990@roundup.psfhosted.org>
2022-03-11 22:37:20tim.peterslinkissue46990 messages
2022-03-11 22:37:19tim.peterscreate