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) |