There is 4.49 GiB of swap in use on my Mac. On a machine with 24 GB of RAM that number
invites a certain kind of worry: either too many things are open, or something is refusing
to let go of memory.
The Memory tab in Activity Monitor carries this figure on its "Swap Used" row, and Apple's
guide defines the row like this: the amount of space being used on the startup disk to swap
unused files to and from RAM. The definition is clear, and it points the reader in a
comforting direction: whatever lands there is something that lost its seat in RAM.
This post takes one corner of that definition apart. Part of what gets written to swap never
comes from RAM at all: it is read out of swap and written back into swap. On this machine,
one in eight pages written to swap since boot belongs to that category. The source and the
counters are both below.
Every figure here comes from the command line rather than from the rounding Activity
Monitor prints, straight out of sysctl. Everything here comes from a single machine: M4 Pro, 24 GB of memory, macOS 26.6.2 (25G83),
kernel xnu-12377.161.14. The counters have been accumulating since the boot at 08:56 on
5 November; the reading was taken at 16:41 on 17 November, a window of 12 days 7 hours
44 minutes.
Six files, and the fifth is missing
macOS keeps swap on its own volume, so the files are easy to find.
$ ls -la /System/Volumes/VM/
total 12582912
drwxr-xr-x 8 root wheel 256 Nov 17 05:07 .
drwxr-xr-x 14 root wheel 448 Aug 13 05:51 ..
-rw------- 1 root wheel 1073741824 Nov 7 17:34 swapfile0
-rw------- 1 root wheel 1073741824 Nov 8 18:12 swapfile1
-rw------- 1 root wheel 1073741824 Nov 16 15:03 swapfile2
-rw------- 1 root wheel 1073741824 Nov 10 10:11 swapfile3
-rw------- 1 root wheel 1073741824 Nov 16 15:03 swapfile4
-rw------- 1 root wheel 1073741824 Nov 16 15:03 swapfile6
Six files, each exactly 1 GiB. The numbers run 0, 1, 2, 3, 4 and 6; swapfile5 is not
there. Both the size and the naming are written down in the kernel source: on the macOS
branch MAX_SWAP_FILE_SIZE is 1 GiB, MIN_SWAP_FILE_SIZE is 256 MiB, and the path is
/System/Volumes/VM/swapfile. The number appended to a new file is whatever
vm_num_swap_files happens to be at that moment.
But the hole is more than that, and the source corrects the obvious reading. When the kernel
drops a swap file it deletes the file, yet it does not take the structure describing that
file out of its list; it parks it with a SWAP_REUSE flag. The comment in the source says
why in plain words: the structure stays in the queue so it can be re-used when needed and so
the namespace is preserved. When a new swap file is required, the kernel scans that parking
lot first, and if it finds a parked structure it generates no new name at all: it reopens
the preserved path.
So the absence of swapfile5 is not a bookkeeping accident; it is the receipt of a reclaim.
The name is still reserved, the file is gone. It also yields a testable prediction: the next
swap file created on this machine will be named swapfile5, not swapfile7.
The hole has a timestamp, too: the directory's own modification time is 05:07 that same
morning. Something was created or deleted in there at that hour.
One more detail explains why every file is exactly 1 GiB. The kernel asks for 1 GiB first;
if the disk refuses, it halves the request and tries again, and it gives up rather than
going below 256 MiB. The halving loop runs on any preallocation failure, not only on a full disk. All six files
here are full size, which says preallocation never failed on this machine.
The df side shows the used space has shrunk, too:
$ df -h /System/Volumes/VM
Filesystem Size Used Avail Capacity Mounted on
/dev/disk3s6 460Gi 6.0Gi 104Gi 6% /System/Volumes/VM
On 30 September, in the post where five different tools gave five different answers about
free disk space, the Used column of that same
line said 8.0 GiB; Size reads 460Gi in both. The machine rebooted several times in
between, so these are two photographs rather than a trend. Both say the same thing: the size
of this directory is not fixed, and something in there both grows it and shrinks it.
Two counters, the same gap
The last two lines of vm_stat give the total swap traffic:
$ vm_stat | tail -2
Swapins: 847594.
Swapouts: 1520275.
The page size here is 16 KiB. So 1,520,275 pages = 23.20 GiB written to swap, and
847,594 pages = 12.93 GiB read back. Over twelve days, an average of 1.88 GiB per day.
The interesting part is how sysctl breaks the same numbers down by reason:
$ sysctl -a | grep 'compressor.swapper'
vm.compressor.swapper.swapouts_total: 1520275
vm.compressor.swapper.swapouts_pressure: 1326336
vm.compressor.swapper.swapouts_freezer: 0
vm.compressor.swapper.swapouts_donate: 0
vm.compressor.swapper.swapouts_scavenger: 0
vm.compressor.swapper.swapouts_darkwake: 0
vm.compressor.swapper.swapins_total: 847594
vm.compressor.swapper.swapins_pressure: 653655
vm.compressor.swapper.swapins_freezer: 0
vm.compressor.swapper.swapins_donate: 0
vm.compressor.swapper.swapins_scavenger: 0
vm.compressor.swapper.swapins_darkwake: 0
The gap between the total and the pressure bucket is 193,939 pages, and every other bucket
is zero. Now do the same subtraction on the read side: 847,594 − 653,655 = 193,939. The
exact same number, sitting inside two separate counters.
That is far too tidy to be a coincidence. A third counter names the owner:
$ sysctl vm.compressor_swapper_reclaim_swapins vm.compressor_swapper_defrag_swapins
vm.compressor_swapper_reclaim_swapins: 193939
vm.compressor_swapper_defrag_swapins: 10055
Reclaim. 193,939 pages, or 2.96 GiB, were read out of swap for the sake of reclaim. And
the same amount shows up on the write side.
The second line, 10,055 pages of defrag_swapins, belongs to defragmentation. That item
does not appear in the subtraction, because a segment read during defragmentation is written
back through the ordinary path and lands in the "pressure" column. So 12.76% is a floor: at
least that much of what goes to swap has nothing to do with what my programs asked for, and
the real share is a little higher.
A single loop in the source
A number like that deserves a look at the code rather than a guess. Apple publishes the
kernel source, and swap files are managed in osfmk/vm/vm_compressor_backing_store.c. In
the newest published tag (xnu-12377.121.6) the reclaim work lives in vm_swap_reclaim()
at line 2165, and the body of its loop goes like this:
- A segment still in use is found inside the file that is being emptied.
- The segment is read from disk, and at line 2327 the read counter goes up:
counter_add(&vm_statistics_swapins, c_size >> PAGE_SHIFT). Right next to it,vmcs_stats.reclaim_swapinsgoes up as well. - The segment is written into a different swap file via
vm_swap_put(); at line 2357 the write counter goes up:counter_add(&vm_statistics_swapouts, c_size >> PAGE_SHIFT). - Once every segment in the file is free, the file is closed and
vm_num_swap_filesdrops by one.
So the way to remove a swap file is to read what is inside it and write that data elsewhere
in swap. The two counters growing by the same amount is exactly this: every reclaimed
segment is one read plus one write.
The work in that loop is not a page descending to disk because RAM ran out. It is a move of
something that was already on disk to another place on disk. The bill sits in the same
counter: 2.96 GiB written plus 2.96 GiB read, 5.92 GiB of traffic in total. That is
12.76% of all pages written to swap, or 246 MiB per day on average.
The idle 1.5 GiB sits inside a band
The answer to when the kernel takes this on is in two thresholds. The source computes when reclaim starts and when
it stops from the swap file size (compressed_swap_chunk_size is 64 KiB on this machine,
which vm.compressor_segment_buffer_size confirms):
| Threshold | Formula in the source | Segments | In bytes |
|---|---|---|---|
| Start reclaiming | 1.7 × (1 GiB / 64 KiB) | 27,852 | 1.70 GiB |
| Stop reclaiming | 1.3 × (1 GiB / 64 KiB) | 21,299 | 1.30 GiB |
In other words, once free segments across the swap files exceed 1.70 GiB the kernel sets out
to close a file, and it stops when the free pool falls to 1.30 GiB. Here is the state at the
time of the reading:
$ sysctl vm.swapusage
vm.swapusage: total = 6144.00M used = 4602.50M free = 1541.50M (encrypted)
1,541.5 MiB free — 24,664 segments at 64 KiB each. Right between the two thresholds.
That is not proof that these thresholds govern this machine; it is a single instantaneous
reading. But a number that falls inside the band computed from the source, read together
with the hole in the file numbering and the directory's 05:07 timestamp, makes a consistent
picture.
The same file also has a defragment threshold: on the macOS branch, defragmentation kicks in
when sparse segments exceed a quarter of the segments in use. Here sparse is 2,363 and a
quarter of in-use is 18,410, so defragmentation's own gate is currently shut. The two jobs
are not independent, though: in the same loop, whenever reclaim is triggered defragmentation
is turned on as well and runs first. That is why defrag_swapins is not zero.
What share of the bill is it
This is the point to look the other way, because the instinct that "swap is bad" runs on
disk wear: you are writing to the SSD for nothing. The total written to disk is measurable
too; the IOKit block driver has been counting bytes since boot:
$ ioreg -rc IOBlockStorageDriver | grep -o '"Bytes (Write)"=[0-9]*'
"Bytes (Write)"=2160454037504
2.16 TB, or 1.965 TiB in binary units. Over twelve days, an average of 163 GiB per day. This
machine runs builds, containers and a virtual machine all day; the number is high but not
surprising.
Now put the two side by side: the 23.20 GiB written to swap is 1.15% of everything
written to disk. Reclaim's share is 0.15%.
| Item | Over 12 days | Per day | Share of all disk writes |
|---|---|---|---|
| Everything written to disk | 1.965 TiB | 163 GiB | 100% |
| Written to swap | 23.20 GiB | 1.88 GiB | 1.15% |
| Reclaim's part of that | 2.96 GiB | 246 MiB | 0.15% |
So swap is not what wears out the SSD on this machine. Looking at a 4.49 GiB swap line and
concluding "I need more memory" has no counterpart on the disk side. And yet the same
measurement surfaces an invisible item: 2.96 GiB of writing that no program asked for, done
purely to keep swap tidy. A small number, and a number with no owner.
It wrote 10 GiB it never read again
Look at those same two numbers as a subtraction: 1,520,275 writes, 847,594 reads. The
difference is 672,681 pages, or 10.26 GiB: data that went down into swap and never came
back up. Reclaim cancels itself out in this subtraction, since it inflates both counters
equally; defragmentation's 10,055 pages do not cancel, so the true gap is about 157 MiB
smaller.
Part of that 10.26 GiB is still in place: swapusage reports 4.49 GiB in use right now. The
remaining 5.77 GiB was released without ever being read: the owning process exited, or
the page came back by another route. Over twelve days that is less than half a gigabyte a
day; hardly a scandal. But the category is interesting: written to disk, wanted by nobody,
deleted.
It also shows why the swap line is hard to read. The 4.49 GiB there is not "what is spilling
out of memory right now"; some of it is spilling, some is left over from reclaim, and some
was written and forgotten. One number, three different stories.
The compressor does the heavy lifting
On macOS, swap is the last link in the chain. Ahead of it sits the memory compressor, and
Apple's guide explains that one to users as well: the memory of inactive apps is compressed
so that active ones get room. The counters show that this job dwarfs swap:
$ sysctl vm.compressor_input_bytes vm.compressor_compressed_bytes
vm.compressor_input_bytes: 9623335665664
vm.compressor_compressed_bytes: 3827692437504
8.75 TiB in, 3.48 TiB out: 2.51×. In twelve days, 8.75 TiB of data passed through the
compressor. The instantaneous picture: 2,038,249 pages (31.10 GiB) of logical data are the
compressor's responsibility; 1,241,156 of those pages (18.94 GiB) are in RAM and occupy
580,380 pages (8.86 GiB) of physical space there, 2.14× for the resident part. The
remaining 797,093 pages (12.16 GiB) live in the swap files.
That 12.16 GiB is a logical figure: the size before compression. Its physical counterpart on
disk is the 4.49 GiB that swapusage reports, which puts the compression ratio on the swap
side at 2.71× (I derived that by dividing the two). Put differently: on a 24 GB machine,
31 GiB of program data is carried by 8.86 GiB of RAM and 4.49 GiB of swap. Reading the swap line on its own means reading the bill for that
arrangement from a single item.
The compressor has housekeeping of its own, bigger than swap reclaim:
$ sysctl vm.compressor.compactor.major_compaction_bytes_moved \
vm.compressor.compactor.swapout_bytes_wasted
vm.compressor.compactor.major_compaction_bytes_moved: 1123602317312
vm.compressor.compactor.swapout_bytes_wasted: 1399963648
1.02 TiB of bytes moved around inside RAM to compact segments. And 1.30 GiB wasted while
writing to swap: the leftover room when a segment goes to disk before it is completely
full. Both are counted, and neither is charged to any application.
The Linux equivalent is not the same thing
This distinction came up before on the Linux side, in the post comparing zram with
zswap; the names rhyme, the
accounting does not. With zram the compressed pool looks like a block device and is counted
as swap in free; with zswap the pool is a cache in front of real swap. On macOS the
compressor is not a swap device at all but a layer inside the pager, and the swap files hold
its segments. So the intuition you carry over from Linux only walks so far: "swap is in use"
on macOS means "compressed segments went to disk", not bare pages.
Where the measurement tripped me up
ioreg returns two block drivers, and one of them is all zeros. In the first instance
every field of Statistics is 0; the second has the real numbers. diskutil list physical
shows a single physical disk on this machine (disk0, the internal APPLE SSD AP0512Z), so the
meaningful instance is the second. Take grep's first hit and you would conclude that nothing
has ever been written to disk.
The counters are cumulative, not rates. Two samples six minutes apart: Swapouts did not
move at all, Swapins went up by 36 pages. So every GiB above is a twelve-day total, and
reading it as "it writes this much per second right now" would be wrong. Reclaim is not a
continuous job either; it wakes when a threshold is crossed.
The published source is not exactly this kernel's source. Apple's newest published tag is
xnu-12377.121.6; this machine runs xnu-12377.161.14. The bucket names do not line up
exactly either: sysctl shows swapouts_pressure, while the reason list in the published
source has C_SWAPOUT_REG. Four of the five buckets match the source by name
(C_SWAPOUT_RIPE → scavenger, plus FREEZER, DONATE and DARKWAKE under their own
names); the only name that does not match is pressure, and pairing it with regular is my
inference. The numbers themselves are a reading, the pairing is not.
Two accountings differ by 369 segments. The "used" value from vm.swapusage is 4,602.5
MiB, which at 64 KiB per segment is 73,640 segments. The segment counters give 70,908 plus
2,363 sparse = 73,271. The difference is 369 segments, 23 MiB. I could not close it; segments
in flight being counted differently on the two sides is the most plausible explanation, but
tracing that through the source did not pan out. That is why the free-segment figure above is
derived from swapusage — and why I say it is derived.
Can I turn this off, and should I
Once the measurement is in hand the questions change. All four answers sit in the same file:
Will it slow me down? When the thread that drives reclaim is created, its I/O priority is
set to THROTTLE_LEVEL_COMPRESSOR_TIER2 and it is marked for passive I/O, so this
read-and-write traffic flows in the background at a throttled priority. In exchange the
thread is allowed to dip into the reserved page pool, because it needs memory to finish its
own cleanup. The design is built around staying out of your way.
Can I change the threshold? No. Both thresholds are computed at init from
MAX_SWAP_FILE_SIZE, and no tunable sysctl sits in front of them. The ceiling on the
number of swap files is fixed in the source too, at 100 on macOS. So the upper bound on swap
for this machine is 100 GiB.
When does it fire? When free segments go past 27,852. In user terms: close a large app,
free roughly 1.7 GiB inside swap, and the kernel sets out to retire a file, spending reads
and writes to do it. One detail keeps the cost bounded: the least-full file is the one
chosen for retirement, so the number of segments that have to move is the smallest available.
What about SSD life? Swap is 1.15% of total writes on this machine. At 163 GiB a day
that works out to 58 TiB a year. If I want to write less, the place to look is not swap but
the other 99%: build output, container layers, the virtual machine's disk. Which of them
writes how much is something I have not attributed per process; fs_usage needs
administrator rights, and no number in this post did.
Four commands for your own machine
None of this needs administrator rights; all four run as a plain user.
# 1. Swap files: how many, and is there a hole in the numbering
ls -la /System/Volumes/VM/
# 2. Total swap traffic (in pages; page size is on vm_stat's first line)
vm_stat | tail -2
# 3. The reason breakdown plus reclaim's share
sysctl -a | grep -E 'compressor\.swapper|swapper_reclaim_swapins'
# 4. Numerator and denominator for swap's share of total disk writes
sysctl vm.swapusage
ioreg -rc IOBlockStorageDriver | grep -o '"Bytes (Write)"=[0-9]*'
In the third command, look at the gap between the total and the sum of the buckets. If there
is a gap, that gap is not your programs.
Running those four every couple of weeks is the shortest path from a vague "I need more
memory" feeling to an actual number. Here the result was this: the 4.49 GiB swap line is not a
distress signal but the disk leg of a 2.51× compression arrangement. Buying more RAM on the
strength of that line would mean acting on the wrong number.
Knowing the band does not change the number, it changes the question
At the start of this post I had a single number: 4.49 GiB of swap. At the end I have a range.
The kernel tries to keep the free space across its swap files between 1.30 and 1.70 GiB;
both limits are written in the source as a single multiplication, and my free space at the
time of the reading (1,541.5 MiB) sits right between them. The hole on disk is that band's
receipt: swapfile5 deleted, its name reserved.
Once you know this, the swap line stops being a warning light. When the number climbs, the
question is not "what did I open?" but: where am I in the band, how many of these bytes came
down from RAM, and how many were already on disk. All three answers are in the four commands
above, none of which needs administrator rights.
One more thing stayed with me. Through all of this the kernel never hid its own housekeeping:
reclaim's reads go into their own counter, defragmentation's into another. What shows the
total and keeps the breakdown out of sight is not the kernel but the default view. In the
post where the process blowing the budget turned out not to own the
budget a second command
settled it as well; the difference is that there were two separate ledgers there, and here
there are two columns of one ledger.
What I am curious about now is whether the next swap file will actually be called
swapfile5. That is the one testable prediction in this post, and the machine will answer
it within a few weeks.
Official Sources
- View memory usage in Activity Monitor on Mac — Apple Support
- Check if your Mac needs more RAM in Activity Monitor — Apple Support
- xnu — osfmk/vm/vm_compressor_backing_store.c (xnu-12377.121.6)
- xnu — osfmk/vm/vm_compressor_backing_store_internal.h (xnu-12377.121.6)
- xnu — osfmk/vm/vm_compressor_xnu.h (xnu-12377.121.6)
Top comments (0)