• 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
    ",maximum*1.0 / 1024.0 / 1024.0 / 1024.0);
        printf("the address is %x
    ",tmpblock); 
        printf("the address end is %x
    ", 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的方式进行, 这一点还不熟,希望以后可以有机会用到

  • 相关阅读:
    servlet规范核心类图
    策略模式
    掌握Spark机器学习库-07-回归算法原理
    掌握Spark机器学习库-07-线性回归算法概述
    掌握Spark机器学习库-07-回归分析概述
    掌握Spark机器学习库-06-基础统计部分
    掌握Spark机器学习库-05-spark中矩阵与向量的使用
    测试开发要懂的设计模式知识
    数据可视化工具-ECharts
    掌握Spark机器学习库-02-mllib数据格式
  • 原文地址:https://www.cnblogs.com/mikeguan/p/7109348.html
Copyright © 2020-2023  润新知