mcu
mcu

Reputation: 3512

Python Infinite Integers

Python 3 integers have unlimited precision. In practice, this is limited by a computer's memory.

Consider the followng code:

i = 12345
while True:
    i = i * 123

This will obviously fail. But what will be the result of this? The entire RAM (and the page file) filled with this one integer (except for the space occupied by other processes)?

Or is there a safeguard to catch this before it gets that far?

Upvotes: 5

Views: 345

Answers (1)

jfs
jfs

Reputation: 414179

You could check what happens without risking to fill all available memory. You could set the memory limit explicitly:

#!/usr/bin/env python
import contextlib
import resource

@contextlib.contextmanager
def limit(limit, type=resource.RLIMIT_AS):
    soft_limit, hard_limit = resource.getrlimit(type)
    resource.setrlimit(type, (limit, hard_limit)) # set soft limit
    try:
        yield
    finally:
        resource.setrlimit(type, (soft_limit, hard_limit)) # restore

with limit(100 * (1 << 20)): # 100MiB
    # do the thing that might try to consume all memory
    i = 1
    while True:
        i <<= 1

This code consumes 100% CPU (on a single core) and the consumed memory grows very very slowly.

In principle, you should get MemoryError at some point whether it happens before your computer turns to dust is unclear. CPython uses a continuous block of memory to store the digits and therefore you may get the error even if there is RAM available but fragmented.

Your specific code shouldn't trigger it but in general you could also get OverflowError if you try to construct an integer larger than sys.maxsize bytes.

Upvotes: 1

Related Questions