Why does the program show so many page-in events although I have enough memory and no paging appears to occur?

All modern operating systems use virtual memory management. In the hardware of the processor, and hence in all applications, memory is not being addressed by real RAM addresses, but only virtual ones are used. All operations take place in virtual memory space. This virtual memory is mapped to addresses of real memory on demand, and each individual process is working in its own virtual space, strictly separated from all others. If not enough main memory is available to be mapped onto all virtual memory spaces for a given workload, blocks of memory which are currently not being used urgently will be moved from main memory to hard disk to make room available. If such an outsourced block is being accessed later, it will be read back from hard disk to main memory. In case there is still not enough room for that, another currently not needed block is selected and moved out. The blocks swap places. The whole technique is known as paging. The reserved area on the hard disk which is used for swapping is called swap space.

When looking at the statistics in the section memory of System Monitor, you’ll notice that many page-ins —and in some cases also some page-outs— are being counted, even if there is enough RAM in the computer and no swap space has been used. At first sight, this seems to be a contradiction.

This effect can be explained by the fact that modern operating systems like macOS are using page-in and page-out operations for other purposes as well, in particular for high-speed access to normal files. If, for example, a 4 MB file should be read by an application, this program can pretend that the whole contents of the file would already be lying in virtual memory space. When accessing individual bytes of this file, no “conventional” file read operations are necessary. Instead, the virtual memory management in the processor’s hardware notices that the actual data is missing, and activates the part of the operating system kernel which is responsible for paging memory blocks in. The file is being read block by block using the highly optimized memory management parts of the system kernel as if this would be parts of the swap space. This technique is called memory-mapped files. In addition to reading files, it is also being used to write files in some cases.

The system does not differentiate if page-in/page-out operations are related to swap space or to normal files. So the statistics counters can show high values even if no “real” paging occurs.