Skip to content

Instantly share code, notes, and snippets.

@mmorearty
Created October 26, 2015 17:08
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 mmorearty/fa34c0f29abe454fd14b to your computer and use it in GitHub Desktop.
Save mmorearty/fa34c0f29abe454fd14b to your computer and use it in GitHub Desktop.
Memory-consumption test to accompany http://superuser.com/a/767491/94393
// Compile this with "cc malloc-bytes.c -o malloc-bytes"
// Run with e.g. "./malloc-bytes 1000000" (mallocs 1MB)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <bytes-to-allocate>\n", argv[0]);
exit(1);
}
size_t bytes = (size_t)atol(argv[1]);
char* buf = malloc(bytes);
int i;
for (i=0; i<bytes; i+=4096) {
buf[i] = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment