SWAP Space in Linux is a dedicated area on a storage device (disk or SSD) used as a backup for system memory (RAM). It serves as an overflow area when the physical memory is full, allowing the system to continue running by temporarily moving inactive pages of memory from RAM to swap. Swap helps prevent system crashes when RAM is insufficient but is slower than RAM since it relies on disk I/O.
Purpose of Swap Space
Extend Available Memory
Actis as Virtual memory when physical RAM is fully utilized.
Suspend-to-Disk
Stores the system's current state when suspending (hibernating) the computer.
Backup for Temporary Spikes.
Handles short-term spikes in memory usage.
Types of Swap Space
Swap Partition
A dedicated disk partition for swap
Created during the installation or manually after.
Swap File
A file on an existing file system used as sap space.
More flexible as it doesn't require repartitioning.
How Linux Uses Swap
Swapping:
Moves inactive memory pages to swap to free up RAM for active processes.Paging:
Handles memory pages actively being used, but this is less common in modern Linux systems.
The Linux kernel decides when to use swap based on the sappiness value:
A percentage (default:60) that determines how aggressively swap is used.
Higher values cause more swapping, even when RAM is available.
Lower values favor RAM usage and reduce swap reliance.
Checking Swap Usage
To view the current swap usage:
$ free -hm
$ swapon --show
Creating and managing swap space
Creating a Swap Partition
Step1: Identify unallocated Disk Space
$ lsblk
Step2: Create a Partition
Use fdisk or parted to create a new partition with 82 (Linux Swap) partition
type
$ sudo fdisk /dev/sdb
Step3: Format as Swap
$ sudo mkswap /dev/sdb1
Step4: Enable the Swap Partition
$ sudo swapon /dev/sdb1
Step5: Make It Persistent:
Add an entry to /etc/fstab
Creating a Swap File
Step1: Create the swap File
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
Step2: Set Permissions
$ sudo chmod 600 /swapfile
Step3: Format the Swap
$ sudo mkswap /swapfile
Step4: Enable the Swap File
$ sudo swapon /swapfile
Step5: Make it Persistent
Add an entry to /etc/fstab file
Adjusting Swappiness
The swappiness value determines the kernel's tendency to use swap:
Range: 0 - 100
Default: 60
To check the current swappiness value
$ cat /proc/sys/vm/swappiness
To temporarily change the value
$ sudo sysctl vm.swappiness=10
To make it permanent, add to /etc/sysctl.conf
vm.swappiness=10
Removing Swap Space
Removing a swap File
Disable the swap file
$ sudo swapoff /swapfileRemove the swap file entry in /etc/fstab
Delete the swap file:
$ sudo rm /swapfile
Removing a Swap Partition
Disable the swap partition
$ sudo swapoff /dev/sdb1Remove the entry in /etc/fstab
Optionally, delete the partition using fdisk or parted.
Monitoring and Managing Swap
View Swap Usage
$ free -hm
$ swapon --show
Turn off swap temporarily
$ sudo swapoff -a
Turn On Swap Temporarily
$ sudo swapon -a
Clear swap space
Move pages/processes back to RAM
$ sudo swapoff -a && sudo swapon -a
Best Practices for Swap Space
Recommended swap sizes
For systems with <=2 GB Ram = 2x RAM Size
For system with > 2GB RAM:
Without hibernation: equal to RAM Size.
With hibernation: RAM size + Additional buffer.
Use Swap Sparingly:
Excessive swap usage indicates insufficient RAM.
Upgrade RAM if swap is heavily used.
Optimize Performance:
Tune swappiness to reduce swap usage
Swap in Modern Systems
Modern systems with large amounts of RAM (e.g., 16GB+) rarely need swap for normal operations.
Swap is still critical for hibernation or when memory spikes occur.
SSD-Based swap is faster than HSS but may wear out SSD due to frequent writes.
Conclusion
Swap space is an essential feature in Linux for memory management and system stability. Whether implemented as partition or a file. It provides flexibility in handling memory overflow and supports advanced functionalities like hibernation. Proper management, including monitoring and tuning ensure optimal system performance.
No comments:
Post a Comment