What is page fault?

Was reading the Response from user Maniero regarding overlay and memory management. He cited the page fault that can occur when a program is running. However, this term page fault gave me some doubts.

Doubts

  1. what is a page fault ?
  2. when does the page fault occur?
  3. page fault may influence performance or operation of my application?
Author: Comunidade, 2017-05-24

1 answers

page fault is when an application asks to access a memory address and it is not mapped in RAM(it is a bit more complex than this, but let's simplify).

Virtual Memory

Modern operating systems use a virtual memory system, so you have up to 4GB in 32 bits and theoretically up to 16eb in 64 bits (in practice it is often more limited). You can access all this even if you don't have enough RAM. It uses some mechanism to deal with the surplus. As far as I know everyone plays on secondary storage (HDD, SSD, etc.) what is not in use.

The problem is that the application cannot directly access something outside of RAM. Then the operating system needs to load what is in the secondary to the primary (RAM). Most likely putting on top of something that was in use before.

For ease of work this is divided into pages, usually of 4KB each (there are larger pages, but it does not come to case). So when you access an address in the application it is a fake address, it is virtual. There is a translation (the processor does this at the request of the operating system, so it has practically no cost) from this address to the actual physical address in memory. That's why moving data can cost very cheap since it only needs to change the page table and not mess with the memory in fact, more or less as it happens with the table of files on disk.

For example, an executable can be all loaded to memory or not, it depends on the operating system, how much has free memory. It is common only the pages with code needed to go loading. But if you have free memory you can load everything even to serve as a cache. The load may not be done at the time of the executable load until to optimize its initial load. It depends on a number of factors.

Reasons for memory failures

Obviously if you try to access an address that is on a page marked as outside the RAM the operating system will have to pick it up on the "disk", put in RAM and only then let the application access. This is the page fault. Failed to access memory directly. Obviously this process is absurdly slower than accessing RAM directly.

The page may be unavailable at that time for some specific protection.

The page has a trigger when trying to write to it and a copy needs to be made (copy on write).

To Page it is mapped but needs to be treated for use. To make quick allocations a mapping is done only logically and only when there is effective use will it be properly prepared.

I did not get into the merit of the faults that are failures and that can not be solved by the operating system, such as the (semi)permanent protections.

Consequences

So it is very difficult to manage memory. Because languages with GC may not be so slow in general use, they help avoid page faults . On the other hand, collections help increase these shortcomings. It's a tricky balancing act.

The fewer page faults you have, the faster your application will run. And you can't control that much. In some languages it is possible to minimize a little, at least unnecessary faults, but it gives a work and nothing is guaranteed. To minimize even more boat a lot of RAM. And an SSD helps a lot when they occur. It seems that with NVRAM the secondary storage will come close to the current RAM speed. It helps, but it's still better not to have fouls.

Guess what happens if you try to access something that shouldn't, something that can't be mapped? The application breaks, the famous general Protection Failure (GPF) or some more specific protection failure. Or an exception is raised for the application to deal with, if possible. Do you know why today you hardly see GPF anymore? Because people use languages that treat it in a way different or the programmer has learned to deal with it in a nicer way.

A lot of people think this is bad memory chip, operating system error, or something, but it's something normal.

It is an exception at an internal operating system level.

Open Task Manager or a better utility like Process Explorer and monitor for faults. Below is a list of processes with their faults at that time. It has two processes that are increasing a 4 or 5 per second. One of them is FireFox. For privacy I did not copy the columns that can identify something:

Task Manager page faults

Want to learn more? Windows shows all its operation in two books Windows Internals.

 8
Author: Maniero, 2020-12-03 13:41:28