[PLUG] cleaning up RAM

Keith Lofstrom keithl at kl-ic.com
Wed Aug 1 15:04:01 UTC 2007


I have a 1GB laptop.  At night I run dirvish/rsync for backups, and
rsync gobbles up all available memory.  Afterwards, the process
monitor in the gnome panel, and the command "free", shows most memory
allocated to buffers and cache (which is no longer used).  This may
not be a lethal problem, but it doesn't look tidy, and I suspect it
makes suspends take too long and perhaps even fail sometimes.

So I wrote a cheezy little application, "bigalloc", that clears out
memory of these unused buffers using lots of malloc() calls, below. 
"bigalloc" pushes all my running applications into swap, making them
a little slow the first time I access them afterwards, but the all
the stale old buffers/caches go away.

I'm going to set this up to run after rsync at night, and see if the
occasional suspend failures and gnome freezes are reduced.

Keith

-------------------------  bigalloc.c --------------------------------
// bigalloc.c  - use up memory 
// Keith Lofstrom  2007 Aug 1

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

// these should be less than 2^31 for int32 counters
// REALBIG is my machine size in KB
#define REALBIG   1048576
#define MAXSIZE   1024

char * ptr[MAXSIZE] ;
int    cnt, size, clear ;

int main() {
   for( size=0; size<MAXSIZE; size++ ) {
      ptr[size] = malloc( REALBIG );
      if( errno == ENOMEM ) break ;

      // memory is not actually taken until it is filled
      for( cnt=0 ; cnt< REALBIG ; cnt++ ) {
         ptr[size][cnt]= (char) 1;
      }
#ifdef DEBUG
      printf( "%8ld\r", size );
      fflush(stdout);
#endif
   }
   for( clear=0; clear<size; clear++ ) {
      free( ptr[clear] );
   }
   printf( "\n" );
   return( 0 );
}
----------------------------------------------------------------------

-- 
Keith Lofstrom          keithl at keithl.com         Voice (503)-520-1993
KLIC --- Keith Lofstrom Integrated Circuits --- "Your Ideas in Silicon"
Design Contracting in Bipolar and CMOS - Analog, Digital, and Scan ICs



More information about the PLUG mailing list