Skip to content

Instantly share code, notes, and snippets.

@travispaul
Last active November 8, 2016 14:38
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 travispaul/9bacb8871c21105785d6a322bae183b1 to your computer and use it in GitHub Desktop.
Save travispaul/9bacb8871c21105785d6a322bae183b1 to your computer and use it in GitHub Desktop.
Eat up memory (for testing memory caps)
// example: ./a.out 10 1 150
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv) {
int mbytes = atoi(argv[1]);
int seconds = atoi(argv[2]);
int limit = atoi(argv[3]);
int total = 0;
printf("malloc'ing %d MB every %d second(s)\n", mbytes, seconds);
while (malloc(mbytes * 1024 * 1024) != NULL) {
total += mbytes;
printf("malloc'd %d MB\n", total);
if (total >= limit) {
break;
}
sleep(seconds);
}
if (errno == ENOMEM)
printf("ENOMEM\n");
if (errno == EAGAIN)
printf("EAGAIN\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment