What is page fault?

Asked

Viewed 4,142 times

11

I was reading the reply of the user Maniero regarding overlay and memory management. He cited the page fault which can occur when a program is running. However, this term page fault caused me some doubts.

Doubts

  1. What is a page fault?
  2. When does the page fault?
  3. The page fault may influence the performance or operation of my application?
  • 1

    I think you’ve already seen this, but just in case:A page failure or page failure (page fault), in the context of computer memory technology, is an interruption (or exception) triggered by the hardware when a program accesses a page mapped in the virtual memory space, but which was not loaded into the physical memory of the computer. https://pt.wikipedia.org/wiki/Falta_de_p%C3%A1gina

  • in English https://blogs.technet.microsoft.com/askperf/2008/06/10/the-basics-of-page-faults/

1 answer

8


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 usually more limited). You can access all of this even if you don’t have enough RAM. It uses some mechanism to handle the surplus. As far as I know all play on secondary storage (HDD, SSD, etc.) which 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.

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

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

Reasons for memory loss

Obviously, if you try to access an address that is on a page marked as outside of the RAM, the operating system will have to find it in the "disk", put it in the RAM and only then let the application access. That’s the page fault. Failed to access the memory directly. Obviously this process is absurdly slower than accessing the 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).

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

I did not enter the merit of faults that are faults and that cannot be solved by the operating system, as the (semi)permanent protections.

Consequences

That’s why it’s very difficult to manage memory. That’s why languages with GC may not be so slow in general use, they help avoid page faults. On the other hand the collections help increase these faults. It is a complicated thing to balance.

The less 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 takes a lot of work and nothing is guaranteed. To minimize even more boat lots of RAM. And an SSD helps a lot when they occur. It seems that with NVRAM secondary storage will come close to the current RAM speed. This helps, but it’s still better not to miss.

Guess what happens if you try to access something that you 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 generated for the application to treat, if possible. Do you know why today you hardly see any more GPF? Because people use languages that treat it differently 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 like that, but it’s something normal.

It is an exception on an internal level of the operating system.

Open Task Manager or a better utility like Process Explorer and monitor faults. Below is a list of processes with your faults at that time. There are two processes that are increasing by 4 or 5 per second. One of them is Firefox. For privacy I didn’t 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.

  • The values shown in the tab Page fault would be the amount of missing page for each application (process)?

  • 1

    @cat put in answer.

  • I was waiting for your answer

  • @bigown , in C, mallocadores can make drastic differences as to page fault? We are studying to use the jemalloc , and page fault was not in our field of vision

  • 2

    @Jeffersonquesado absurd differences, I even quote in the answer generically how it ends up being important. Memory today has more importance than speed processing of most things. Specifically I do not know, I would have to study each allocator. It is a very complicated discipline.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.