Chinaunix
标题:
When Linux Runs Out of Memory
[打印本页]
作者:
eslover
时间:
2008-09-03 17:05
标题:
When Linux Runs Out of Memory
[/url]
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:
[url=http://lixiang.cn/?q=node/110]http://lixiang.cn/?q=node/110
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u1/59445/showart_1163185.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2