See what is filling your Mac's swap, and get it back.
One bash file. No dependencies. Safe by default. Built for people and agents.
sudo curl -fsSLo /usr/local/bin/swapclean https://raw.githubusercontent.com/codeclawd/swapclean/main/swapclean
sudo chmod +x /usr/local/bin/swapcleanswapclean # how much swap, who holds it, can macOS clean it now?
swapclean clean # pick holders to free from a numbered list
swapclean reclaim # watch macOS clean up the dead spaceEnding a process frees its memory at once, but the swap files often stay the same size. The freed slots stay inside them as dead space.
macOS cleans up dead space by itself, but only while nothing is being
swapped out. In the kernel source
(vm_compressor_backing_store.c,
vm_swap_consider_defragmenting), both cleanup jobs are skipped while the
Mac is swapping out (VM_SWAP_BUSY()). There is no command to force them.
The only code path that forces them is the memory freezer
(CONFIG_FREEZE), which is off on macOS
(sysctl vm.compressor.config.freezer_swap_enabled reads 0).
So the method is:
- Check the swap-out rate with
swapclean. - Free memory with
swapclean cleanif the rate is above 0. - Watch the kernel reclaim the space with
swapclean reclaim.
On the test Mac, swap fell from 22.1 GB to 17.8 GB, and the swap files went from 23 GB to 20 GB, while swap-outs were at 0. When swap-outs came back, the cleanup stopped. A restart is still the only instant full reset.
| Command | What it does |
|---|---|
swapclean |
Show swap used, the swap-out rate and the top 10 holders |
swapclean status --json |
The same, as JSON |
swapclean clean |
Pick holders to free from a numbered list |
swapclean clean 1 3 |
Free list items 1 and 3 without a prompt |
swapclean free PID... |
Free these PIDs without a prompt |
swapclean reclaim [SEC] |
Watch macOS clean dead swap space (default 300 s) |
Each holder is freed according to what it is:
| Tag | What happens |
|---|---|
service |
A launchd job, restarted with launchctl kickstart -k. It comes back by itself. |
app |
A .app, sent a normal Quit, so it can ask you to save. |
process |
Anything else, sent SIGTERM, a polite stop. |
protected |
Never touched. |
A process is protected if it, any of its parents, or the service it
belongs to matches the protect list. The default list is OrbStack,
Docker, kernel_task, WindowServer and launchd. Set
SWAPCLEAN_PROTECT to a regex to change it.
swapclean never deletes /System/Volumes/VM/swapfile*. Deleting those
files by hand can crash the Mac.
Use status --json to read the state and free PID... to act. A PID
stays valid between the two calls. A list rank can shift.
$ swapclean status --json
{"swap_used_mb":21856.56,"swap_total_mb":22528,"swapouts_per_s":0,"holders":[
{"rank":1,"pid":81352,"cmprs":"3515M","kind":"service","name":"node"}, ...]}
$ swapclean free 81352| Exit code | Meaning |
|---|---|
0 |
OK |
1 |
Bad usage |
2 |
A PID was missing, protected, or could not be freed |
No command prompts except clean without arguments.
- The
CMPRScolumn counts compressed memory in RAM and in swap together, because macOS does not report swap for each process. - Freeing a holder drops RAM pressure right away. The swap number can lag until the kernel reclaims the dead space.
./test.shThe tests run against your live system. They only stop processes they start themselves.