最近有内存使用报警的邮件发出,之后杀掉了内存占用高的进程,使内存恢复正常
但是发现某些程序被杀掉了,有过怀疑是被人手动杀掉的,看日志后发现应该是内存占用过大,系统自动杀掉的内存耗尽会调用oom 对进程进行评估 并选择一个进程杀死 以释放内存
dmesgJun 26 08:45:47 localhost kernel: [6409835.925696] curl invoked oom-killer: gfp_mask=0x280da, order=0, oom_adj=0, oom_score_adj=0Jun 26 08:45:48 localhost kernel: [6409835.925705] curl cpuset=/ mems_allowed=0-1Jun 26 08:45:48 localhost kernel: [6409835.925711] Pid: 3640, comm: curl Not tainted 3.2.0-41-generic #66-UbuntuJun 26 08:45:48 localhost kernel: [6409835.925715] Call Trace:Jun 26 08:45:48 localhost kernel: [6409835.925729] [] dump_header+0x91/0xe0Jun 26 08:45:48 localhost kernel: [6409835.925734] [ ] oom_kill_process+0x85/0xb0Jun 26 08:45:48 localhost kernel: [6409835.925740] [ ] 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 childJun 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.confubuntu soft as 1200000ubuntu hard as 1500000
系统内存情况
free total used free shared buffers cachedMem: 1015664 851624 164040 148 216548 378456-/+ buffers/cache: 256620 759044Swap: 999996 3344 996652
申请内存的代码
test.c#include#include 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./mallocmaximum malloc size = 1.101562 GBthe address is 618c2010the address end is a80c2010
去掉交换内存后的情况
free total used free shared buffers cachedMem: 1015664 856708 158956 384 216580 379532-/+ buffers/cache: 260596 755068Swap: 0 0 0
系统内存使用情况
./mallocmaximum malloc size = 0.784881 GBthe address is 3dc48010the address end is 6fffff56
这个限制是包括交换内存在内的,系统会保留一定的内存供内核使用,用户空间能够申请的内存使用量达不到最大值
附注:
限制用户的内存和cpu使用还可以通过cgroup的方式进行, 这一点还不熟,希望以后可以有机会用到