进程可能具有软和硬内存限制,如https://manpages.debian.org/buster/manpages-dev/getrlimit.2.en.html 中所述。但是,我不清楚软限制的结果。
答案 0 :(得分:2)
Python 在达到软内存限制时会引发 MemoryError。例如,尝试运行以下命令:
import resource
print('before limit')
print(resource.getrlimit(resource.RLIMIT_AS))
soft_limit = 400000000 # 400 MB
hard_limit = -1 # unlimited
resource.setrlimit(resource.RLIMIT_AS, (soft_limit, hard_limit))
print('after limit')
print(resource.getrlimit(resource.RLIMIT_AS))
print('trying to allocate memory')
x = bytearray(500*1024*1024) # 500 MB
print('ok')
import time;time.sleep(5)
运行它会导致 MemoryError 异常。如果您将第 10 行的 500 更改为 300,它将正常工作。