
When it comes to optimizing server performance in 2025, understanding swap and memory configurations on Ubuntu servers can make or break your system’s stability and efficiency. Whether you’re running a personal VPS, a large-scale production environment, or a containerized application, properly tuning your swap and RAM usage ensures smoother performance, fewer crashes, and faster response times. Let’s dive into the most effective, modern configurations and practical strategies to get the best out of your Ubuntu server setup.
What Is Swap Space and Why It Still Matters in 2025
Even with servers boasting 64GB or more of RAM, swap space remains crucial. Swap acts as an overflow area for when physical RAM runs out. When your system’s memory is fully used, Ubuntu temporarily moves inactive pages of memory to the swap space—typically on disk—so that the server can continue running instead of crashing or killing processes.
However, swap isn’t a replacement for RAM. Since it’s located on a storage drive (SSD or HDD), it’s slower—sometimes dramatically so. The key in 2025 is balancing performance and stability by tuning swap size, location, and management settings correctly.
Recommended Swap Size for Ubuntu Servers in 2025
Gone are the days of the “2x your RAM” rule. Modern servers and workloads demand a more nuanced approach:
| System RAM | Recommended Swap (2025) | Use Case |
|---|---|---|
| < 4 GB | 2x RAM | Light workloads, basic VPS hosting |
| 4–16 GB | Equal to RAM (1x) | Web servers, moderate applications |
| 16–64 GB | 4–8 GB minimum | Databases, Docker hosts |
| 64+ GB | 2–4 GB (plus hibernation if needed) | Enterprise workloads |
For most Ubuntu 22.04/24.04 servers in 2025, a 4–8 GB swap is sufficient, assuming you monitor your system properly. You can check current usage with:
free -h
and adjust swap dynamically as needed.
Choosing Between Swap Partition vs Swap File
In earlier Ubuntu versions, swap partitions were standard. But since Ubuntu 20.04, swap files have become the default—and for good reason. Here’s why:
Swap Partition
- Slightly faster on dedicated SSDs.
- Fixed size—difficult to resize dynamically.
- Best for systems where swap use is predictable.
Swap File
- Easier to resize or move.
- Works on most file systems (ext4, xfs, etc.).
- Now the default and recommended approach for most Ubuntu 22.04+ setups.
To create a swap file:
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
This setup gives flexibility to modify or disable swap later without repartitioning disks.
Understanding Swappiness and How to Tune It
Swappiness is one of the most important yet misunderstood kernel parameters. It controls how aggressively Ubuntu uses swap. A higher value (e.g., 60) means the system swaps sooner, while a lower value (e.g., 10) makes the OS rely more on RAM.
You can check the current setting with:
cat /proc/sys/vm/swappiness
To adjust it:
sudo sysctl vm.swappiness=10
And to make it permanent:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Recommended Swappiness Values (2025):
- Desktop systems: 40–60 (to prevent UI lag)
- Web servers / Databases: 10–20 (favor in-memory performance)
- Low-memory VPS: 30–40 (balance between responsiveness and stability)
For most production Ubuntu servers, 10–15 hits the sweet spot—just enough to use swap when truly necessary without killing performance.
Tuning vm.vfs_cache_pressure for Better Memory Efficiency
Another critical parameter for optimizing memory behavior is vfs_cache_pressure. It determines how aggressively the kernel reclaims memory used for caching directory and inode information.
By default, it’s set to 100, meaning Ubuntu reclaims cache at a normal rate. Lowering it (e.g., to 50) makes the system keep cached data longer, which can improve filesystem access performance.
To adjust:
sudo sysctl vm.vfs_cache_pressure=50
Make it permanent:
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
When used together, swappiness and vfs_cache_pressure create a balanced memory ecosystem—less disk thrashing, better I/O performance, and smoother multi-user environments.
Optimizing Swap on SSDs and NVMe Drives
In 2025, most servers are running SSD or NVMe storage. While these drives are fast, constant swap I/O can still shorten their lifespan. Here’s how to optimize swap on SSDs:
- Use zswap or zram (more below).
- Keep swap sizes moderate (not excessive).
- Monitor wear-leveling with
smartctl. - Avoid swap partitions; prefer files (easier to move or limit).
You can also enable TRIM support for swap files to keep SSDs healthy:
sudo systemctl enable fstrim.timer
Modern Swap Technologies: zswap, zram, and Compressed Memory
Ubuntu 24.04 LTS and later support advanced swap compression technologies that can drastically improve memory efficiency.
1. zswap
zswap is a compressed swap cache inside RAM. Instead of writing pages to disk, Ubuntu compresses them and keeps them in memory until necessary—reducing I/O load and speeding up swapping.
Enable it by editing GRUB:
sudo nano /etc/default/grub
Add:
GRUB_CMDLINE_LINUX_DEFAULT="zswap.enabled=1"
Then:
sudo update-grub
sudo reboot
2. zram
zram creates a compressed block device in RAM for swap usage—perfect for low-memory VPS or IoT servers.
To enable zram:
sudo apt install zram-tools
Ubuntu automatically creates compressed swap devices after enabling it. This can double or triple your effective RAM for lightweight workloads.
3. Hybrid Setup (zram + Swap File)
For high-performance setups, use both. zram handles quick bursts, while a small disk-based swap file provides backup for overflow situations.
Monitoring Memory and Swap Usage Effectively
Once you’ve optimized configurations, regular monitoring ensures long-term stability. Use these commands:
free -h— quick overview of RAM and swap usagevmstat 5— live performance snapshothtoporglances— interactive system monitorsmem— more accurate memory usage breakdown
You can also install Netdata or Prometheus + Grafana for visual or for VPS monitoring, historical tracking of memory and swap trends.
Practical Tips for Maintaining Healthy Memory Performance
- Keep background processes minimal.
- Restart long-running applications occasionally to clear memory leaks.
- Enable
oomd(Out-Of-Memory Daemon) in Ubuntu 24.04+ for smart process killing instead of total crashes. - Regularly update your kernel—memory management improvements continue to arrive with each release.
A stable Ubuntu server isn’t just about having more RAM; it’s about teaching your system how to use it wisely.
Final Thoughts
In 2025, the best swap and memory configuration for Ubuntu servers isn’t one-size-fits-all. It’s a blend of right-sized swap, smart kernel tuning, and modern compression technologies like zram or zswap. With just a few tweaks—optimizing swappiness, vfs_cache_pressure, and enabling compressed memory—you can dramatically extend your server’s stability, reduce downtime, and make better use of every gigabyte available.
Remember: swap is not a failure—it’s a safety net. Tune it well, and your Ubuntu server will reward you with consistent, reliable performance all year long.
FAQs
1. Is swap still necessary if I have 64GB RAM?
Yes. Even with large RAM, swap provides stability during memory spikes and allows for crash-free operation under unexpected loads.
2. What’s the difference between zswap and zram?
zswap caches compressed pages in RAM before writing to disk, while zram creates a compressed in-memory swap device. zram is faster for low-memory systems.
3. Does swap slow down SSDs?
Excessive swap writes can wear SSDs, but with modern drives and moderate swap activity, the impact is minimal.
4. How can I tell if my swap is overused?
Run vmstat 5 or check htop. High “si” (swap-in) and “so” (swap-out) values indicate your system is relying heavily on swap.
5. Should I disable swap completely?
Not recommended. Some Ubuntu features (like hibernation) rely on swap. Instead, minimize swap usage with low swappiness values.

