0

AFAIK, malloc have no reasons to use physical memory unless the actual write operation is taken, because of Demand paging, but when I actually test:

// gcc test.c
#include <stdio.h>
 #include <stdlib.h>

 int main (void) {
         int n = 0;

         while (1) {
                 if (malloc(1<<20) == NULL) {
                         printf("malloc failure after %d MiB\n", n);
                         return 0;
                 }
                 printf ("got %d MiB\n", ++n);
         }
 }

then gcc test.c -o test && ./test and top -d $(pgrep test) in another shell, you will note rss is increasing crazily!

Chen Li
  • 4,824
  • 3
  • 28
  • 55
  • 2
    It may not be the allocated memory itself, but the structures that malloc uses to keep track of stuff that are growing. Stop it after allocating a certain large amount, compare the RSS with the amount of memory you requested. It should grow proportionally. (without demand paging/overcommit it would be 1:1) –  Apr 11 '21 at 15:41
  • Page tables are another source of overhead. When I ran this code, it got up to a supposed allocation of about 1 TB, with RSS around 4 GB. At least half of that is directly explainable by page tables: one 8-byte page table entry per 4KB page. Huge pages aren't an option because your allocations are smaller than a huge page, and the OS deliberately leaves an unmapped page between them so they can't be coalesced. – Nate Eldredge Apr 11 '21 at 18:20

0 Answers0