Message414942
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 ;-) |
|
| Date |
User |
Action |
Args |
| 2022-03-11 22:37:20 | tim.peters | set | recipients:
+ tim.peters |
| 2022-03-11 22:37:20 | tim.peters | set | messageid: <1647038240.05.0.997478605012.issue46990@roundup.psfhosted.org> |
| 2022-03-11 22:37:20 | tim.peters | link | issue46990 messages |
| 2022-03-11 22:37:19 | tim.peters | create | |
|