What is a Swap Partition?
A swap partition in Linux is a dedicated section of a hard drive used to extend the system’s RAM (Random Access Memory). When the physical RAM is full, the Linux kernel moves inactive pages (data) from RAM to the swap partition to free up more space in memory. This allows the system to handle processes that exceed the available physical memory.
How Swap Works
- Primary Function: Swap acts as a “backup” memory when the system runs out of physical RAM. When your system has more running applications than can fit into RAM, Linux transfers some of the inactive processes or data to the swap area to keep the system from crashing or slowing down excessively.
- Swap vs. RAM: While swap allows your system to handle more data than fits in RAM, it is much slower than RAM because accessing data from a hard disk or SSD is far slower than accessing it from memory. Thus, swap should only be a fallback when RAM is fully utilized.
- Two Forms of Swap:
- Swap Partition: A dedicated partition on your hard drive.
- Swap File: A regular file on an existing partition that can act as swap space.
When Swap is Used
- System Memory Overload: When your applications and running processes consume all of the available RAM, the kernel begins using swap space.
- Hibernation: When you hibernate your system, the contents of RAM are written to the swap space, so when the system wakes up, it can restore the state exactly as it was.
- Application Memory Requirements: Certain applications that use a large amount of memory, such as scientific computing applications or databases, may push the system to use swap.
Swap Space Size Recommendations
- General Rule: The swap size is typically recommended as 1.5 to 2 times the amount of RAM, but this can vary based on system needs. On modern systems with large amounts of RAM, the need for swap decreases unless the system is using hibernation.
Commands Related to Swap
1. Checking Swap Space
- To view the current swap usage and configuration:
swapon --show
or
free -h
This shows both the RAM and swap usage in a human-readable format.
2. Enabling Swap Partition
- To enable an existing swap partition (e.g., /dev/sda2):
swapon /dev/sda2
3. Disabling Swap
- To turn off swap space:
swapoff /dev/sda2
4. Creating a Swap Partition
- Create a New Swap Partition: Use fdisk or parted to create a partition and mark it as swap (type 82 in fdisk).
fdisk /dev/sda
Create a partition, set its type to 82 for Linux swap.
- Format the Partition as Swap: After the partition is created, format it for swap:
mkswap /dev/sda2
- Activate the Swap Partition: To activate it immediately:
swapon /dev/sda2
- Make it Permanent: Add the following line to /etc/fstab to ensure that the swap partition is used every time the system boots:
/dev/sda2 none swap sw 0 0
5. Creating a Swap File
- If you prefer to create a swap file rather than a partition:
- Create an Empty File:
fallocate -l 2G /swapfile
This creates a 2GB file for swap.
- Set the File Permissions:
chmod 600 /swapfile
- Format the File for Swap:
mkswap /swapfile
- Enable the Swap File:
swapon /swapfile
- Add to /etc/fstab for Persistence: To ensure the system uses the swap file after reboot, add this to /etc/fstab:
/swapfile none swap sw 0 0
6. Swappiness Setting
- Swappiness controls how frequently your system uses swap instead of RAM. The default value is typically 60 (on a scale from 0 to 100), but you can change it. Lower values reduce swap usage, keeping more data in RAM.
- To check the current swappiness value:
cat /proc/sys/vm/swappiness
- To temporarily change the swappiness value to 20:
sysctl vm.swappiness=20
- To make this change permanent, add the following line to /etc/sysctl.conf:
vm.swappiness=20
Example Scenario
- You have a system with 4GB of RAM and running multiple applications. As RAM fills up, Linux may start using the swap partition to move less active processes or data out of RAM, making room for more immediate needs. This avoids system crashes, but the system performance may slow down because accessing swap is slower than accessing RAM.
Summary
The swap partition in Linux acts as an overflow space for RAM when system memory is full. While it’s slower than RAM, swap helps prevent crashes and keeps the system running smoothly under heavy memory loads. Proper configuration and management of swap space can enhance system performance, particularly in low-memory environments.