What Is Linux Swap? (And How to Set It Up)

Linux swap acts as a safety net when your server runs low on RAM, but do modern systems really need it? In this article, we explore when swap is essential, when to skip it, and how to configure it properly for optimal server performance.

Publish date: 7/7/2025

Whether you're setting up a dedicated server, a local virtual machine, or a cloud VPS, the Linux installation process almost always asks about configuring swap space. With RAM being more affordable and plentiful than ever, you might wonder if you still need this feature. This guide will dive into what Linux swap does, helping you make an informed decision for any system you manage.

Understanding swap memory basics

Linux swap is essentially a virtual memory extension on your disk. When your system's physical memory (RAM) is nearly full, the operating system can move less-frequently used pages of memory to a pre-allocated space on your storage drive, either a swap partition or a swap file. This process, called swapping, frees up RAM for active processes and can prevent your applications from crashing due to out-of-memory errors.

Think of it like overflow parking for a busy restaurant. When the main lot (RAM) is full, cars can still be accommodated in a secondary lot (swap). However, retrieving a car from the overflow lot takes longer. Similarly, accessing data from swap is significantly slower than accessing it from RAM, which has performance trade-offs. The performance gap is enormous, even with fast NVMe SSDs, so swap should be seen as a safety net, not a substitute for adequate RAM.

When you definitely need swap

While not always mandatory, swap is a good idea in several scenarios:

  • Systems with low RAM: Any system with 4GB of RAM or less benefits greatly from swap as a safety net.
  • Preventing system crashes: Swap provides a buffer during unexpected traffic spikes or when running memory-intensive applications. Without it, the kernel's Out-Of-Memory (OOM) killer will forcefully terminate processes to free up memory, leading to service interruptions.
  • Running database servers: Some applications, like PostgreSQL and MySQL, can perform better when swap is available, even if it's not heavily used. The kernel can make more efficient memory management decisions simply by knowing swap is there.
  • Using hibernation: If you use the hibernation feature (suspend-to-disk) on a Linux desktop or laptop, your swap space must be at least as large as your physical RAM.

When swap might not be necessary

In certain specialized environments, skipping swap can be a valid choice:

  • High-performance, high-RAM systems: If a server has ample RAM (e.g., 128GB+) for its specific, predictable workload, and any performance dip from swapping is unacceptable, you might disable swap. This is common in latency-sensitive applications, like in-memory databases.
  • Auto-scaling cloud environments: Instead of relying on slower swap space, cloud-native infrastructures can be configured to automatically provision new instances when memory pressure increases. This maintains high performance while handling the load.

How to add and configure swap

Linux supports two types of swap space: a dedicated swap partition or a flexible swap file. For most modern systems, a swap file is the recommended choice because it can be easily created, resized, or removed on a running system without repartitioning your disk.

Here is a complete guide to adding, sizing, and configuring a swap file:

Step 1: Create the swap file

First, choose a size for your swap file. In this example, we will create a 4GB file. You have two common commands for this task:

  • Option A: fallocate (Recommended & faster) This command instantly allocates the disk space. It's very fast but is not supported by all filesystems (e.g., older XFS).
    # Creates a 4GB file instantly
    sudo fallocate -l 4G /swapfile
  • Option B: dd (Universal compatibility) This command works on any filesystem because it physically writes data to the disk, which is slower. Use this if fallocate gives you an error.
    # Creates a 4GB file by writing 4096 1MB blocks
    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

Step 2: Set secure permissions

For security, the swap file should only be readable and writable by the root user.

sudo chmod 600 /swapfile

Step 3: Format the file for swap and enable it

Next, format the file as a swap area and then activate it.

# Set up the file as a Linux swap area
sudo mkswap /swapfile

# Enable the swap file for immediate use
sudo swapon /swapfile

Step 4: Make the swap permanent

To make sure the swap file is automatically used after a reboot, add it to the /etc/fstab configuration file.

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

You can verify that your new swap is active using sudo swapon --show or free -h.

Step 5: Configure sizing and swappiness

Sizing recommendations: A good starting point for swap size is:

  • Systems with < 2GB RAM: 2 times the amount of RAM
  • Systems with 2-8GB RAM: Equal to the amount of RAM
  • Systems with > 8GB RAM: At least 8-16 GB. For systems with very large amounts of RAM (64GB+), you often don't need to match the RAM size unless you require hibernation. A smaller 8-16 GB swap is typically sufficient.

Adjusting swappiness: The swappiness parameter (a value from 0 to 100) controls how aggressively the kernel uses swap.

  • A low value (e.g., 10) tells the kernel to avoid swapping unless absolutely necessary. This is ideal for database servers.
  • A high value (e.g., 60), the default on many systems, makes the kernel swap more readily to keep RAM free.

You can check your current swappiness value with:

cat /proc/sys/vm/swappiness

To change it for the current session (e.g., for a database server), use sysctl:

# To prioritize keeping data in RAM
sudo sysctl vm.swappiness=10

To make this change permanent, add the line vm.swappiness=10 to your /etc/sysctl.conf file.

Monitoring Swap Usage

High swap usage, especially when RAM is still available, can point to a misconfigured swappiness value or an application with a memory leak. Use these commands to investigate:

# Check current memory and swap usage
free -h

# Find the top 10 processes using swap
# The output format is: /proc/[PID]/status:VmSwap:[Value] [Unit]
grep VmSwap /proc/*/status | sort -k 2 -n -r | head -10

If you find that a specific process is constantly using swap, it’s worth investigating its configuration to optimize its memory usage.

Conclusion

Whether you're running a personal blog on a budget VPS or managing enterprise applications, swap gives you a buffer when memory usage spikes unexpectedly.

The decision comes down to your specific use case. If you're managing systems where uptime matters and you can't afford sudden process kills, enable swap. If you're running high-performance applications where even small amounts of swap-induced latency are unacceptable, you might skip it—but only if you're confident your RAM allocation covers all scenarios.

That said, if you're looking for reliable infrastructure for your next project, xTom provides a range of solutions, including dedicated servers and colocation, and beyond. For flexible and scalable KVM instances, be sure to check out the NVMe-powered VPS hosting at V.PS.

Thanks for reading, and hopefully it was helpful!

Frequently asked questions about Linux swap

What happens if I run a Linux server without swap?

Without swap, your server will terminate processes when it runs out of physical RAM. The kernel's OOM (Out of Memory) killer will forcefully stop applications to free memory, which can cause unexpected service disruptions. While a server with a lot of RAM might run fine, it loses the safety buffer for memory spikes.

How much swap space should I allocate for a web server?

For a web server with 4-8GB of RAM, allocating a swap space equal to your RAM size is a good rule of thumb. For servers with more than 8GB of RAM, 8-16GB of swap is typically sufficient. The key is to monitor your usage and remember that swap is for emergencies, not for daily operation.

Does having swap slow down my Linux server?

Having swap space available does not slow down your server. Performance is only impacted when the system actively uses swap (reading from or writing to it). If you notice constant swapping during normal operation, your system needs more physical RAM.

Can I add swap to a running Linux system?

Yes, you can add a swap file to a running Linux system at any time without downtime. You simply create the file, format it for swap, and enable it with the swapon command. This flexibility is a primary advantage of swap files over partitions.

Should I use a swap partition or a swap file?

For most modern systems, a swap file is the recommended choice due to its flexibility. You can easily create, resize, or remove swap files without repartitioning your disks. While swap partitions were historically considered slightly faster, this performance difference is negligible on systems with SSD or NVMe storage.