遇到这个问题是在測试项目的性能时发现的,每一个对象分配一页大小的空间然后mprotect() 保护起来,当系统分配3W多个页的时候会出现这个问题。

google到在一份邮件列表中也曾提到该问题.https://sourceware.org/ml/libc-help/2010-04/msg00026.html



摘抄部分例如以下:

----------------------------------------------------------------------------------------------------------------------

On Wed, 2010-04-21 at 01:05 +0200, PaweÅ Sikora wrote:

> hi,



> i'm trying to debug an ugly application with ElectricFence.

> in fact, on x86-64 box with 8GB ram and 16GB swap i'm getting following error:



> "ElectricFence Exiting: mprotect() failed: Cannot allocate memory"



> the program has been compiled with gcc-4.5, glibc-2.11.1, kernel-2.6.32.

> did you ever come across such (kernel/glibc) limitations?



> here's a simple testcase which triggs -ENOMEM in mprotect().



You probably depleted the max map count, see:

  /proc/sys/vm/max_map_count



We have a limit on the number of maps you can have, those mprotect()

calls split you maps like crazy, see also /proc/$pid/maps.

eg. change your second test program to include something like:

char buf[128];

  snprintf(buf, sizeof(buf), "cat /proc/%d/maps", (int)getpid());

  system(buf);



at the end after lowering your NN count to fit, and observe the result

of those mprotect() calls.



yes, that is the clue :)



the limit in /proc/sys/vm/max_map_count was set to 65530.

with `echo 128000 > /proc/sys/vm/max_map_count` the testcase passes.

----------------------------------------------------------------------------------------------------------------------

所以 解决方式是设置/proc/sys/vm/max_map_count为更大的值。

查看 max_map_count值:

more /proc/sys/vm/max_map_count

vm.max_map_count=128000直接写到/etc/sysctl.conf中,

然后运行

sysctl -p

或者直接运行

echo 128000 > /proc/sys/vm/max_map_count

OK 到这里问题就攻克了。总结一下原因:

proc文件系统给用户提供了非常多内核信息帮助,使得用户能够通过改动内核參数达到提高系统性能的目的.

-----------------------------------------------------------------------------------------------------------------------------------------------------

wangtao@wangtao-Z87X-UD5H:/proc/sys/vm$ ls

block_dump                  legacy_va_layout           oom_dump_tasks

compact_memory              lowmem_reserve_ratio       oom_kill_allocating_task

dirty_background_bytes      max_map_count              overcommit_memory

dirty_background_ratio      memory_failure_early_kill  overcommit_ratio

dirty_bytes                 memory_failure_recovery    page-cluster

dirty_expire_centisecs      min_free_kbytes            panic_on_oom

dirty_ratio                 min_slab_ratio             percpu_pagelist_fraction

dirty_writeback_centisecs   min_unmapped_ratio         scan_unevictable_pages

drop_caches                 mmap_min_addr              stat_interval

extfrag_threshold           nr_hugepages               swappiness

extra_free_kbytes           nr_hugepages_mempolicy     vfs_cache_pressure

hugepages_treat_as_movable  nr_overcommit_hugepages    would_have_oomkilled

hugetlb_shm_group           nr_pdflush_threads         zone_reclaim_mode

laptop_mode                 numa_zonelist_order

---------------------------------------------------------------------------------

/proc/sys/vm 下每一个文件的信息详见http://www.linuxinsight.com/proc_sys_vm_hierarchy.html

max_map_count :  This file contains the maximum number of memory map areas a process may have. Memory map areas are used as a side-effect of calling
malloc, directly by mmap and mprotect, and also when loading shared libraries. While most applications need less than a thousand maps, certain programs, particularly malloc debuggers, may consume lots of them, e.g., up to one or two maps per allocation. The
default value is 65536.

mprotect() failed: Cannot allocate memory的更多相关文章

  1. 解决Failed to allocate memory: 8转

    解决Failed to allocate memory: 8 昨天换了x64的Win7,发现在Eclipse上启动模拟器的时候存在问题,当设置的模拟器分辨率大于400×800的时候会出现 Failed ...

  2. Android 启动模拟器是出现“Failed to allocate memory: 8”错误提示的原因及解决办法

    某天,Android 启动模拟器是出现“Failed to allocate memory: 8”错误,模拟器无法启动,如下图: 原因:设置了不正确AVD显示屏模式,4.0版默认的模式为WVGA800 ...

  3. Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 (转)

    Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8http://www.crifan.com/android_emulator_ ...

  4. Failed to allocate memory: 8

    Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual ...

  5. 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8

    [问题] 折腾: [已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 过程中,增大对应AVD的内存为2G后,结果无法启 ...

  6. 关于阿里云ESC上go语言项目编译6l: running gcc failed: Cannot allocate memory

    (1)前段时间将自己的阿里云服务器上的系统由centos 6.5换为了ubuntu 14,其他的硬件配置都没有发生改变,将服务器上的数据恢复并且重新安装了golang的编译环境后,发现使用go bui ...

  7. fork failed.: Cannot allocate memory

    在做压力测试时候: [root@666 ok]# webbench -c 5000 -t30 http://10.100.0.61/ Webbench - Simple Web Benchmark 1 ...

  8. 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory

    [已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 结论是: 当前有个bug: 默认是通过 hw.ramSize=1024 ...

  9. 解决Zend OPcache huge_code_pages: mmap(HUGETLB) failed: Cannot allocate memory报错

    前几日看到鸟哥介绍的 <让你的PHP7更快之Hugepage>, 于是想试试手给服务器加上,参照格式安装好扩展,调整好配置文件,然后重启php-fpm,结果启动一直报Zend OPcach ...

随机推荐

  1. 在CMD中查看端口被什么程序占用

    我们要查看端口被什么程序占用,可以使用下面方法.比如端口28848 1. 打开cmd,输入命令netstat -ano | findstr ":28848",显示结果如下,最后一个 ...

  2. webpack 与 热编译webpack-dev-server

    webpack.config.js 只需要注意加大加粗的地方. var webpack = require("webpack"); var HtmlWebpackPlugin = ...

  3. PE下挂载注册表文件然后清除系统托盘空白图标缓存

    清除了右下角通知栏图标缓存TrayNotify(否则会出现一堆空白图标)清除缓存批处理脚本.bat如何在PE系统环境下清除宿主系统的托盘图标缓存? 清除了右下角通知栏图标缓存TrayNotify(否则 ...

  4. 浅谈Java中的System.gc()的工作原理

    很多人把Java的“效率低下”归咎于不能自由管理内存,但我们也知道将内存管理封装起来的好处,这里就不赘述. Java中的内存分配是随着new一个新的对象来实现的,这个很简单,而且也还是有一些可以“改进 ...

  5. mysql之log-slave-updates参数

    1.引言 使用Mysql的replication机制实现主从同步时,其是由三个线程实现了,主库一个I/O线程,从库一个I/O线程和一个SQL线程.配置时主库需要开始bin-log参数,即在配置文件中添 ...

  6. There's no Qt version assigned to this project for platform Win32. Please use the 'change Qt version' feature and choose a valid Qt version for this platform.

    这个是用在vs2015时爆出的问题. 解决方法是: 1.鼠标放置在解决方案中的工程名处,用鼠标右键点击(右击). 2.选择Qt Project Settings,在弹出的对话框中的version栏处填 ...

  7. spring :概述

    本章涵盖 spring's bean容器 探讨spring's 核心模块 强大的spring生态 spring 4新增内容 spring提供了众多的功能,但是这些功能都有基础的任务:让java开发变得 ...

  8. TCP/IP笔记(一)网络基础知识

    计算机与网络发展 计算机自诞生伊始,经历了一系列演变与发展.大型通用机计算机.超级计算机.小型机.个人电脑.工作站.便携式电以及现如今的智能手机终端都是这一过程的产物.它们性能逐年增强,价格却逐年下降 ...

  9. 一个改动配置文件的linux shell script

    不久以前,以前搜到一篇博客是读取配置文件的,http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html,用到如今,感觉十分方便.感谢作者. ...

  10. 微信 oauth4

    4.最后 4. 使用access_token获取用户信息 请求方法: https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN& ...