mprotect() failed: Cannot allocate memory

遇到这个问题是在测试项目的性能时发现的,每个对象分配一页大小的空间然后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文件系统给用户提供了很多内核信息帮助,使得用户可以通过修改内核参数达到提高系统性能的目的.

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

[email protected]:/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.

时间: 2024-07-31 10:54:34

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

Failed to allocate memory: 8

Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information. 解决办法直接在新建模拟器的时候把它的内存RAM设置在1024以下就可以,最好设置成512 如图:

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

前段时间将自己的阿里云服务器上的系统由centos 6.5换为了ubuntu 14,其他的硬件配置都没有发生改变,将服务器上的数据恢复并且重新安装了golang的编译环境后,发现使用go build编译稍微大一点的golang项目就会报错: /usr/local/go/pkg/tool/linux_amd64/6l: running gcc failed: Cannot allocate memory 一直想不通为啥换了个系统就会报这个错,字面意思是gcc分配内存失败,应该是内存不够用,机器配置

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

过程中,增大对应AVD的内存为2G后,结果无法启动AVD了: [2012-12-18 18:01:38 – Emulator] Failed to allocate memory: 8 [2012-12-18 18:01:38 – Emulator] [2012-12-18 18:01:38 – Emulator] This application has requested the Runtime to terminate it in an unusual way. [2012-12-18 1

解决Failed to allocate memory: 8转

解决Failed to allocate memory: 8 昨天换了x64的Win7,发现在Eclipse上启动模拟器的时候存在问题,当设置的模拟器分辨率大于400×800的时候会出现 Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for m

fork failed.: Cannot allocate memory

在做压力测试时候: [[email protected] ok]# webbench -c 5000 -t30 http://10.100.0.61/ Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://10.100.0.61/ 5000 clients, running 30 sec. Speed=1

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

前几日看到鸟哥介绍的 <让你的PHP7更快之Hugepage>, 于是想试试手给服务器加上,参照格式安装好扩展,调整好配置文件,然后重启php-fpm,结果启动一直报Zend OPcache huge_code_pages: mmap(HUGETLB) failed: Cannot allocate memory 看字面意思是无法分配内存?怎么会呢,机器内存还是很充足的,这点排查掉了.后面查找相关资料得到,是因为系统Hugepage不够,一般系统Hugepage是没有开启的,可以通过下面命令查

Linux Swap故障之 swapoff failed: Cannot allocate memory

swap分区关闭 准备调整Linux下的swap分区的使用率. 在Linux下执行 swapoff -a -v报如下错误: swapoff: /dev/mapper/cryptswap1: swapoff failed: Cannot allocate memory 上述错误原因分析: 从上述的信息可以看出,当前Linux系统把/dev/mapper/cryptswap1这个设备当做了交换分区,如果当前改交换分区使用的容量大于系统当前剩余的内存,就会报这个错误,因为在关闭交换分区的时候,需要把分

安装composer后报错proc_open(): fork failed - Cannot allocate memory

1.问题描述: 在linux服务器使用composer 安装插件时,出现“proc_open(): fork failed - Cannot allocate memory” 也就是提示“提示内存不足”,我们可以通过创建swap分区解决这个问题. 2.解决方法: 先运行 free -m 看下空间是多少 在命令行环境依次运行以下三条命令 dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 mkswap /var/swap.1 swapon /var/sw

关于Android创建虚拟机出现Failed to allocate memory: 8解决办法

开发前期使用eclipse,后来手贱又安装了Android studio,创建虚拟机后eclipse打不开已经创建好的虚拟机,哇擦~这也冲突??? 查了好多资料,结果发现,只需要直接在新建模拟器的时候把它的内存RAM设置在1024以下就可以,我改成了512 至于为什么内存改低了就可以,留待以后探索吧....也希望大神能够指点,谢谢~