Created
March 13, 2024 11:47
-
-
Save gwpl/70795cf77a2c0d17c3c3277874aa3a0d to your computer and use it in GitHub Desktop.
Toy example illustrating that on Linux memory is initialized when used.
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 <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
int main() { | |
// Allocate 16GB of memory | |
size_t size = 16UL * 1024 * 1024 * 1024; | |
char* memory = malloc(size); | |
// Access only some memory in the middle | |
size_t middle = size / 2; | |
memory[middle] = 42; | |
// Sleep for a while to allow for memory usage inspection | |
sleep(6); | |
free(memory); | |
return 0; | |
} | |
// To compile this program, use the following command: | |
// gcc -o memtest memtest.c | |
// To run this program and measure its memory usage, use the following command: | |
// /usr/bin/time -v ./memtest | |
// Please note that the actual memory usage might be different depending on the system's memory management. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment