I used RLIMIT_RSS to limit my program's RES, but it didn't work

399 Views Asked by At

When I want to use struct rlimit to limit my program's memory, but RLIMIT_RSS didn't work. I set my RSS limit is 100 MiB.But when my test program used about 700 MiB, it still continue;

This is my Code.

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;
}

Limitlist is this.

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;

This is my test Code.

#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;
}

And this is my test program's proc status file.

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

You can see that VmRSS had been about 700 MiB, but it isn't killed.

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

Thanks in advance.

0

There are 0 best solutions below