• Indonesian
  • English
  • Linux Virtual Memory Swappiness Tuning: Complete Guide 2026

    Kecepatan:
    ⏱ 8 min read

    Alright, let’s talk about something that’s quietly ruining your server’s performance — and you might not even know it. vm.swappiness. It’s that one little kernel parameter that controls how aggressively your Linux system shoves things into swap space. And honestly? Most people never touch it. That’s the problem.

    tune Linux swappiness virtual memory

    Difficulty: Intermediate
    Last Updated: August 2026
    Tested On: Ubuntu 22.04 LTS, Debian 12, CentOS 7/8, AlmaLinux 9, Rocky Linux 9, Kernel 5.x-6.x

    What’s Swappiness and Why Should You Care?

    So here’s the deal. Every Linux system has this thing called virtual memory — it uses a combination of physical RAM and disk-based swap space to handle memory allocation. The kernel needs to decide when to start moving data from fast RAM to slow disk swap. That decision is controlled by vm.swappiness.

    By default, most distros set this to 60. That’s a balanced setting that works fine for desktops where you’ve got a mix of interactive and background processes. But for servers? It’s often too aggressive. A value of 60 means the kernel starts pushing pages to swap way before it absolutely needs to. And swap on a disk — even an NVMe — is orders of magnitude slower than RAM. We’re talking nanoseconds vs. milliseconds. When your database query takes 200ms instead of 2ms because the working set got swapped out, your users notice. Your monitoring alerts go crazy. And you’re left wondering what happened.

    I’ve seen this cause some really nasty production issues. One time, a client’s e-commerce site was intermittently slow during peak hours. Load average was fine, CPU was fine, but disk I/O was through the roof. Turns out, their PostgreSQL instance had been swapped out because nobody ever touched the default swappiness setting. Once we set it to 10, the problem literally disappeared. Ten minutes of config change, hours of debugging avoided.

    Understanding the Values

    Here’s something a lot of people get wrong. Swappiness doesn’t go from 0 to 100. It goes from 0 to 200. Yeah, 200. Most people only ever see 0-100 in tutorials, but the kernel actually supports up to 200. Values above 100 tell the kernel to prioritize swap even more aggressively than RAM — useful in some edge cases but not something you’d normally set on a server.

    At the common range of 0-100:

    • 0: Kernel avoids swap as much as possible, only swapping as an absolute last resort under extreme memory pressure
    • 1-10: Minimal swapping. Good for servers where you want to maximize RAM usage
    • 30-60: Balanced. Default 60 falls here. Fine for desktops, often too aggressive for servers
    • 70-100: Aggressive swapping. Basically tells the kernel to use swap early and often

    Check Your Current Setting

    Before you change anything, know where you stand:

    cat /proc/sys/vm/swappiness

    Or the slightly more readable version:

    sysctl vm.swappiness

    Now also check your actual swap usage:

    free -h

    If your swap used is creeping up toward swap total, and you’ve got available RAM sitting idle, that’s your smoking gun. The kernel is being too eager to swap things out.

    3 Ways to Tune Swappiness

    Method 1: Runtime (Lost on Reboot)

    Great for testing. Applies immediately:

    sudo sysctl -w vm.swappiness=10

    Done. Active right now. But reboot and it’s gone. Use this to test the waters — set it, monitor for a few hours, see if performance improves. If it does, make it permanent.

    Method 2: Permanent via /etc/sysctl.conf

    The classic approach:

    sudo nano /etc/sysctl.conf

    Add this line at the end:

    vm.swappiness = 10

    Save and apply:

    sudo sysctl -p

    Verify with cat /proc/sys/vm/swappiness — should show your new value. This survives reboots.

    Method 3: via sysctl.d (Recommended)

    This is the cleaner, more modern approach:

    echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swappiness.conf
    sudo sysctl --system

    Why is this better? It keeps your swappiness config separate from everything else in sysctl.conf. More modular, easier to maintain, and dead simple to roll back if needed. This is what I use on all production servers I manage.

    Recommended Values by Use Case

    Scenario Recommended Value Why
    Web Server (Nginx/Apache) 10 Minimize swap, maximize RAM for serving requests
    Database (MySQL/PostgreSQL) 1 – 10 Databases live and die by RAM for query caching
    Redis/Memcached 1 These are in-memory stores. Swap = performance death
    Docker Host 10 Containers are already memory-sized. Swap makes it unpredictable
    Kubernetes Node 1 K8s has its own memory management. Minimize OS-level swap
    VPS with Small RAM (1-2GB) 30-50 Need swap as safety net. Don’t go too low
    Desktop/Workstation 60 (default) Default is already optimal for this use case
    Production DB + App 10 Balance between safety net and performance

    What to Monitor After Changing Swappiness

    Look, changing the value is the easy part. Making sure it doesn’t break anything — that takes a bit more effort. Here’s what I always do after a swappiness change:

    First, watch swap usage for at least a few hours:

    watch -n 5 'free -h'

    If swap usage stays low and there are no OOM killer events, you’re golden. If OOM starts showing up in dmesg, your swappiness is too low for that server’s workload — bump it up to 20 or 30.

    Second, check kernel logs for memory pressure events:

    sudo dmesg | grep -i "out of memory"

    Third, if you’re using monitoring tools like Netdata or Grafana, set up an alert for swap usage above 50%. Catching it early is always better than getting a panicked call at 3 AM.

    monitor swap usage after tuning swappiness

    Troubleshooting When Swappiness Alone Isn’t Enough

    Heads up: Don’t just blindly set swappiness to 0 on a production server without testing first. I’ve seen an admin do exactly that, and when a traffic spike hit, the server OOM-killed the Rails app because there was zero swap safety net. Total chaos.

    If you’ve tuned swappiness but swap is still high, the root cause might be something else entirely:

    1. Actual RAM exhaustion: Run ps aux --sort=-%mem | head -20 and check which processes are eating your memory. If something’s hogging 50%+ of RAM, that’s your real problem.
    2. Memory leaks: Use smem -t -k -s pss to get real per-process memory numbers. If you see a process’s memory growing steadily over hours, you’ve got a leak.
    3. HugePages: On database servers, huge pages can make memory usage look artificially high. Check with grep HugePages /proc/meminfo.
    4. Overcommit settings: Check cat /proc/sys/vm/overcommit_memory. If it’s 1, the kernel allows memory allocation beyond physical RAM, which can lead to more frequent OOM events.

    Best Practices for Production

    Here’s my quick-reference checklist:

    1. Default vm.swappiness = 10 for all production servers
    2. For databases with ample RAM (16GB+), consider swappiness = 1
    3. For Redis/Memcached, always 1 — these are in-memory systems
    4. Monitor for at least 48 hours after any change
    5. Alert on swap usage > 50%
    6. In container environments, set swappiness on the host, not inside containers
    7. Document every swappiness change in your CMDB or server notes
    8. Test on staging before touching production

    One more thing — if you’re running on NVMe SSDs, high swap usage actually accelerates SSD wear due to write amplification. So tuning swappiness isn’t just about performance — it’s about hardware longevity too.

    Pro Tip: Combine swappiness tuning with vm.vfs_cache_pressure = 50 for optimal results. This tells the kernel to cache filesystem metadata more aggressively while reducing pressure on the page cache. The combo works especially well on web servers serving lots of static files.

    Related Articles

    Q: Is it safe to set swappiness to 0 on production?

    It can be, but with conditions. If you have plenty of RAM (16GB+) and you’re confident no traffic spike will exceed physical memory, swappiness 0 is safe. But if RAM is limited, it’s risky because the kernel has no swap fallback before the OOM killer kicks in. For servers with 8GB or less, stick to 10-20.

    Q: Does the change apply to already-running processes?

    Partially. The new swappiness value affects how the kernel makes swap decisions going forward. Processes that are already swapped out won’t immediately be pulled back into RAM — the kernel will gradually page them back in as the working set demands it. So expect the full effect to take some time, anywhere from a few minutes to a few hours depending on workload.

    Q: Why do Docker and Kubernetes recommend different swappiness values?

    Because in container environments, RAM is already allocated per container. When a container starts swapping, its application performance drops immediately and unpredictably. Kubernetes explicitly recommends swappiness 1 on nodes. Docker also lets you control swap per container with --memory-swap for finer-grained control.

    Q: What’s the difference between swappiness 0 and swappiness 1?

    At 0, the kernel avoids swap as an absolute last resort — only when memory is critically low and the OOM killer is about to fire. At 1, there’s a tiny bit of proactive swapping allowed, but it’s minimal. In practice, the difference is small, but many experienced engineers prefer 1 because it provides a thin safety net without noticeable performance impact.

    Author: Syslog Solutions — NOC & Server Management Team. We handle 500+ servers daily, from shared hosting to enterprise dedicated infrastructure.

    So yeah, that’s it. Check your swappiness, tune it based on your workload, monitor for 48 hours, set alerts. Done. Don’t overthink it — start with 10, see how it goes, and adjust from there. Your servers will thank you.