New submission from Tim Peters <[email protected]>:
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 ;-)
----------
components: Interpreter Core
messages: 414942
nosy: tim.peters
priority: normal
severity: normal
status: open
title: Surprising list overallocation from .split()
type: behavior
versions: Python 3.11
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue46990>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com