Skip to content

Instantly share code, notes, and snippets.

@shalecraig
Last active December 14, 2015 07:29
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 shalecraig/5051161 to your computer and use it in GitHub Desktop.
Save shalecraig/5051161 to your computer and use it in GitHub Desktop.
Kill your computer with this quick memory allocation. Thrashing guaranteed, or your money back.
#include <cstdlib>
#include <stdio.h>
#include <sys/mman.h>
int main() {
char buffer[100];
int n;
// 16 GB * 1000
void* allRam = malloc(17179869184*1000);
printf("allocated \n");
n = sprintf(buffer, "%d", mlock(allRam, 17179869184 * 1000));
printf("result is %s, %d length\n", buffer, n);
return 0;
}
curl -O https://gist.github.com/shalecraig/5051161/raw/test.cpp && gcc test.cpp && ./a.out
@shalecraig
Copy link
Author

  1. Allocate a lot of ram.
  2. Force it to be locked in memory (and not swapped out of it).
  3. Laugh while you watch your computer transfer as much of your memory to ram as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment