0

I have created the bellow small c program which allocates 50Mb of memory using malloc() and then using a loop to "touch" every page in order to make it residence.

int main ()
{
  // Map 50M to RAM
  unsigned char *p = malloc(52428800);
  sleep(5);

  // Touch every page 
  for (int i = 0; i< 52428800; i+=4096)
    p[i] = 1;    
  sleep(100000);
}

Indeed the program seems to work, meaning that after the first 5 second sleep, the resident memory on the "top command" output starts to fill up and eventually allocating all Virtual Memory to RAM.

PID   %MEM    VIRT    RES   CODE    DATA   SHR                                             
32486  0.6   55396   52360     4   51528   1104   

I noticed the page faults of the program and there are only minor ones:

ps -ef -o min_flt,maj_flt 32486 

MINFL  MAJFL
12879      0

Shouldn't there be major page faults? As far as i understood, when i use malloc(), a virtual address space of 50Mb is created. Prior writing on each virtual page, the actual residence size is very small, but after is equal to the virtual memory requested.

When i "touch" the pages (in order to make them residence), then each page is moved from disk to DRAM, right? Why there aren't any Major Page faults then?

Also, when you malloc() for 50m and you noticed at the residence size, there are only a few Kbytes, where are the rest pages? are they on the disk?

giomanda
  • 177
  • 9

1 Answers1

0

The operating system could create demand-zero pages in support of your malloc calls. You have done thing here to force a read from disk.

You might want to try a second loop after the pages are modified to see if that causes page faults.

user3344003
  • 20,574
  • 3
  • 26
  • 62