Linux mmap() ENOMEM error causing Segmentation Fault,使用mongodb注意

from: http://www.enchantedage.com/node/235

mongodb 使用了mmap来映射管理文件,使用mongodb的要注意


I have a system that creates files on disk, then uses mmap and madvise and mflush to asynchronously do I/O to the disk. This system may potentially create many, many files, each of which will have three mmap sections, that will be rotated through the file.

After trying to run this system for a while, I started getting segmentation violations that I couldn't quite understand. Initially, I thought it was a threading problem, because I'm using boost::asio and boost::thread quite heavily. I used strace() to figure out what the system was doing, and found that right before the crashes, one or more calls to mmap() would fail.

Long story short: There is a limit to the number of mmap() segments that can be active in a Linux process at any one time. This limit is configurable in /proc/sys/vm/max_map_count. I already knew there was a file descriptor limit, and I raised that pretty high, but apparently Linux doesn't think you'll be using lots of mmap() just because you're using lots of files. Adding the following to /etc/rc.local will fix the problem:

echo 1000000 > /proc/sys/vm/max_map_count


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中的mmap函数是用来在用户空间和内核空间之间建立内存映射关系的。通过mmap函数,可以将一个文件或设备的物理内存映射到用户空间的虚拟地址中,从而可以通过访问这些虚拟地址来读写文件或控制硬件。 使用mmap函数的步骤如下: 1. 打开文件或设备,并获取文件描述符或设备句柄。 2. 调用mmap函数,传入文件描述符或设备句柄、映射区域大小、映射区域保护模式等参数。 3. mmap函数返回一个虚拟地址,可以通过该地址来访问映射的内存。 4. 访问虚拟地址对应的内存,可以读取或写入数据。 5. 使用完毕后,调用munmap函数来解除内存映射关系。 在Linux中,进程和线程都是用task_struct结构体来描述的。每个进程在内核中都有一个对应的task_struct结构体,该结构体包含了进程的各种信息,包括进程的内存映射信息(mm_struct)。 用户空间的虚拟地址空间被划分为不同的区域,包括栈区、堆区、BSS段区、DATA段区和TEXT段区。其中,栈区用于存储函数调用的局部变量和函数调用栈信息,堆区用于动态分配内存,BSS段区用于存储未初始化的全局变量,DATA段区用于存储已初始化的全局变量,TEXT段区用于存储可执行代码。 总结来说,Linux中的mmap函数可以用来在用户空间和内核空间之间建立内存映射关系,通过访问虚拟地址来读写文件或控制硬件。而在内核中,进程和线程都是用task_struct结构体来描述的,每个进程都有一个对应的mm_struct结构体来描述内存映射信息。用户空间的虚拟地址空间被划分为不同的区域,用于不同的目的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux中的mmap使用](https://blog.csdn.net/qq_28090573/article/details/50987550)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值