[CentOS] Swap priorities with swapon (Is my swap drive working?)

Fri Jun 5 15:23:37 UTC 2009
Nicolas Thierry-Mieg <Nicolas.Thierry-Mieg at imag.fr>


James Bensley wrote:
>> It will kick in, regardless of priority.
>>
>> But probably someone will come up with a small c program which eats all
>> available memory :)
>>
>> Regards,
>>
>> Ralph
> 
> I would be interested  in such a program if anyone has one or a mega
> bash script that can achive the same?
> 

cat /proc/meminfo says it all, but if you really want to test this can 
do the job:

[nthierry at tryo tmp]$ cat totalMem.c
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   int size = 0 ;
   while(1)
     {
       if (malloc(1024*1024*sizeof(char)))
         size++ ;
       else
         {
           printf("Total available memory: %d Mb\n", size) ;
           exit(0) ;
         }
     }
}

[nthierry at tryo tmp]$ gcc -W -Wall totalMem.c
[nthierry at tryo tmp]$ su -
[root at tryo tmp]# echo 2 > /proc/sys/vm/overcommit_memory
[root at tryo tmp]# echo 100 > /proc/sys/vm/overcommit_ratio
[NOTE: the previous 2 commands must be run as root, and disable the 
memory overcommit mode. See man malloc (under BUGS) and man proc (search 
for overcommit) for details.]
[root at tryo tmp]# swapoff -a
[root at tryo tmp]# ./a.out
Total available memory: 1487 Mb
[root at tryo tmp]# swapon -a
[root at tryo tmp]# ./a.out
Total available memory: 2056 Mb

This system has 600 Mb swap space and 2 Gb of RAM of which ~500 Mb are 
currently used by other processes.
So it works, and shows that the swap is indeed available.

When satisfied restore the vm settings
[root at tryo tmp]# echo 50 > /proc/sys/vm/overcommit_ratio
[root at tryo tmp]# echo 0 > /proc/sys/vm/overcommit_memory