Re: Memory overcommitting (was Re: http://www.redhat.com/redhat/)

Richard B. Johnson (root@analogic.com)
Thu, 20 Feb 1997 08:38:06 -0500 (EST)


On Thu, 20 Feb 1997, Tuukka Toivonen wrote:

> As I have understand, Linux returns ALWAYS success when using malloc(),
> because only reason why malloc() would fail, is memory overrun, and this
> will never happen in malloc().
>
> So my question is: is there any point in checking whether malloc() returned
> NULL (failure) or success? Should i just start using the memory without
> checking if the pointer is NULL?
>
> | Tuukka Toivonen <tuukkat@stekt.oulu.fi>
> | Homepage: http://stekt.oulu.fi/~tuukkat/
> | Try also finger -l tuukkat@stekt.oulu.fi
> | Studying information engineering at the University of Oulu
> +-----------------------------------------------------------
>
Not true. Malloc and friends will return a NULL pointer when it is
no longer able to set a new break address, i.e., get more memory from
the kernel. You can try it yourself....
long mem = 0L;
char *ptr;
for(;;)
{
ptr = malloc(0x10000);
if(ptr == NULL)
{
fprintf(stderr, "No memory %d\n", mem);
exit(1);
}
*ptr = 0xaa; /* dirty memory */
mem += 0x10000;
}

Now, if you don't write to the memory, the allocation still fails at
what seems to be around the page-file size + 4 megabytes * 3. If
you do write to memory, the allocation fails at about the page-file
size + 4 megabytes. I have 32 megabytes of RAM so your mileage may
vary.

It looks as though "over commitment" should not be a problem. If memory
allocation fails, the program should have some way of handling the
situation, i.e., writing primary results out to disk files, etc. Too
many so-called Software Engineers think that the way to sort something,
for instance, is to load the entire database into RAM. Sorry. I have
a database you will NEVER fit into RAM. There just isn't that much
RAM in the world (--yet, but they are working on it).

Cheers,
Dick Johnson
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Richard B. Johnson
Project Engineer
Analogic Corporation
Voice : (508) 977-3000 ext. 3754
Fax : (508) 532-6097
Modem : (508) 977-6870
Ftp : ftp@boneserver.analogic.com
Email : rjohnson@analogic.com, johnson@analogic.com
Penguin : Linux version 2.1.26 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-