//------------------------------------------------------------------- //-------------------------------------------------------------------
Swap Usage on Linux Servers

Linux Swap Usage Guide: Why It Happens and How to Optimize It

Many Linux admins are struggling with this question: Why is my server using swap when I still have free RAM? Swap is a safety net for system stability, but it can cause performance bottlenecks if you don’t understand how it works. Swap usage on Linux is for freeing up RAM for caching, so seeing it in use is usually normal. However, without the right settings, this can lead to swap storms, where your server freezes because the hard drive is working too hard.

This guide from PerLod Hosting covers everything you need to know about Linux swap. You will learn the commands to check your memory, understand how swap works, and find the best strategies to keep your production servers running smoothly.

Understand and Analyze Swap Usage on Linux

Swap is a space on a hard drive or SSD that functions as an extension of your physical RAM. When your server runs out of physical memory (RAM), the Linux kernel moves inactive pages of memory to the swap space to free up RAM for active processes.

In this step, you must check and analyze swap usage on Linux to check if swap usage is normal on your server.

Check Swap Usage on Linux

To verify the current status of your swap, you can use the free command to see total swap usage on your server:

free -h

In the output, you will see the following metrics:

  • total: Total swap space available.
  • used: Amount currently used. A high used value is not necessarily bad if the system is stable.
  • free: Remaining available swap.

To see if you are using a swap partition or a swap file and its priority, you can use the swapon command to get a more detailed view:

swapon --show

Real-time Swap Activity in Linux

Having data sitting quietly in swap is fine. The real problem happens when data is constantly moving between your fast RAM and your slow hard drive. If you see high numbers for si (swap in) or so (swap out), it means your server is struggling.

You can use the vmstat command to watch real-time swap activity. For example, watch swap activity every 1 second for 5 times:

vmstat 1 5

Key metrics to watch:

  • si (Swap In): Memory being moved from disk back to RAM. High numbers here mean processes are waiting for data, which causes lag.
  • so (Swap Out): Memory being moved from RAM to disk.

High swap activity leads to increased disk latency. You can check this guide on monitoring I/O latency to see exactly how disk usage affects your applications.

Why Linux Uses Swap Even With Free RAM?

It is common for Linux admins to see swap usage on Linux when the free command shows plenty of available RAM. This is usually intended behavior, not a bug.

Linux manages memory with two main priorities:

  • Anonymous Memory: Data actively used by programs, for example, Python script’s variables.
  • Page Cache: Data from the disk is stored in RAM to speed up file access, for example, log files and web server assets.

The kernel often decides to move used anonymous memory to swap, so it can use that physical RAM for page cache, which improves I/O performance. If Linux never swapped until RAM was 100% full, it would have no room to cache disk files, which would make the entire server slow down.

This behavior is controlled by a kernel parameter called swappiness, a value from 0 to 100.

  • Default (60): This is a balanced setting. The server tries to find a balance between using swap and using RAM.
  • High (80 to 100): This is usually use swap. It aggressively moves program data to the disk to keep physical RAM free for speeding up file access.
  • Low (0 to 10): This avoids using swap unless RAM is full, and it has no other choice.

To check the current swappiness on your server, you can run:

sysctl vm.swappiness

How to Prevent Swap Usage on Linux?

Swap storms happen when the server gets stuck in a loop of constantly swapping data in and out, which makes the system unresponsive.

To prevent swap storms from happening on your server, you can follow these best practices:

Tune Swappiness for Servers

For database servers like PostgreSQL, MySQL, or application servers where latency matters more than file caching, the default swappiness of 60 is often too high. You must reduce it to ensure the kernel prefers to keep application data in fast RAM.

You can temporarily change the swappiness value with the command below:

sudo sysctl vm.swappiness=10

To make the swappiness changes permanent, edit the /etc/sysctl.conf and add the following line:

vm.swappiness=10

Then reload the settings to apply the changes:

sudo sysctl -p

Note: Setting this to 0 does not turn off swap completely; it just tells the system to avoid it. We recommend using 1 or 10 for production servers. This minimizes swap usage but still prevents crashes (OOM errors) if memory runs out.

If you are using VPS hosting, resources are often shared or strictly limited. Setting a lower swappiness is especially important to prevent your noisy neighbor processes from forcing your active data into swap.

Adjust Cache Pressure

Another hidden cause of swap usage on Linux is cache pressure. This setting controls how quickly the system clears out its cache of file and folder locations to free up memory.

The default value of cache pressure is 100. If your server handles many small files, like a large PHP application or a mail server, these cache file locations can grow and push app memory to swap. You can increase this value, for example, to 200, to force the kernel to delete old file cache from RAM before it starts moving your active programs to swap.

To do this, you can edit the /etc/sysctl.conf file and add the following line to the file:

vm.vfs_cache_pressure=200

Monitor Swap Activity vs. Swap Usage

Many admins set an alarm to go off if swap gets 50% full, which is usually a mistake.

High Usage is often fine: If your server puts 2GB of old, unused data into swap and leaves it there, that is healthy. It means that 2GB of fast RAM is now free for more important tasks.

High Activity is the enemy: The real problem happens when the server is constantly writing to and reading from swap. If your server is swapping 100MB every second, it is spending all its energy moving data instead of running your applications. To the user, it will look like the server has crashed.

You must change your monitor settings in tools like Zabbix, Prometheus, or Datadog:

  • Disable: Alerts based on total swap space used, for example, when the swap space is higher than 80%.
  • Enable: Alerts based on Swap I/O rates ( vmstat si and so metrics). If these numbers spike, your server is in trouble.

Note: To understand if your server is healthy, you need to monitor key performance metrics beyond just swap space, such as CPU latency and memory throughput.

FAQs

Should I disable swap if I have lots of RAM?

No, do not disable it. Swap moves inactive data out of RAM to make room for important tasks like file caching. If you disable it, a sudden memory spike won’t just slow your server down; it will crash your applications immediately.

Is a Swap File slower than a Swap Partition?

Not anymore. On modern systems with SSDs, the speed difference is tiny. Swap files are now preferred because they are flexible; you can easily resize or delete them without having to reformat your hard drive.

How big should my swap size be?

Under 2GB RAM: Set swap to 2x your RAM.
2GB to 8GB RAM: Set swap equal to your RAM.
Over 8GB RAM: 4GB is usually enough.
Exception: If you use Hibernation, your swap must be bigger than your total RAM.

Conclusion

Managing swap usage on Linux is about finding the balance between stability and performance. It is normal and healthy for a server to use some swap space, which proves the kernel is efficiently managing resources by prioritizing active processes and file caching.

Just remember, swap is not real RAM. If your server is constantly struggling with high si and so activity even after tuning settings, no trick will fix it. At that point, you only have two choices: upgrade your RAM or fix your applications to use less memory.

By monitoring swap activity instead of just swap usage, you can ensure your Linux servers remain responsive and stable under load.

We hope you enjoy this guide. Subscribe to our X and Facebook channels to get the latest articles.

If you are running on limited resources, check our guide on VPS performance optimization to get the most out of your hardware before increasing swap.

Post Your Comment

PerLod delivers high-performance hosting with real-time support and unmatched reliability.

Contact us

Payment methods

payment gateway
Perlod Logo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.