How to Configure Swap Space on Linux: Step‑by‑Step Guide
This article explains what swap space is, why it matters for system stability and performance, and provides detailed Linux commands and procedures for creating, configuring, activating, and monitoring swap files or partitions.
Overview
Swap space is a disk‑based extension of RAM that stores inactive memory pages when physical memory is insufficient, preventing crashes despite being slower than RAM.
Configuration
Configuration Principles
The total swap size is usually recommended to be the larger of twice the physical RAM or 32 MB, but not exceeding 2 GB, adjusted per actual needs.
Using a dedicated swap partition is preferred for better performance and manageability.
Linux Configuration Method
Creating a swap file:
Disable any existing swap.
Use the dd command to create a file, e.g., a 10 GB swap file: sudo dd if=/dev/zero of=/swapfile bs=1G count=10 Set file permissions so only root can read/write: sudo chmod 600 /swapfile Mark the file as swap space: sudo mkswap /swapfile Activate the swap file: sudo swapon /swapfile Configure a swap partition (optional):
Partition the disk using fdisk or parted to create a swap partition.
Format it with mkswap.
Activate it with swapon.
Viewing Swap Usage
Linux provides several commands to inspect swap status: free -m – shows memory and swap usage in megabytes. free -m Option -m displays values in MB. swapon --show – lists active swap files or partitions.
swapon --show cat /proc/swaps– displays detailed swap information. cat /proc/swaps Tools like top or htop display real‑time swap usage.
Functions
Memory extension: provides extra storage when RAM is low, keeping the system running.
Performance optimization: frees physical RAM for active processes by moving idle pages to swap.
System stability: prevents crashes or severe slowdowns caused by memory exhaustion.
Example
Creating an 8 GB swap file and activating it:
sudo dd if=/dev/zero of=/swapfile bs=1G count=8
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileVerify the configuration with: free -m The system now has swap space enabled, improving stability and performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
