0

当我想使用 struct rlimit 来限制我的程序的内存,但 RLIMIT_RSS 不起作用。我将我的 RSS 限制设置为 100 MiB。但是当我的测试程序使用大约 700 MiB 时,它仍然继续;

这是我的代码。

int load_limit_config(const Limitlist *CFG) { 
    struct rlimit rtmp;
    /* set time limit */
    rtmp.rlim_max = TIMETOP;
    rtmp.rlim_cur = min(TIMETOP, 
            (unsigned long int)(CFG->lim_time*1.3)/1000+1);
    if(setrlimit(RLIMIT_CPU, &rtmp) < 0) { 
        REPORTER2("SET CPU TIME FAILED.", stderr);
        return -1;
    }
    /* set virtual memory limit */
    rtmp.rlim_max = MEMORYTOP;
    rtmp.rlim_cur = min(MEMORYTOP, CFG->lim_memory * 1024 * 1024);
    if(setrlimit(RLIMIT_AS, &rtmp) < 0) { 
        REPORTER2("SET AS LIMIT FAILED.", stderr);
        return -1;
    }
    /*set real memory limit */
    rtmp.rlim_max = min(MEMORYTOP, CFG->lim_memory * 1024 * 1024);
    rtmp.rlim_cur = rtmp.rlim_max;
    if(setrlimit(RLIMIT_RSS, &rtmp) < 0) { 
        REPORTER2("SET REAL MEMORY LIMIT FAILED.", stderr);
        return -1;
    }
    /* set stack limit */
    rtmp.rlim_max = MEMORYTOP;
    rtmp.rlim_cur = min(MEMORYTOP, CFG->lim_memory * 1024 * 1024);
    if(setrlimit(RLIMIT_STACK, &rtmp) < 0) { 
        REPORTER2("SET STACK LIMIT FAILED.", stderr);
        return -1;
    }
    /* set output limit */
    rtmp.rlim_max = FILESIZETOP;
    rtmp.rlim_cur = min(FILESIZETOP, CFG->lim_memory * 1024 * 1024);
    if(setrlimit(RLIMIT_FSIZE, &rtmp) < 0) { 
        REPORTER2("SET FILE LIMIT FAILED.", stderr);
        return -1;
    }
    /* set proc limit */
    rtmp.rlim_max = PROCTOP;
    rtmp.rlim_cur = min(PROCTOP, CFG->lim_proc);
    if(setrlimit(RLIMIT_NPROC, &rtmp) < 0) { 
        REPORTER2("SET PROC LIMIT FAILED.", stderr);
        return -1;
    }
    return 0;
}

限制清单是这个。

typedef struct Limitlist{ 
    unsigned long int lim_time;     /* MS */
    unsigned long int lim_memory;   /* MiB */
    unsigned long int lim_output;   /* MiB */
    unsigned long int lim_proc;     /* NUM */
}Limitlist;

这是我的测试代码。

#include <unistd.h>
#include <stdio.h>

#define maxn (200000000)

int s[maxn], i = maxn;

int main() { 
    while(i--) s[i] = i;
    printf("Run Complete\n\n");
    return 0;
}

这是我的测试程序的 proc 状态文件。

Name:   test.run
State:  S (sleeping)
Tgid:   6821
Ngid:   0
Pid:    6821
PPid:   3062
TracerPid:  0
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
FDSize: 256
Groups: 4 24 27 30 46 110 115 116 1000 
NStgid: 6821
NSpid:  6821
NSpgid: 6821
NSsid:  3027
VmPeak:   785608 kB
VmSize:   785608 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:    782160 kB
VmRSS:    782160 kB
VmData:   781440 kB
VmStk:       132 kB
VmExe:         4 kB
VmLib:      1944 kB
VmPTE:      1556 kB
VmPMD:        12 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
Threads:    1
SigQ:   1/7807
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
Seccomp:    0
Cpus_allowed:   ffffffff,ffffffff
Cpus_allowed_list:  0-63
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    4
nonvoluntary_ctxt_switches: 45

您可以看到 VmRSS 大约有 700 MiB,但它并没有被杀死。

我在网上找了很久。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。

提前致谢。

4

0 回答 0