Article 6FEKQ How can I write a program to trigger memory direct reclaim ?

How can I write a program to trigger memory direct reclaim ?

by
plznobug
from LinuxQuestions.org on (#6FEKQ)
Recently, my server experienced a hang which lasted for about ten minutes.
Upon investigation, I discovered that the system triggered a direct memory reclaim.
In my server, the value of /proc/sys/vm/min_free_kbytes is set to 2GB.
We have launched a program to monitor the memory.
During the hung period, I noticed that the available field and free field of 'free -h' were 15GB and 2GB respectively.

In an attempt to trigger direct memory reclamation, I wrote a program that utilizes mmap to allocate memory as below.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <time.h>

#define PAGE_SZ 4096
#define GB (1<<30)
int random_number(int min, int max) {
return min + rand() % (max - min + 1);
}

int main(int argc, char** argv) {
size_t size;
size = 5ul * GB;
printf("size %lu\n",size);

void *mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}

size_t num_pages = size / PAGE_SZ;

size_t i;
for (i = 0; i < num_pages; i++) {
*((char *)mem + i * PAGE_SZ) = '6';
}

srand(time(NULL));

while (1) {
size_t random_page = random_number(0, num_pages - 1);
char first_byte = *((char *)mem + random_page * PAGE_SZ);
}

return 0;
}

However, I observed that only the available field decreased, while the free field did not show a noticeable decrease. This made it impossible to trigger the direct memory reclamation.

So I wonder how to make free and available decrease simultaneously?

I would greatly appreciate any advice or suggestions on how to write testing programs that can effectively trigger direct memory reclamation.
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments