Running resource-intensive tasks on Ubuntu—such as compiling large software codebases, rendering 3D graphics, running multiple Docker containers, or managing database workloads—can quickly exhaust available physical RAM. When physical memory fills up completely and no virtual memory buffer exists, the Linux kernel’s Out of Memory (OOM) Killer abruptly terminates active processes, resulting in lost work, database corruption, or complete server lockups. While adding more physical RAM hardware is the permanent solution, you can immediately stabilize your system by creating or enlarging a Swap File in Ubuntu. Swap memory reserves a portion of your hard drive or SSD to act as secondary virtual memory when RAM capacity is exceeded.
Understanding Swap Files vs Swap Partitions in Ubuntu
In modern Linux architecture, swap memory can be implemented in two ways:
- Dedicated Swap Partition: A fixed partition on your storage drive formatted with a swap filesystem. Modifying partition sizes requires unmounting filesystems, using live USB environments, and risking partition table errors.
- Dynamic Swap File: A standard regular file (commonly located at
/swapfile) formatted as swap space. In Ubuntu 20.04, 22.04, 24.04, and later, swap files are the default standard. Swap files can be resized, created, or deleted on live running production servers in seconds without repartitioning disks. - Kernel Swappiness Tuning: A Linux kernel parameter (ranging from 0 to 100) that governs how aggressively the operating system transfers memory pages from RAM into swap space.
Step-by-Step: Checking Existing Swap Space in Ubuntu
Before modifying swap configuration, inspect your system’s current memory status:
- Open your terminal emulator.
- Check active swap allocations by running:
sudo swapon --showIf the command returns nothing, your system currently has zero swap space enabled.
- Inspect total, used, and available physical and virtual memory:
free -h - Verify available disk storage on your root partition:
df -h /Ensure your storage drive has sufficient unallocated gigabytes before allocating a larger swap file.
Creating or Increasing a Swap File (Step-by-Step)
To safely increase an existing swap file (for example, upgrading from 2 GB to 8 GB), follow these sequential terminal commands:
1. Turn Off Existing Swap Space
Before replacing an existing swap file, deactivate active swap processing:
sudo swapoff /swapfile
2. Allocate the Desired File Size
Use fallocate to instantly reserve your target disk capacity. For example, to allocate an 8 GB swap file:
sudo fallocate -l 8G /swapfile
Note: If you use the Btrfs filesystem or encounter errors with fallocate, use the dd utility instead:
sudo dd if=/dev/zero of=/swapfile bs=1G count=8 status=progress
3. Secure File Permissions (Crucial Security Step)
Because swap space can contain plaintext credentials, encryption keys, and private application memory dumped from RAM, you must restrict read and write permissions exclusively to the root user:
sudo chmod 600 /swapfile
4. Format the File as Linux Swap
sudo mkswap /swapfile
The terminal outputs a unique UUID confirming that the file is initialized as swap space.
5. Activate the New Swap Space
sudo swapon /swapfile
6. Verify the New Capacity
Confirm that your new swap allocation is active and recognized by the kernel:
sudo swapon --show
free -h
Making the Swap File Permanent Across Reboots
To ensure Ubuntu mounts your swap file automatically whenever the server or computer restarts, verify that the entry exists in /etc/fstab:
- Open the filesystem table configuration file in a text editor:
sudo nano /etc/fstab - Check if the following line is present at the bottom of the file:
/swapfile none swap sw 0 0 - If the line is missing, add it to the bottom of the file, then save and exit (press Ctrl + O, then Enter, followed by Ctrl + X).
Tuning the Swappiness Kernel Parameter for Better Performance
The vm.swappiness setting determines how eagerly the Linux kernel swaps memory. The default Ubuntu value is typically 60. For modern desktop and server systems with SSD storage, reducing this value to 10 or 20 ensures the OS prioritizes keeping applications in fast physical RAM, using swap primarily as an emergency safety net:
- Check your current swappiness value:
cat /proc/sys/vm/swappiness - Temporarily test a lower swappiness value:
sudo sysctl vm.swappiness=20 - To make this setting permanent across reboots, edit
/etc/sysctl.conf:sudo nano /etc/sysctl.confAppend this line at the bottom of the file:
vm.swappiness=20
Recommended Swap Size Guidelines for Ubuntu
| Physical RAM Installed | Recommended Swap (Standard Workloads) | Recommended Swap (With System Hibernation) |
|---|---|---|
| 1 GB – 2 GB | 2 GB – 4 GB (Double RAM) | 3 GB – 5 GB |
| 4 GB – 8 GB | 4 GB | 8 GB – 12 GB (1.5x RAM) |
| 16 GB – 32 GB | 4 GB – 8 GB | Equal to RAM size (16 GB – 32 GB) |
| 64 GB+ (Servers) | 8 GB – 16 GB | Not recommended (disable hibernation) |
Frequently Asked Questions
Does using a swap file degrade NVMe SSD lifespan?
Modern NVMe and SATA solid-state drives possess high Terabytes Written (TBW) endurance ratings. With a tuned swappiness value (such as 10 or 20), swap is accessed infrequently during normal operation, causing negligible impact on SSD longevity.
Can I remove a swap file completely if I have plenty of RAM?
Yes. Run sudo swapoff /swapfile, remove the entry from /etc/fstab, and delete the file with sudo rm /swapfile. However, retaining at least 2–4 GB of swap is strongly recommended to handle rare memory spikes safely.
Why did fallocate fail with “fallocate failed: Operation not supported”?
Certain filesystems (such as Btrfs or older ext3 configurations) do not support fallocate for swap files. In this case, use the dd command provided in step 2 to allocate zero-filled disk blocks directly.