Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101465
  • 博文数量: 26
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-13 11:46
文章分类

全部博文(26)

文章存档

2011年(1)

2010年(2)

2009年(3)

2008年(20)

我的朋友
最近访客

分类: LINUX

2008-09-03 17:05:58

Perhaps you rarely face it, but once you do, you surely know what's wrong: lack of free memory, or Out of Memory (OOM). The results are typical: you can no longer allocate more memory and the kernel kills a task (usually the current running one). Heavy swapping usually accompanies this situation, so both screen and disk activity reflect this.
At the bottom of this problem lie other questions: how much memory do you want to allocate? How much does the operating system (OS) allocate for you? The basic reason of OOM is simple: you've asked for more than the available virtual memory space. I say "virtual" because RAM isn't the only place counted as free memory; any swap areas apply.

Exploring OOM

To begin exploring OOM, first type and run this code snippet that allocates huge blocks of memory:
#include 
#include


#define MEGABYTE 1024*1024
int main(int argc, char *argv[])
{
void *myblock = NULL;
int count = 0;

while (1)
{
myblock = (void *) malloc(MEGABYTE);
if (!myblock) break;
printf("Currently allocating %d MB\n", ++count);
}

exit(0);
}

Compile the program, run it, and wait for a moment. Sooner or later it will go OOM.
Now compile the next program, which allocates huge blocks and fills them with 1:

阅读(761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~