Created
October 29, 2020 01:59
-
-
Save aaronmussig/4431bbec814d1543b5212c90822f361b to your computer and use it in GitHub Desktop.
Progressively request memory up to the specified GB (e.g. "./memtest 5" will try allocate up to 5 GB of memory).
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
#include <stdio.h> | |
#include <stdlib.h> | |
int main( int argc, char *argv[] ) { | |
// Check input arguments. | |
if(argc<=1) { | |
printf("Please provide the maximum number of GB to allocate, e.g.: ./memtest 5"); | |
exit(1); | |
} | |
int max_gb; | |
max_gb = atoi(argv[1]); // Convert input argument to integer. | |
printf("Requesting 1 GB chunks up to %d GB.\n", max_gb); | |
// Initialise vars. | |
int* arr; | |
int iter_gb, cur_gb, i, n, j; | |
iter_gb = 1; | |
n = (1000000000 * iter_gb) / sizeof(int); // number of elements to be iter_gb. | |
// Initialise the pointer by allocating a small amount of memory. | |
arr = (int*)malloc(sizeof(int)); | |
if (arr == NULL) { | |
printf("ERROR: Unable to initialise the array.\n"); | |
exit(1); | |
} | |
// Keep requesting memory up to max_gb. | |
for(i = 0; i < max_gb; ++i) { | |
printf(" INFO: Memory requested (%d/%d) GB... requesting memory...", i * iter_gb, max_gb); | |
arr = realloc(arr, n * (i + 1) * sizeof(int)); | |
if (arr == NULL) { | |
printf("\nERROR: Unable to request more memory (failed at %d GB).\n", i * iter_gb); | |
exit(1); | |
} | |
// Initialise the elements of the array. | |
printf(" initialising..."); | |
for (j = 0; j < n * (i + 1); ++j) { | |
arr[j] = j; | |
} | |
printf(" done!\n"); | |
} | |
// Done. | |
printf(" DONE: Successfully allocated %d GB of memory.\n", max_gb); | |
free(arr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment