Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Permanently Disable Swap in Linux?
Swap space is a portion of the hard disk that Linux uses as virtual memory. When the system runs out of physical RAM, it moves inactive pages to swap space to free up memory for active processes. However, there are scenarios where permanently disabling swap is beneficial ? when you have sufficient RAM, want to improve performance, or need to reclaim disk space.
In this tutorial, we will explore both temporary and permanent methods to disable swap in Linux, understand the implications, and learn how to verify the changes.
What is Swap Space?
Swap space acts as an extension to physical memory (RAM). When the system experiences memory pressure, the kernel moves less frequently used memory pages to the swap area on disk. This prevents system crashes due to out-of-memory conditions but comes with performance penalties since disk access is much slower than RAM access.
Checking Current Swap Usage
Before disabling swap, let's check the current swap configuration using the free command
$ free -h
The output displays memory and swap usage in human-readable format
total used free shared buff/cache available
Mem: 7.7G 2.9G 2.0G 221M 2.7G 4.2G
Swap: 2.0G 0B 2.0G
The Swap line shows total swap space (2.0G), currently used (0B), and available space.
Temporary Swap Disable
Temporary disabling is useful for maintenance tasks or testing system behavior without swap. Use the swapoff command
$ sudo swapoff -a
The -a flag disables all swap devices. To disable a specific swap partition, replace -a with the device path (e.g., /dev/sda3).
Warning: Temporary disabling can cause system instability if RAM becomes insufficient. The system may become unresponsive or crash under heavy memory load.
Permanent Swap Disable
Permanently disabling swap requires modifying the system's filesystem table and ensuring swap doesn't activate on boot.
Step 1: Edit the /etc/fstab File
The /etc/fstab file contains mount information for filesystems and swap partitions. Open it with a text editor
$ sudo nano /etc/fstab
Locate the line containing swap. It typically looks like
/dev/sda3 none swap sw 0 0
Comment out this line by adding a # at the beginning
# /dev/sda3 none swap sw 0 0
Save and exit the editor (Ctrl+X, then Y, then Enter in nano).
Step 2: Disable Current Swap
Turn off active swap space immediately
$ sudo swapoff -a
Step 3: Verify the Configuration
Check that swap is completely disabled
$ free -h
The output should show zero swap usage
total used free shared buff/cache available
Mem: 7.8G 1.1G 5.5G 108M 1.2G 6.3G
Swap: 0B 0B 0B
You can also verify using
$ swapon --show
This command should return no output if swap is completely disabled.
Considerations and Risks
Before permanently disabling swap, consider these factors
Sufficient RAM: Ensure your system has adequate physical memory for all applications
Memory-intensive applications: Some applications may fail without swap space
System stability: Without swap, the system may crash if memory is exhausted
Hibernation: Disabling swap prevents hibernation functionality
Re-enabling Swap
To re-enable swap, reverse the process
# Uncomment the swap line in /etc/fstab $ sudo nano /etc/fstab # Activate swap $ sudo swapon -a
Conclusion
Permanently disabling swap in Linux involves commenting out the swap entry in /etc/fstab and turning off active swap with swapoff -a. While this can improve performance and reclaim disk space, ensure you have sufficient RAM to handle your system's memory requirements. Always test the configuration before implementing it on production systems.
