Skip to content

Instantly share code, notes, and snippets.

@teknoraver
Last active November 24, 2025 12:53
Show Gist options
  • Select an option

  • Save teknoraver/08baef0e33a5969d1e2a8224ccc6034d to your computer and use it in GitHub Desktop.

Select an option

Save teknoraver/08baef0e33a5969d1e2a8224ccc6034d to your computer and use it in GitHub Desktop.
Allocates memory and trigger pgscan activity
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
void allocate_memory(size_t gb)
{
size_t bytes = gb * 1024 * 1024 * 1024ULL;
size_t pgsize = sysconf(_SC_PAGESIZE);
unsigned char *ptr = malloc(bytes);
if (!ptr) {
puts("Memory allocation failed");
exit(1);
}
// Continuously write the memory to trigger pgscan activity
while (1) {
static int once = 0;
for (size_t i = 0; i < bytes; i += pgsize)
ptr[i] = 0xff;
if (!once) {
printf("Allocated %zu GB. Press Ctrl+C to exit.\n", gb);
once = 1;
}
sleep(1);
}
}
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage: %s <gb>\n", argv[0]);
return 1;
}
int gb = atoi(argv[1]);
if (gb <= 0) {
puts("Please provide a positive integer for GB.");
return 1;
}
allocate_memory(gb);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment