[vox-tech] can YOU be certified?!?

Jeff Newmiller vox-tech@lists.lugod.org
Sun, 21 Jul 2002 11:40:00 -0700 (PDT)


On Sun, 21 Jul 2002, Tim Riley wrote:

> 
> 
> Peter Jay Salzman wrote:
> 
> > begin ME <dugan@passwall.com>
> > > On Sun, 21 Jul 2002, Peter Jay Salzman wrote:
> > <snip>
> >
> > besides /cat/swaps, i thought of one more good way to know if you need
> > to add swap space:
> >
> > out of memory errors when you start new processes.
> 
> To confirm this I wrote the following program:
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> #define BUFFER_INCREMENT        1024000
> 
> int main( int argc, char **argv )
> {
>         char *buffer = "";
>         unsigned long i;
> 
>         for ( i = BUFFER_INCREMENT; buffer ; i += BUFFER_INCREMENT )
>         {
>                 buffer = malloc( BUFFER_INCREMENT );
>                 printf( "Total allocation = %ld ", i );
> 
>                 if ( buffer )
>                 {
>                         strcpy( buffer, "Hello world!" );
>                         printf( "Succeeded = %s\n", buffer );
>                 }
>         }
>         printf( "Failed\n" );
>         sleep( 10 );
>         exit( 0 );
> }
> 
> Two weird things occurred when I ran this.
> 
> First, the "top" command never showed
> any swap space usage. Moreover, the
> memory available never made it to zero.
> It went from 28M down
> to 18M before the malloc failed.

Until you touch the pages involved, they do not become allocated.  Like
holes in files.  
 
> Second and serendipitously, the bytes
> allocated was 14 times more that the
> total of memory and swap space.
> (I have 64M memory and 72M
> of swap (136M total), yet I was able to malloc
> 1987M of memory.)

malloc allocates some memory as overhead, and you wrote a bit more.  Thus,
I would expect one 4096 byte page was physically allocated per
buffer.  You allocated about 2034 buffers, so that racks up about 8M of
touched memory.

Either two pages are actually allocated (8M*2=16M < 18M) or the kernel is
trying to keep some spare memory around.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
---------------------------------------------------------------------------