Friday, July 12, 2013

How to know if Linux swap is working


After installation of Ubuntu 12.04 LTS, its seems like my swap was never, used, most of the time it remained inactive in my system monitor. In order to ensure, if swap is working, one trick is to write a C code that fills up the available RAM and ensure, if swap is being used.

The code below, allocates 10 MB per allocation, do monitor the swap.[1]

 #include <stdlib.h>  
 #include <stdio.h>  
 #include <string.h>  
 int main(int argc, char** argv) {  
   int max = -1, mb = 0;  
   char* buffer;  
   if(argc > 1)   
     max = atoi(argv[1]);  
   else {  
      printf("\nmeater <size in MB> eg: meater 2048\n");  
       exit(0);       
   }  
   printf("\n[INFO] attempting to allocate %d MB @ 10 MB\n", max);  
   while((buffer=malloc(10*1024*1024)) != NULL && mb != max/10) {  
     memset(buffer, 0, 10*1024*1024);  
     mb++;  
     printf("[INFO] allocated %d MB\n", mb*10);  
     sleep(1);  
   }  
   printf("[INFO] meater done eating, yummy..!\n");  
   return 0;  
 }  

No comments :

Post a Comment

Translate