linux限制用戶內存使用


最近有內存使用報警的郵件發出,之后殺掉了內存占用高的進程,使內存恢復正常
但是發現某些程序被殺掉了,有過懷疑是被人手動殺掉的,看日志后發現應該是內存占用過大,系統自動殺掉的

內存耗盡會調用oom 對進程進行評估 並選擇一個進程殺死 以釋放內存

dmesg

Jun 26 08:45:47 localhost kernel: [6409835.925696] curl invoked oom-killer: gfp_mask=0x280da, order=0, oom_adj=0, oom_score_adj=0
Jun 26 08:45:48 localhost kernel: [6409835.925705] curl cpuset=/ mems_allowed=0-1
Jun 26 08:45:48 localhost kernel: [6409835.925711] Pid: 3640, comm: curl Not tainted 3.2.0-41-generic #66-Ubuntu
Jun 26 08:45:48 localhost kernel: [6409835.925715] Call Trace:
Jun 26 08:45:48 localhost kernel: [6409835.925729]  [<ffffffff8111c281>] dump_header+0x91/0xe0
Jun 26 08:45:48 localhost kernel: [6409835.925734]  [<ffffffff8111c605>] oom_kill_process+0x85/0xb0
Jun 26 08:45:48 localhost kernel: [6409835.925740]  [<ffffffff8111c9aa>] out_of_memory+0xfa/0x220
...


Jun 26 08:45:48 localhost kernel: [6409836.005515] Out of memory: Kill process 13655 (sh) score 1 or sacrifice child
Jun 26 08:45:48 localhost kernel: [6409836.027998] Killed process 13657 (python) total-vm:1710324kB, anon-rss:2324kB, file-rss:360kB

系統限制用戶內存使用量

cat /etc/security/limits.conf

ubuntu		soft	as	1200000
ubuntu		hard	as	1500000

系統內存情況

free

             total       used       free     shared    buffers     cached
Mem:       1015664     851624     164040        148     216548     378456
-/+ buffers/cache:     256620     759044
Swap:       999996       3344     996652

申請內存的代碼

test.c

#include<stdio.h>                                                                                                                                                                                                                           
#include<stdlib.h>
size_t maximum=0;
int main(int argc,char *argv[])
{
    void * block;
    void * tmpblock;
    size_t blocksize[]={1024*1024, 1024, 1}; 
    int i,count;
    for(i=0;i<3;i++){
        for(count=1;;count++){
            block = malloc(maximum+blocksize[i]*count);
            if(block){
                tmpblock = block;
                maximum += blocksize[i]*count;
                free(block);
            }else{
                break;
            }   
        }   
    }   
    printf("maximum malloc size = %lf GB\n",maximum*1.0 / 1024.0 / 1024.0 / 1024.0);
    printf("the address is %x\n",tmpblock); 
    printf("the address end is %x\n", tmpblock + maximum);
    //while(1);
}

編譯運行后的結果

gcc -o malloc  test.c
./malloc

maximum malloc size = 1.101562 GB
the address is 618c2010
the address end is a80c2010

去掉交換內存后的情況

free

             total       used       free     shared    buffers     cached
Mem:       1015664     856708     158956        384     216580     379532
-/+ buffers/cache:     260596     755068
Swap:            0          0          0

系統內存使用情況

./malloc

maximum malloc size = 0.784881 GB
the address is 3dc48010
the address end is 6fffff56

這個限制是包括交換內存在內的,系統會保留一定的內存供內核使用,用戶空間能夠申請的內存使用量達不到最大值

附注:
限制用戶的內存和cpu使用還可以通過cgroup的方式進行, 這一點還不熟,希望以后可以有機會用到


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM