linux setrlimit 内存,如何使用“setrlimit”限制内存使用?RLIMIT\u AS杀死得太快;RLIMIT\u DATA,RLIMIT\u RSS,RLIMIT\u STACK ...

我试图使用setrlimit来限制我在Linux系统上的内存使用,以防止进程崩溃(我的代码在高性能集群上崩溃节点,因为一个bug导致内存消耗超过100gib)。我似乎找不到要传递给setrlimit的正确资源;我认为它应该是resident,即cannot be limited with setrlimit,但是我被resident、heap、stack搞糊涂了。在下面的代码中;如果我只取消了RLIMIT_AS的注释,那么即使该数组应该只有80MB,代码也会在MemoryError处失败。如果我只取消注释RLIMIT_DATA、RLIMIT_RSS或{}两个数组都会成功分配,即使总内存使用量是2GB,或者是所需最大值的两倍。在

我想让我的程序失败(无论如何),只要它试图分配太多的内存。为什么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行的输出:

^{pr2}$

与其他未注释的任何一个一起运行时的输出:$ ./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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值