最近有内存使用报警的邮件发出,之后杀掉了内存占用高的进程,使内存恢复正常

但是发现某些程序被杀掉了,有过怀疑是被人手动杀掉的,看日志后发现应该是内存占用过大,系统自动杀掉的

内存耗尽会调用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的方式进行, 这一点还不熟,希望以后可以有机会用到

linux限制用户内存使用的更多相关文章

  1. linux进程用户内存空间和内核空间

    When a process running in user mode requests additional memory, pages are allocated from the list of ...

  2. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  3. (转载) Linux IO模式及 select、poll、epoll详解

    注:本文是对众多博客的学习和总结,可能存在理解错误.请带着怀疑的眼光,同时如果有错误希望能指出. 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案 ...

  4. Linux安装详情图解

    本文讲解Linux的安装 因为是纯属学习使用,所以安装在了虚拟机里   需要软件: VirtualBox-5.1.10 ubuntu-16.04.1-desktop-amd64 说明: 虚拟机可以选择 ...

  5. 基于Linux平台的libpcap源码分析和优化

    目录 1..... libpcap简介... 1 2..... libpcap捕包过程... 2 2.1        数据包基本捕包流程... 2 2.2        libpcap捕包过程... ...

  6. linux 内核与用户空间通信之netlink使用方法

    转自:http://blog.csdn.net/haomcu/article/details/7371835 Linux中的进程间通信机制源自于Unix平台上的进程通信机制.Unix的两大分支AT&a ...

  7. 【转】 linux内存管理

    一 为什么需要使用虚拟内存 大家都知道,进程需要使用的代码和数据都放在内存中,比放在外存中要快很多.问题是内存空间太小了,不能满足进程的需求,而且现在都是多进程,情况更加糟糕.所以提出了虚拟内存,使得 ...

  8. Linux IO漫谈

    本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background IO可能是我们接触最频繁的系统调用,比如printf到终端,send content到对端, ...

  9. Linux下的C编程实战

    Linux下的C编程实战(一) ――开发平台搭建 1.引言 Linux操作系统在服务器领域的应用和普及已经有较长的历史,这源于它的开源特点以及其超越Windows的安全性和稳定性.而近年来, Linu ...

随机推荐

  1. systemd管理进程

    systemd很强大的管理工具,这里简单用来管理一个进程: [Unit]Description=Imges Compress Server [Service]Type=simpleExecStart= ...

  2. SpringCloud系列二:硬编码实现简单的服务提供者与服务消费者

    从本文开始,以一个电影售票系统为例讲解Spring Cloud 1. 版本 jdk:1.8 SpringBoot:2.0.0.RELEASE SpringCloud:Finchley.M8 2. 系统 ...

  3. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  4. android读取xml文件来实现省份,城市,区的选择

    本博客如需转载.请注明出处. ------------------------------------------------------------------------------------- ...

  5. Android程序的打包和安装

    当我们使用Android Studio的时候,这些步骤都交给它去做了. 编译 classes.dex 文件 编译 resources.arsc 文件 生成资源索引表resources.arsc. 把r ...

  6. 内核initcall分析

    linux中init相关内容定义在include/linux/init.h initcall相关定义 先看下文件说明,此文件定义的宏主要用于初始化阶段标记函数或初始化数据,之后占用的资源会被释放掉. ...

  7. linux下apache+php搭建配置记录

    第1章  环境说明1.1 系统说明Centos 6.2 (最小化安装)1.2 软件说明httpd-2.4.2.tar.gzapr-util-1.4.1.tar.gzapr-1.4.6.tar.gzpc ...

  8. 在Windows 10中开启开发者模式

    及以上)的电脑上使用Visual Studio来开发Windows 10或者Windows 8.1的应用,你可能会遇到下面的问题,要求你开启开发者模式. 于是你跑到设置里面,把开发者模式打开: 结果你 ...

  9. iphone开发技术要学习的内容

    一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...

  10. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...