Monday, April 27, 2015

Increase swap memory utilization

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

int main(int argc, char** argv) {
    int max = -1;
    int mb = 0;
    char* buffer;

    if(argc > 1)
        max = atoi(argv[1]);

    while((buffer=malloc(1024*1024)) != NULL && mb != max) {
        memset(buffer, 0, 1024*1024);
        mb++;
        printf("Allocated %d MB\n", mb);
        sleep(1);
    }      
return 0;
}
Save the file on Linux with filename.c
Your program name will be "memeater".
Compile your code as under:
gcc filename.c -o memeater --you could specify desired name here....
execute the code: "./memeater". Once it is done you will see progress like,
[root@oralinux3 tmp]# ./memeater
Allocated 1 MB
Allocated 2 MB
Allocated 3 MB
Allocated 4 MB
Allocated 5 MB
Allocated 6 MB
Allocated 7 MB
Allocated 8 MB
Allocated 9 MB
Allocated 10 MB
Allocated 11 MB
Allocated 12 MB
Allocated 13 MB
# top -M
top - 21:00:09 up  1:43,  2 users,  load average: 0.02, 0.01, 0.00
Tasks: 119 total,   1 running, 118 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.2%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  1867.559M total,  279.988M used, 1587.570M free,   55.551M buffers
Swap: 3999.992M total,   17.824M used, 3982.168M free,   34.516M cached