Last active
August 25, 2016 14:38
-
-
Save miku/795fca2728902e857e012a00e1a9308e to your computer and use it in GitHub Desktop.
Endless malloc
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
/mallocx |
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
all: mallocx | |
mallocx: mallocx.c | |
cc -o mallocx mallocx.c | |
clean: | |
rm -f mallocx |
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
// Allocate until machine runs out of memory. Virtual memory is different from | |
// physical. | |
// | |
// $ make | |
// $ ./mallocx | |
// 1000000 10485760000000 | |
// 2000000 20971520000000 | |
// 3000000 31457280000000 | |
// 4000000 41943040000000 | |
// 5000000 52428800000000 | |
// 6000000 62914560000000 | |
// 7000000 73400320000000 | |
// 8000000 83886080000000 | |
// 9000000 94371840000000 | |
// 10000000 104857600000000 | |
// 11000000 115343360000000 | |
// 12000000 125829120000000 | |
// 13000000 136314880000000 | |
// mallocx(21718,0x7fff77527000) malloc: *** mach_vm_map(size=10485760) failed (error code=3) | |
// *** error: can't allocate region | |
// *** set a breakpoint in malloc_error_break to debug | |
// 131063.58GB | |
// | |
// On my old Macbook Pro with 16GB RAM, OS X can allocate ~127TB virtual memory | |
// before bailing out. | |
// | |
// More: http://cecs.wright.edu/~pmateti/Courses/233/Labs/Processes/virtualMemory.html | |
#include <stdlib.h> | |
#include <stdio.h> | |
// MB | |
const long MB = 1024 * 1024; | |
// GB | |
const long GB = 1024 * 1024 * 1024; | |
// allocation increment | |
const int increment = 10 * MB; | |
int main(int argc, char const *argv[]) | |
{ | |
unsigned long i = 0; | |
while (1) { | |
int *p = malloc(increment); | |
if (p == NULL) { | |
fprintf(stderr, "%0.2fGB\n", (double)(i * increment) / GB); | |
break; | |
} | |
i++; | |
if (i % 1000000 == 0) { | |
printf("%10lu\t%20lu\n", i, i * increment); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment