我试图使用setrlimit
来限制我在Linux系统上的内存使用,以防止进程崩溃(我的代码在高性能集群上崩溃节点,因为一个bug导致内存消耗超过100gib)。我似乎找不到要传递给setrlimit
的正确资源;我认为它应该是resident,即cannot be limited with setrlimit,但是我被resident、heap、stack搞糊涂了。在下面的代码中;如果我只取消了RLIMIT_AS
的注释,那么即使该数组应该只有80MB,代码也会在MemoryError
处失败。如果我只取消注释RLIMIT_DATA
、RLIMIT_RSS
或{
我想让我的程序失败(无论如何),只要它试图分配太多的内存。为什么RLIMIT_DATA
、RLIMIT_RSS
、RLIMIT_STACK
和{setrlimit
的正确资源是什么?在
$ cat mwe.py
#!/usr/bin/env python3.5
import resource
import numpy
#rsrc = resource.RLIMIT_AS
#rsrc = resource.RLIMIT_DATA
#rsrc = resource.RLIMIT_RSS
#rsrc = resource.RLIMIT_STACK
soft, hard = resource.getrlimit(rsrc)
print("Limit starts as:", soft, hard)
resource.setrlimit(rsrc, (1e9, 1e9))
soft, hard = resource.getrlimit(rsrc)
print("Limit is now:", soft, hard)
print("Allocating 80 KB, should certainly work")
M1 = numpy.arange(100*100, dtype="u8")
print("Allocating 80 MB, should work")
M2 = numpy.arange(1000*1000*10, dtype="u8")
print("Allocating 2 GB, should fail")
M3 = numpy.arange(1000*1000*250, dtype="u8")
input("Still here…")
未注释RLIMIT_AS
行的输出:
与其他未注释的任何一个一起运行时的输出:
$ ./mwe.py
Limit starts as: -1 -1
Limit is now: 1000000000 -1
Allocating 80 KB, should certainly work
Allocating 80 MB, should work
Allocating 2 GB, should fail
Still here…
在最后一行,top
报告我的进程正在使用379gbvirt,2.0gbres
系统详细信息:
$ uname -a
Linux host.somewhere.ac.uk 2.6.32-573.3.1.el6.x86_64 #1 SMP Mon Aug 10 09:44:54 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.7 (Santiago)
$ free -h
total used free shared buffers cached
Mem: 2.0T 1.9T 37G 1.6G 3.4G 1.8T
-/+ buffers/cache: 88G 1.9T
Swap: 464G 4.8M 464G
$ python3.5 --version
Python 3.5.0
$ python3.5 -c "import numpy; print(numpy.__version__)"
1.11.1
唉,你的问题我没有答案。但我希望以下几点可能会有所帮助:
RLIMIT_AS
。如前所述,here这应该限制进程使用的整个虚拟内存。虚拟内存包括:交换内存、共享库、代码和数据。更多详细信息here。在您可以在脚本中添加以下函数(从this answer)来检查实际的虚拟内存使用情况:
相关问题 更多 >
编程相关推荐