Linux swap is a space on a hard drive that is used as virtual memory when the physical RAM is full. It allows the system to temporarily move inactive pages from RAM to disk, freeing up RAM for active processes. This helps to manage memory more efficiently and can prevent system crashes due to memory exhaustion.
Key points about Linux swap:
- Swap Space: It can be a dedicated swap partition or a swap file within a filesystem.
- Usage: When the system runs low on RAM, it will swap out less frequently used data to the swap space.
- Performance: Accessing data from swap is slower than accessing it from RAM, so excessive swapping (known as "swapping thrashing") can degrade performance.
- Configuration: The amount of swap space needed depends on the system's workload and RAM size. A common recommendation is to have swap space equal to the size of RAM or 1.5 times the RAM size for systems with less RAM.
You can check the current swap usage with the command:
swapon --show
And to create a swap file, you can use the following commands:
# Create a swap file of 1GB
sudo fallocate -l 1G /swapfile
# Set the correct permissions
sudo chmod 600 /swapfile
# Set up the swap file
sudo mkswap /swapfile
# Enable the swap file
sudo swapon /swapfile
To make the swap file permanent, add the following line to /etc/fstab:
/swapfile none swap sw 0 0
