TExcelTAD
Home › Operating Systems: Linux & Windows › Swap & the Page File

Swap & the Page File

When RAM runs short, the kernel can move pages that nobody is using right now out to disk and bring them back on demand. Linux calls that disk area swap; Windows calls it the page file (C:\pagefile.sys). The idea is the same, but the two systems account for memory very differently, and that difference explains why a Linux box can suddenly kill a process (the OOM killer) while Windows refuses an allocation with "out of memory" instead.

Which pages go to disk?

Only anonymous pages need swap: heap, stack and private copy-on-write data that exist nowhere else. Pages that came from a file (program code, DLLs, shared libraries, cached file data) are simply dropped and re-read from the file later. So swap/page-file usage is about your programs' data, not about executables.

text
                     RAM full, kernel needs a frame
                                 |
              +------------------+-------------------+
              |                                      |
        file-backed page                       anonymous page
   (code, .so/.dll, page cache)              (heap, stack, COW data)
              |                                      |
     clean: drop it                       write it to swap / pagefile.sys,
     dirty: write back to its file        keep its location in the page-table
                                          entry (marked "not present")
              |                                      |
      next access: major fault,           next access: major fault,
      read from the file                  read from swap / the page file

Swap on Linux

Linux can swap to a dedicated swap partition or to a swap file on a normal file system, and it can use several at once with priorities. Typical setup commands (they need root, so they're shown, not run):

bash
ada@ubuntu:~$ sudo fallocate -l 4G /swapfile
ada@ubuntu:~$ sudo chmod 600 /swapfile
ada@ubuntu:~$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=6f1c0e1a-5d3b-4c8e-9a1f-2b7d4e5c6a90
ada@ubuntu:~$ sudo swapon /swapfile
ada@ubuntu:~$ swapon --show
NAME      TYPE      SIZE   USED PRIO
/swapfile file        4G     0B   -2
/dev/zram0 partition 7.7G 412.5M  100
ada@ubuntu:~$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

(Sample output; yours will differ.) The active swap areas are listed in /proc/swaps. That file is not per-container, so this example prints only its header:

bash · try it yourself
head -n 1 /proc/swaps | tr -s '\t ' ' '
grep -E '^(Swap|Commit)' /proc/meminfo | cut -d: -f1
Filename Type Size Used Priority
SwapCached
SwapTotal
SwapFree
CommitLimit
Committed_AS

Knobs that matter, all under /proc/sys/vm/ (or sysctl vm.<name>):

bash · try it yourself
ls /proc/sys/vm | grep -E '^(swappiness|overcommit_|page-cluster|min_free_kbytes)'
min_free_kbytes
overcommit_kbytes
overcommit_memory
overcommit_ratio
page-cluster
swappiness
Setting Meaning
vm.swappiness 0–200, default 60: how readily anonymous pages are swapped compared with dropping file cache. Low values favour keeping program data in RAM
vm.overcommit_memory 0 = heuristic (default), 1 = always allow, 2 = strict accounting against CommitLimit
vm.overcommit_ratio In mode 2: CommitLimit = swap + RAM × ratio / 100
vm.page-cluster How many pages are read from swap at once (2n)

Modern distributions often add compressed swap in RAM: zram is a compressed block device used as a high-priority swap device (Fedora and Chrome OS enable it by default), and zswap is a compressed cache in front of a disk swap device. Compressing a page is far faster than writing it to an SSD.

The page file on Windows

Windows keeps pagefile.sys in the root of the system drive, "system managed" by default (it grows and shrinks within limits based on RAM and crash-dump settings). There is also swapfile.sys, used to swap out whole Store (UWP) apps, and hiberfil.sys, which holds the RAM image for hibernation and Fast Startup (that one is not paging).

cmd
C:\Users\ada>dir /a:h C:\ | findstr /i "sys"
10/17/2026  08:02 AM     6,744,186,880 hiberfil.sys
10/17/2026  08:02 AM     4,294,967,296 pagefile.sys
10/17/2026  08:02 AM        16,777,216 swapfile.sys
powershell
PS C:\Users\ada> Get-CimInstance Win32_PageFileUsage | Format-List Name, AllocatedBaseSize, CurrentUsage, PeakUsage

Name              : C:\pagefile.sys
AllocatedBaseSize : 4096
CurrentUsage      : 312
PeakUsage         : 655

PS C:\Users\ada> (Get-CimInstance Win32_ComputerSystem).AutomaticManagedPagefile
True

(Sample output; the Win32_PageFileUsage sizes are in MB and yours will differ.) The GUI is System Properties → Advanced → Performance Settings → Advanced → Virtual memory.

Commit charge vs overcommit

This is the real difference between the two systems.

  • Windows counts every committed page (private memory a process has asked for, whether touched or not) in the commit charge. The commit limit is roughly RAM + page file size. When the charge would exceed the limit, VirtualAlloc fails (the program sees an out-of-memory error) or Windows grows a system-managed page file. Every committed page is guaranteed a home, so Windows never has to kill anybody.
  • Linux by default overcommits: malloc of 10 GB succeeds on a 4 GB machine as long as the request isn't absurd, because most programs never touch everything they allocate (and fork would otherwise double every process's commitment). Memory is only really used on first touch. If RAM and swap then run out, the kernel has no allocation to fail, so the OOM killer picks a victim (highest oom_score, adjustable via /proc/<pid>/oom_score_adj) and kills it with SIGKILL. Containers and systemd units hit the same thing at their cgroup memory.max.

The Linux counters with the same meaning as Windows' are Committed_AS (charge) and CommitLimit (only enforced in mode 2). This simulation runs the same sequence of allocate and touch requests under both policies:

python · try it yourself
RAM, SWAP = 8, 4                     # GiB
LIMIT = RAM + SWAP                   # commit limit (Windows, or Linux in strict mode)

events = [("browser", "alloc", 5), ("browser", "touch", 3),
          ("database", "alloc", 6), ("database", "touch", 6),
          ("build", "alloc", 4), ("build", "touch", 4),
          ("browser", "touch", 2)]

def run(policy):
    committed, used = 0, 0
    procs = {}                       # name -> [committed, touched]
    print(f"--- {policy} ---")
    for name, op, gib in events:
        if op == "alloc":
            if policy == "windows" and committed + gib > LIMIT:
                print(f"{name:<8} alloc {gib} GiB -> FAILED (commit {committed}+{gib} > limit {LIMIT})")
                continue
            procs.setdefault(name, [0, 0])[0] += gib
            committed += gib
            print(f"{name:<8} alloc {gib} GiB -> ok   (commit charge {committed} GiB)")
        else:
            if name not in procs:
                print(f"{name:<8} touch {gib} GiB -> skipped (allocation failed earlier)")
                continue
            procs[name][1] += gib
            used += gib
            note = f"in use {used} GiB of {RAM + SWAP} (RAM+swap)"
            if used > RAM + SWAP:    # only possible with overcommit
                victim = max(procs, key=lambda p: procs[p][1])
                freed = procs.pop(victim)
                used -= freed[1]
                committed -= freed[0]
                note += f" -> OOM killer kills {victim}, frees {freed[1]} GiB"
            print(f"{name:<8} touch {gib} GiB -> {note}")
    print(f"survivors: {', '.join(procs)}")

run("windows")
run("linux-overcommit")
--- windows ---
browser  alloc 5 GiB -> ok   (commit charge 5 GiB)
browser  touch 3 GiB -> in use 3 GiB of 12 (RAM+swap)
database alloc 6 GiB -> ok   (commit charge 11 GiB)
database touch 6 GiB -> in use 9 GiB of 12 (RAM+swap)
build    alloc 4 GiB -> FAILED (commit 11+4 > limit 12)
build    touch 4 GiB -> skipped (allocation failed earlier)
browser  touch 2 GiB -> in use 11 GiB of 12 (RAM+swap)
survivors: browser, database
--- linux-overcommit ---
browser  alloc 5 GiB -> ok   (commit charge 5 GiB)
browser  touch 3 GiB -> in use 3 GiB of 12 (RAM+swap)
database alloc 6 GiB -> ok   (commit charge 11 GiB)
database touch 6 GiB -> in use 9 GiB of 12 (RAM+swap)
build    alloc 4 GiB -> ok   (commit charge 15 GiB)
build    touch 4 GiB -> in use 13 GiB of 12 (RAM+swap) -> OOM killer kills database, frees 6 GiB
browser  touch 2 GiB -> in use 9 GiB of 12 (RAM+swap)
survivors: browser, build

Windows refused the third allocation up front, so build could react (show an error, use less memory) and nothing else was harmed. Linux let every allocation through and only discovered the shortage when pages were touched; then it had to kill the process using the most memory: database, even though build caused the shortage. Setting vm.overcommit_memory=2 makes Linux behave like Windows.

Sizing advice

Situation Linux Windows
Desktop with plenty of RAM A small swap (a few GiB) or zram; lets the kernel park idle pages Leave the page file system-managed
Hibernation Swap ≥ RAM (the image is written to swap) Separate hiberfil.sys, nothing to do
Crash dumps kdump reserves its own memory A page file is needed for kernel memory dumps
Servers / databases Small swap, low swappiness; or strict overcommit System-managed or fixed size; watch commit charge
Kubernetes nodes Swap traditionally off (now optional with NodeSwap) Not applicable

Tip: "Swap is in use" is not a problem by itself; the kernel moved idle pages out to make room for cache. The warning sign is constant swap-in activity (si/so columns of vmstat 1 on Linux; Pages Input/sec in Windows Performance Monitor): that is thrashing.

bash
ada@ubuntu:~$ vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 412340 812004  98320 4120552    0    0    12    40  410  820  4  1 95  0  0
 0  0 412340 811740  98320 4120560    0    0     0     0  380  760  2  1 97  0  0
 0  0 412340 811512  98320 4120560    0    0     0    16  395  790  3  1 96  0  0

(Sample output.) Here swap is in use (swpd) but si/so are zero: healthy.

Learn more: Page Replacement · Memory Management · Linux vs Windows

Test yourself

Which pages need swap space or the page file when they are evicted?

  1. Program code
  2. Shared libraries and DLLs
  3. Anonymous pages such as heap and stack
  4. Cached file data

On Windows, what is the commit limit approximately equal to?

  1. The size of RAM
  2. RAM plus the page file size
  3. The page file size only
  4. The size of the system drive

In Linux's default overcommit mode, what happens when RAM and swap are exhausted by pages that programs actually touch?

  1. malloc returns NULL
  2. The OOM killer terminates a process
  3. The kernel grows the swap file
  4. The system hibernates