
When it comes to server optimization in the year 2026, the knowledge of swap and memory configuration on Ubuntu servers is what will make or break your system’s efficiency and stability. Whether you are running a personal VPS, a production server with a lot of traffic, or a containerized application, the way your system handles memory and swap will have a direct effect on its uptime, speed, and reliability.
Today’s applications are more resource-intensive than ever before. Web applications, databases, Docker containers, and background processes all fight for memory. If memory is not handled correctly, your server could slow down, freeze, or even crash. This is where effective memory optimization comes into play to keep your server responsive even during traffic spikes or heavy processing.
Swap is also a safety net when the physical RAM is fully utilized. Although it is not a substitute for physical RAM, a properly tuned swap configuration can help prevent unexpected service crashes and OOM kills and give your system some leeway during peak times. However, it can also cause performance bottlenecks if it is overutilized or improperly tuned.
In this tutorial, we will walk you through some effective and contemporary methods to help you get the most out of your Ubuntu server in 2026. You will learn how to properly utilize swap, how much swap to allocate, how to properly tune the swappiness for optimal performance, and how to monitor memory usage effectively. Whether you are looking to achieve maximum performance, enhance system stability, or optimize resource usage, these tried-and-true methods will help you create a faster and more stable Ubuntu server configuration.
What Is Swap Space and Why Does It Still Matter in 2026
In practical server environments, memory utilization is rarely steady. Traffic spikes, background jobs, software updates, and database queries are just a few examples of memory-intensive activities that can happen suddenly. Swap space can mitigate these sudden memory spikes, giving the server time to recover without affecting already running processes.
In 2026, many modern applications, including containerized applications, WordPress sites running multiple plugins, and cloud infrastructure, exhibit unpredictable memory usage. Swap space ensures that non-critical or idle processes are prioritized for removal from RAM first, keeping critical processes such as web servers and databases running smoothly.
Swap space also mitigates the chances of the Linux OOM (Out of Memory) killer process intervening. In the absence of swap space, the kernel will suddenly kill critical processes to reclaim memory, resulting in lost data or server downtime. With swap space enabled, the server has more leeway to handle memory usage safely.
However, steady swap space usage is a sign of a problem. It typically indicates that the server has reached the end of its RAM capacity or that applications are memory-inefficient. In such scenarios, swap space should be a supplement, not a substitute.
Recommended Swap Size for Ubuntu Servers in 2026
Gone are the days of the “2x your RAM” rule. Modern servers and workloads demand a more nuanced approach:
| System RAM | Recommended Swap (2026) | 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 2026, 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 (2026):
- 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 2026, 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 them. 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 OOMDoomd(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 the year 2026, there is no one “perfect” swap and memory configuration for Ubuntu servers. The trick is finding the right balance using the right amount of swap, properly configuring the kernel, and leveraging the latest memory optimizations, such as zram or zswap. With a few tweaks, such as tweaking swappiness, adjusting vfs_cache_pressure, and enabling compressed memory, you can dramatically improve the reliability of your server and get the most out of every gigabyte of RAM.
It is worth noting that having a swap is not a problem; it is a crutch. With a little bit of tweaking, you can ensure that your server runs smoothly even when memory is at a premium.
FAQs:
Is a swap still necessary if I have 64GB RAM?
Yes. Even with large amounts of RAM, swap acts as a safety net for sudden memory spikes and ensures critical processes aren’t killed during unexpected loads.What’s the difference between zswap and zram?
zswap compresses and caches memory pages in RAM before writing to disk, while zram creates a fully compressed in-memory swap device. zram remains faster and ideal for servers with limited RAM or heavy workloads.Does swapping wear out SSDs?
Modern SSDs are highly durable, so moderate swap usage has minimal impact. Excessive swapping can increase writes, but for most workloads in 2026, SSD longevity isn’t a concern.How can I tell if my swap is overused?
Use commands likevmstat 5,free -h, or monitoring tools likehtop. High swap-in (“si”) or swap-out (“so”) values indicate the system is relying heavily on swap.Should I disable swap completely?
No. Swap is essential for stability, hibernation, and handling unexpected memory spikes. Instead, optimize swap usage by adjustingswappiness.How much swap should I allocate in 2026?
For modern servers, 4–8GB of swap is usually sufficient for systems with 64GB+ RAM. The exact size depends on workload intensity and whether compressed swap is used.Where should the swap be located: SSD, NVMe, or HDD?
SSDs or NVMe drives are preferred for speed. HDD-based swap works but may slow down performance during heavy memory pressure.Can swapping improve server uptime?
Absolutely. Swap prevents crashes during memory spikes, allowing critical services to continue running without interruption.How do I optimize swap performance in 2026?
Adjust kernel parameters likeswappinessandvfs_cache_pressure, and use compressed swap solutions like zram or zswap to improve memory efficiency.Is constant swap usage a problem?
Yes. Frequent swapping indicates RAM may be insufficient or workloads need optimization. Swap should remain a safety buffer, not a primary memory source.
Suggestions:
- https://mainvps.net/blog/mainvps-vs-bluehost-which-web-hosting-provider/
- https://mainvps.net/blog/low-cost-windows-vps-hosting-in-india/
- https://mainvps.net/blog/linux-vps-hosting-india/
- https://mainvps.net/blog/how-to-install-moltbot-clawdbot-on-a-vps/
- https://mainvps.net/blog/windows-server-guide-dde-dns-tls-1-2-uptime/

