Skip to content

Instantly share code, notes, and snippets.

@ddsimoes
Created July 12, 2023 13:25
Show Gist options
  • Save ddsimoes/a48a639539443be2e75a66042a2a4b26 to your computer and use it in GitHub Desktop.
Save ddsimoes/a48a639539443be2e75a66042a2a4b26 to your computer and use it in GitHub Desktop.
Try to allocate the max amount of dynamic memory to probe OS behaviour. Mostly for 32bit systems.
#include <stdio.h>
#include <stdlib.h>
#ifdef __MINGW32__
#define printf __mingw_printf
#endif
int main() {
size_t base_size = 0;
size_t limit = 0;
int progress = 1;
while (progress) {
progress = 0;
size_t size = 32 * 1024 * 1024; //10MB
void* ptr;
while ((ptr = malloc(base_size + size)) != NULL) {
progress = 1;
limit = base_size + size;
printf("Allocated %zu bytes of memory\n", limit);
size *= 2;
free(ptr);
}
base_size = limit;
printf("Unable to allocate %zu bytes of memory\n", base_size);
}
printf("MAX allocated memory: %zu MB \n", limit / (1024 * 1024));
return 0;
}
@ddsimoes
Copy link
Author

ddsimoes commented Jul 12, 2023

Compiled with:

Linux 64

gcc -o malluco --std=c99 --pedantic malluco.c

Linux 32

gcc -o malluco32 -m32 --std=c99 --pedantic malluco.c

Windows 64 (MingW)

x86_64-w64-mingw32-gcc -o malluco.exe -Wall -pedantic -std=c99 malluco.c

Windows 32 (MingW)

i686-w64-mingw32-gcc -o malluco32.exe -Wall -pedantic -std=c99 malluco.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment