Skip to content

Instantly share code, notes, and snippets.

@Bystroushaak
Created August 25, 2011 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bystroushaak/1171204 to your computer and use it in GitHub Desktop.
Save Bystroushaak/1171204 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MEMINFO "/proc/meminfo"
#define MEM "/dev/mem"
#define MB 1024 * 1024
// Returns free memory in kB
int get_free_memory(){
FILE *f;
char buff[80];
f = fopen(MEMINFO, "r");
fscanf(f, "%*s%*s%*s%*s%s", buff); // MemFree: %s
fclose(f);
return atoi(buff);
}
int main(int argc, char *argv[]){
// check permissions
if (geteuid() != 0){
fputs("This program needs to run under the root (euid == 0)!\n", stderr);
return 1;
}
// check procfs
if(access(MEMINFO, R_OK) == -1){
fputs("This program needs procfs (http://en.wikipedia.org/wiki/proc)!\n", stderr);
return 2;
}
// check swapoff
if (system("which swapoff > /dev/null") != 0){
fputs("This program needs 'swapoff' command!\n", stderr);
return 3;
}
// turn off swap
printf("Turning off swap .. ");
if (system("swapoff -a") != 0){
fputs("Oucha, cant turn off swap! :(\n", stderr);
return 4;
}
puts("done.");
// eat free memory
char *v;
uint i, amount;
puts("Eating memory:");
while((amount = get_free_memory()) > 0){
printf("\tFree memory: %dkB\n", amount);
if ((v = malloc(MB)) != 0)
for(i = 0; i < MB; i++)
v[i] = 0xFF;
else
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment