Skip to content
toolcallsdevPublic

About

See what is filling your Mac's swap, and get it back. One bash file, safe by default, agent-friendly.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swapclean

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.

macOS bash dependencies license

swapclean showing 19.8 GB of swap, the top memory holders tagged protected, service, app and process, then a reclaim run

Install

sudo curl -fsSLo /usr/local/bin/swapclean https://raw.githubusercontent.com/codeclawd/swapclean/main/swapclean
sudo chmod +x /usr/local/bin/swapclean

Quick start

swapclean            # 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 space

Why swap stays high

Ending 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:

  1. Check the swap-out rate with swapclean.
  2. Free memory with swapclean clean if the rate is above 0.
  3. 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.

Commands

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)

Safe by default

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.

For agents

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.

Good to know

  • The CMPRS column 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

./test.sh

The tests run against your live system. They only stop processes they start themselves.

License

MIT

About

See what is filling your Mac's swap, and get it back. One bash file, safe by default, agent-friendly.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages