Created
November 30, 2017 05:57
-
-
Save yangyuqian/dd3b8e8756a73d6bcb141f05e507f22a to your computer and use it in GitHub Desktop.
Code snippet to simulate memory load
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
// 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