Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python无限整数_Python_Python 3.x_Memory_Integer_Long Integer - Fatal编程技术网

Python无限整数

Python无限整数,python,python-3.x,memory,integer,long-integer,Python,Python 3.x,Memory,Integer,Long Integer,Python3整数具有。实际上,这受到计算机内存的限制 考虑以下代码: i = 12345 while True: i = i * 123 这显然将失败。但结果会是什么呢?整个RAM(和页面文件)都填充了这个整数(其他进程占用的空间除外) 或者,有没有一种安全措施可以在它发展到这一步之前抓住它?您可以检查发生了什么,而不必冒着填满所有可用内存的风险。你可以: #/usr/bin/env python 导入上下文库 导入资源 @contextlib.contextmanager def

Python3整数具有。实际上,这受到计算机内存的限制

考虑以下代码:

i = 12345
while True:
    i = i * 123
这显然将失败。但结果会是什么呢?整个RAM(和页面文件)都填充了这个整数(其他进程占用的空间除外)


或者,有没有一种安全措施可以在它发展到这一步之前抓住它?

您可以检查发生了什么,而不必冒着填满所有可用内存的风险。你可以:

#/usr/bin/env python
导入上下文库
导入资源
@contextlib.contextmanager
def limit(limit,type=resource.RLIMIT_AS):
软限制,硬限制=resource.getrlimit(类型)
resource.setrlimit(类型,(限制,硬限制))#设置软限制
尝试:
产量
最后:
resource.setrlimit(类型,(软限制,硬限制))#还原

有限制(100*(1)你会碰到内存错误,大部分RAM和页面文件都会被覆盖?这取决于操作系统允许什么,以及
i
一开始是否为零。一个非零的
i
。那么,在典型的Windows系统将进程视为失控并抛出内存错误之前,你认为这会有多远?@coding4fun阅读以下内容:那么,没有sp然后插入pagefile,因为它不是连续的。@coding4fun:python不关心内存来自何处。操作系统是否使用pagefile对python来说是完全透明的。看起来算法太慢,无法填充内存。
#!/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