Demand paging is the Linux method of allocating physical memory to its processes. This is a just-in-time or lazy method as processes are granted memory right before they require it

Demand paging is handled by the Linux Memory Management Subsystem (MMS)

The MMS is a record of all of the virtual addresses which have not been allocated a physical memory space yet. Therefore, when a process tries to access one of these uninitialized memory locations a page fault occurs, and the system allocates the memory

Copy on Write

Child and parent processes have distinct virtual memory spaces but both map to the same physical addresses.

When a write operation occurs, the system dynamically allocates a fresh frame (set of pages) for the process.

Copy on Write (COW) makes use of the methods of page protection and page fault handling detailed previously

COW defers the copying of a resource until the first write operation is required. This concept is used in various system processes, including forking of processes and implementation of snapshotting features in filesystems.

When a new process is forked in many operating systems, it initially shares the same memory pages as its parent to save resources. Only when one of the processes attempts to write to these shared pages, the operating system intervenes and creates a new copy of the page for the writing process, allowing modifications without affecting the original page. This mechanism saves memory and reduces the system load since unnecessary copying is avoided.

Benefits

It increases efficiency, since both the parent and child process can share pages up until a write operation is performed

Out of Memory Killer (OOM-killer)

Worst-case scenario no sufficient memory the kernel will run a killer process: OOM (Out-of-Memory) Killer

Terminate processes and freeup resources The ‘rules’ for the OOM-killer:

  • The kernel requires enough memory to run its own processes
  • Try and free up a large amount of memory
  • Try and minimise the number of processes terminated

Running processes are given a score and the highest is terminated.