Skip to content

Instantly share code, notes, and snippets.

@pavel-odintsov
Last active August 29, 2015 14:19
Show Gist options
  • Save pavel-odintsov/717820007c8de969b051 to your computer and use it in GitHub Desktop.
Save pavel-odintsov/717820007c8de969b051 to your computer and use it in GitHub Desktop.
memory_eater_ng.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Author: pavel.odintsov@gmail.com at FastVPS Eesti OU
// License: GPLv2
// This script allocate huge block of memory and fill it with random crap
// It's useful for checking kdump or ISP's with memory oversell :)
// Compilation: gcc memory_eater_ng.c -omemory_eater_ng
// Run it for 4GB memory region: ./memory_eater_ng 4
// This code could work correctly only on 64 bit platforms, i.e. x86_64
int main(int argc, char *argv[]) {
// Allocate 1GB memory by default
unsigned long long int buffer_size_in_gb = 1L;
if (argc > 1) {
buffer_size_in_gb = atoi(argv[1]);
}
// Calculate memory region in bytes
unsigned long long int buffer_size = buffer_size_in_gb * 1024L * 1024L * 1024L;
printf("I will allocate %d GB of RAM\n", buffer_size_in_gb);
unsigned long long* buffer_as_array_of_64_bit_integers = (unsigned long long*)malloc(buffer_size);;
printf("Well, I allocated memory\n");
printf("I will fill this memory by random crap\n");
unsigned int index = 0;
for (index = 0; index < buffer_size / sizeof(unsigned long long); index++) {
// Fill it with random crap
buffer_as_array_of_64_bit_integers[index] = rand();
}
printf("Memory filled by crap corectly, go to infinite sleep\n");
// Go to infinite loop, wait forever
while (1) {
sleep(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment