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

Fri Jun 5 15:13:01 UTC 2009
John Doe <jdmls at yahoo.com>

From: James Bensley <jwbensley at gmail.com>
> > But probably someone will come up with a small c program which eats all
> > available memory :)
> I would be interested  in such a program if anyone has one or a mega
> bash script that can achive the same?

Quick, dirty and unoptimized (and barely tested, might not work past 4GB)... ^_^

- - - - - -8<- - - - - - - - - - - -8<- - - - - - - - - - - -8<- - - - - -
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

    FILE *fs;
    int i;
    long j;
    int mem_total;
    int mem_free;
    int mem_buffered;
    int mem_cached;
    int mem_to_fill;
    char *megabuff[1024];

    fs = fopen("/proc/meminfo", "r");
    fscanf(fs, "%*s %d %*s\n", &mem_total);
    fscanf(fs, "%*s %d %*s\n", &mem_free);
    fscanf(fs, "%*s %d %*s\n", &mem_buffered);
    fscanf(fs, "%*s %d %*s\n", &mem_cached);
    fclose(fs);

    mem_to_fill = (mem_free+mem_buffered+mem_cached+1024)*sizeof(char);
    printf("Filling %dKB...\n", mem_to_fill);
    for (i=0; i<1024; i++) {
        if ((megabuff[i] = malloc(mem_to_fill)) == NULL) {
            fprintf(stderr, "malloc error...");
        } else {
            for (j=0; j<mem_to_fill; j++) {
                megabuff[i][j] = '1';
            }
        }
    }
    for (i=0; i<1024; i++) {
        free(megabuff[i]);
    }
}

- - - - - -8<- - - - - - - - - - - -8<- - - - - - - - - - - -8<- - - - - -

Then swapoff /dev/..; swapon /dev/... to clean...

JD