Skip to content

Instantly share code, notes, and snippets.

@yangyuqian
Created November 30, 2017 05:57
Show Gist options
  • Save yangyuqian/dd3b8e8756a73d6bcb141f05e507f22a to your computer and use it in GitHub Desktop.
Save yangyuqian/dd3b8e8756a73d6bcb141f05e507f22a to your computer and use it in GitHub Desktop.
Code snippet to simulate memory load
// This program takes a integer N as input to "eat" N megabytes RAM
// to simulate arbitrary ram load in reality.
//
// To run this program on CentOS:
// ```
// # install gcc
// $ yum install gcc -y
// # compile the program
// $ gcc -o crazy_ram crazy_ram.c
// # run the program to consume 1G RAM
// $ ./crazy_ram 1024
// ```
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define MEGABYTE 1024*1024
int main(int argc, char *argv[])
{
void *myblock = NULL;
int count = 0;
int i;
char *p;
int num;
errno = 0;
long conv = strtol(argv[1], &p, 10);
// Allocate 2G RAM to simulate memory load
for(i = 0; i < conv; i++)
{
myblock = (void *) malloc(MEGABYTE);
if (!myblock) break;
memset(myblock,1, MEGABYTE);
printf("Currently allocating %d MB\n",++count);
}
while(1){}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment