Why does my Mac use so much swap space? Examining htop on iTerm2, I'm finding that my machine is maxing out swap space while leaving ~4GB of normal RAM. Why?! This seems like it's going to really slow up my machine. What's going on here? Is there something I can do to improve the distribution?
When your computer runs out of physical RAM, it swaps some of the data in RAM out to disk.
Later when the need for RAM decreases, you will have a situation where you have free RAM, but still an amount of data swapped out to RAM.
You could argue that the operating system should start swapping that data back into RAM immediately. However there are arguments that this is not a good strategy, namely because:
a) swapping in resources takes away resources from other programs running
b) swapping in resources will occupy RAM that another program might need in a short while, provoking another swap out
These are reasons why the swapped out data isn't immediately swapped in when RAM is freed. Instead it is swapped in when needed.