5

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?

mcu
  • 3,302
  • 8
  • 38
  • 64

1 Answers1

1

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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • So, no spillage into pagefile then, because it would not be continuous. – mcu Sep 20 '15 at 21:17
  • @coding4fun: python doesn't care where the memory comes from. Whether OS uses pagefile or not is completely transparent for python. It looks like the algorithm is too slow to fill the memory anyway. – jfs Sep 20 '15 at 21:41