Alright, let’s talk about vm.swappiness. This is the knob the kernel gives you to express your opinion on how much it should like using its swap file or partition. The key word here is opinion. You’re not giving it orders so much as strongly suggesting its drinking habits.

The swappiness value isn’t a percentage of memory to use for swap, which is a common and totally understandable misconception. Instead, it’s an abstract value from 0 to 100 that controls the kernel’s aggression in moving memory pages out to disk. A higher value means the kernel will be more eager to swap stuff out to make room for more disk cache in RAM. A lower value tells the kernel to hold onto application memory in RAM for as long as it possibly can, preferring to shrink the disk cache instead.

The default value on most Linux distributions is 60. I don’t know who decided on 60, but it feels like a committee compromise. It’s fine for a general-purpose desktop where you want a snappy feel when opening new applications, even if it means occasionally swapping out idle stuff. For most servers, it’s borderline psychotic.

How the Kernel Makes the Choice

Think of the kernel’s memory manager as constantly calculating a “score” for how badly it needs to free up some RAM. It’s looking at two main things: the amount of free memory and the amount of “inactive” memory—that’s memory that’s allocated but hasn’t been used recently. Swappiness directly influences this calculation.

A high swappiness value lowers the threshold for deciding that inactive file-backed memory (like cached files from disk) should be kept, and that inactive anonymous memory (like your application’s heap) should be swapped out. A low swappiness value raises that threshold, making the kernel say, “Nah, let’s just ditch some of these cached files from the disk and keep the application memory safe in RAM.”

When to Crank It Down (or to Zero)

You should seriously consider lowering swappiness if you’re running a performance-sensitive server workload—think a database (PostgreSQL, MySQL), a large Java application, or a caching server like Redis. These applications have their own sophisticated memory management, and the kernel swapping out parts of them is like you “helping” me cook by putting my steak in the freezer. You’re not helping.

For these, I often set vm.swappiness to a very low value, like 1 or 10. This tells the kernel to avoid swap until it’s absolutely, positively desperate.

And what about setting it to 0? The man page will tell you that a value of 0 doesn’t disable swap; it just tells the kernel to avoid swapping as long as there is enough free memory and reclaimable page cache. In practice, on modern kernels, it acts very much like disabling swap. The problem is, if memory pressure gets severe enough, the kernel will eventually ignore your polite request and start swapping anyway to avoid an Out-of-Memory (OOM) kill. If you truly want to disable swap, you need to use swapoff -a, but be warned: do that on a machine with no free memory and you’re inviting the OOM killer to a murder spree.

How to Tune It Live and Make It Stick

You can test a new value on the fly. This is great for experimentation without committing.

# Set swappiness to 10 immediately
sudo sysctl vm.swappiness=10

To check your current value:

cat /proc/sys/vm/swappiness

To make it permanent across reboots, add a configuration file. Don’t just edit random existing files in /etc/sysctl.conf; the package manager owns those. Create your own.

# Add your specific tuning to a new file
echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.d/99-my-tune.conf

Then, to apply all your custom settings (now and after reboot):

sudo sysctl --system

The Big Pitfall: The “No Swap” Zealot

Some people see any swap usage as a failure and aim for zero. This is a fantastic way to get your most important process shot by the OOM killer. The kernel uses swap as a safety valve. Without it, when memory gets completely full, the kernel’s only option is to invoke the OOM killer, which picks a process—often a critical one—and terminates it to free memory. It’s a blunt instrument. A small amount of swap, even if it’s rarely used, gives the kernel room to breathe and clean up memory gracefully. The goal isn’t zero swap usage; the goal is to avoid excessive and performance-degrading swap usage. There’s a massive difference.

So, don’t be a zealot. Be a pragmatist. Lower swappiness on your important servers, monitor your swap usage (free -h, vmstat 1), and make sure you have enough RAM for your workload. The kernel is smart, but sometimes you need to give your brilliant friend a nudge in the right direction.