Created
August 24, 2015 18:03
-
-
Save meddulla/471828968568c77a8a29 to your computer and use it in GitHub Desktop.
Gobble memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MB 1048576 | |
#define CONSUME 10 * MB | |
// Usage: ./memory-eater {total MB} {sleep seconds} | |
// eg ./memory-eater 700 30 to consume 700MB for 30 secs | |
int main(int argc, char *argv[]) { | |
void *b = NULL; | |
int bytes = 0; | |
int max_mem = 0; | |
int secs = 5; | |
if( argc == 2 ) { | |
max_mem = atoi(argv[1]) * MB; | |
} else if(argc == 3) { | |
max_mem = atoi(argv[1]) * MB; | |
secs = atoi(argv[2]); | |
} else { | |
printf("Usage: ./memory-eater {total MB} {sleep seconds} \n"); | |
exit(0); | |
} | |
printf("Gobbling up %d bytes from RAM for %d seconds\n", max_mem, secs); | |
while(1) { | |
if (bytes >= max_mem) { | |
printf("Reached max memory: %d bytes\n", bytes); | |
break; | |
} | |
/* allocate xMB of memory */ | |
b = (void *) malloc(CONSUME); | |
if (!b) { | |
break; | |
} | |
/* consume our allocated memory */ | |
memset(b, 1, CONSUME); | |
bytes = bytes + CONSUME; | |
} | |
sleep(secs); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment