memory – In Python, what is `sys.maxsize`?
memory – In Python, what is `sys.maxsize`?
Python can handle arbitrarily large integers in computation. Any integer too big to fit in 64 bits (or whatever the underlying hardware limit is) is handled in software. For that reason, Python 3 doesnt have a sys.maxint
constant.
The value sys.maxsize
, on the other hand, reports the platforms pointer size, and that limits the size of Pythons data structures such as strings and lists.
Documentation for sys.maxsize:
An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 – 1 on a 32-bit platform and 2**63 – 1 on a 64-bit platform. python3
The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have. python2
What is Py_ssize_t
?
It is an index type (number type for indexing things, like lists). It is the signed version of size_t (from the C language).
- We dont use a normal number/ int, because this is unbounded in Python.
- In Python, we dont use
size_t
because we want to support negative indexing, in Python we can domy_list[-4:]
. SoPy_ssize_t
provides negative and positive numbers between a range. - The
_t
stands for type, to inform developers thatsize_t
is a type name, not a variable. Just a convention.
So what is the effect of having a limit on Py_ssize_t
? Why does this limit list, strings, dict size?
- There is no way to index a list with an element larger than this. The list cannot get bigger than this, because it wont accept a non
Py_ssize_t
. - In the dictionary case,
Py_ssize_t
is used as the hash. Python doesnt use linked lists in its dictionary implementation, it uses Open Addressing/ probing, where if a collision is found, we a systematic way of getting another place to find the key and put the value. So you cant have more thanPy_ssize_t
in a dictionary in Python.
In all practical cases (64 bit machines aka. probably you), you will run out of memory before you max out Py_ssize_t
. Trying dict.fromkeys(range(sys.maxsize + 5))
never got there, it just slowed my computer down.