我正在使用Linux(正好是Ubuntu 13.04),目前我有一个问题:为什么内存分配将失败,即使有足够的内存?
我今天写了一个简单的testing应用程序,在运行这个testing应用程序时遇到了这个问题 以下是我用来testing的代码片段:
#include <stdio.h> #include <unistd.h> #include <list> #include <vector> #include <strings.h> using namespace std; unsigned short calcrc(unsigned char *ptr,int count) { unsigned short crc; unsigned char i; //high cpu-consumption code //implements CRC algorithm: Cylic //Redundancy code } void* CreateChild(void* param){ vector<unsigned char*> MemoryVector; pid_t PID = fork(); if (PID == 0){ const int MEMORY_TO_ALLOC = 1024 * 1024; unsigned char* buffer = NULL; while(1){ buffer = NULL; try{ buffer = new unsigned char [MEMORY_TO_ALLOC](); calcrc(buffer,MEMORY_TO_ALLOC ); MemoryVector.push_back(buffer); } catch(...){ printf("an exception was thrown!n"); continue; } //try ... catch } //while } // if pid == 0 return NULL; } int main(){ int children = 4; while(--children >= 0){ CreateChild(NULL); }; while(1) sleep(3600); return 0; }
在我的testing中,上面的代码在有大约220M RAM的时候开始抛出exception。 而从现在起,看起来应用程序不能再获得更多的内存,因为TOP命令显示的空闲内存仍然高于210M。 那为什么会这样呢?
UPDATE
1.软件&&硬件信息
RAM是4G ,交换大约是9G字节。 运行“uname -a”给出:Linux steve -ThinkPad -T410 3.8.0-30-generic#44-Ubuntu SMP Thu Aug 22 20:54:42 UTC 2013 i686 i686 i686 GNU / Linux
分析C应用程序中的最大内存使用情况 – linux
我怎么知道我的.so库的脚印?
为什么calloc无法在具有4GB内存的系统上分配1GB?
应用程序可以使用的最大内存空间是多less(物理内存+交换空间)?
进程的虚拟地址范围
Right after Test App Starts Throwing Exception steve@steve-ThinkPad-T410:~$ free total used free shared buffers cached Mem: 3989340 3763292 226048 0 2548 79728 -/+ buffers/cache: 3681016 308324 Swap: 9760764 9432896 327868 10 minutes after Test App Starts Throwing Exception steve@steve-ThinkPad-T410:~$ free total used free shared buffers cached Mem: 3989340 3770808 218532 0 3420 80632 -/+ buffers/cache: 3686756 302584 Swap: 9760764 9436168 324596 20 minutes after Test App Starts Throwing Exception steve@steve-ThinkPad-T410:~$ free total used free shared buffers cached Mem: 3989340 3770960 218380 0 4376 104716 -/+ buffers/cache: 3661868 327472 Swap: 9760764 9535700 225064 40 minutes after Test App Starts Throwing Exception steve@steve-ThinkPad-T410:~$ free total used free shared buffers cached Mem: 3989340 3739168 250172 0 2272 139108 -/+ buffers/cache: 3597788 391552 Swap: 9760764 9556292 204472
什么导致MysqL交换使用,并最终oom_killer
在Windows中分配内存
分配“临时”内存(在Linux中)
如何确定堆栈指针的初始值?
原生应用程序的初始内存占用空间较大
你在x86-32上运行,所以你的进程是32位的。 即使有更多的内存+交换,他们将受到他们的地址空间的限制,运行:
grep “HIGHMEM” /boot/config-`uname -r` grep “VMSPLIT” /boot/config-`uname -r`
看看你的内核是如何配置的。
也许你的4个子进程仅限于3G,并且正在使用12G +的其他进程〜700M来达到你所看到的数字。
编辑:
所以你的内核被配置为给每个用户空间进程的地址空间,其中一些将被程序,库和初始运行时内存(将由于分叉而共享)占用。
因此,你有4个孩子每个〜3G〜〜12G +〜780M其他程序。 一旦孩子们开始报告错误,这将留下大约220M的空闲。
你可以运行另一个子进程,或者你可以用Ubuntu的AND64 / x86-64版本重新安装,每个进程将能够分配更多的内存。
可能你的地址空间中没有更多1MB的连续内存页面。 你有空闲空间碎片。
在我的测试中,上面的代码在有大约220M的内存时开始抛出异常。 而从现在起,看起来应用程序不能再获得更多的内存,因为TOP命令显示的空闲内存仍然高于210M。 那为什么会这样呢?
另一方面,内存分配速度非常快。
发生什么事是你的程序吃内存,并在某些时候(当顶部显示200 MB免费)开始失败。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。