Swap is an essential part of Linux systems, especially for systems with limited physical memory. It provides a backup for the RAM, ensuring that the system runs smoothly even when the RAM is fully utilized. Swap can exist as a file or a partition.
For performance reasons, a dedicated swap partition is preferred over a swap file, especially for systems with a large amount of RAM. This article details the steps to add a swap partition in Linux using existing partitions.
Using tools such as lsblk, you can get a list of existing partitions and decide which one to use or resize to make space for the swap partition. Before proceeding, ensure you've backed up any critical data, as adjusting partitions can lead to data loss.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 238.5G 0 disk ├─sda1 8:1 0 200M 0 part /boot/efi └─sda2 8:2 0 238.3G 0 part /
Identify an unused partition or space where you'd like to create the wap partition.
$ sudo mkswap /dev/sdX
$ sudo swapon /dev/sdX
$ echo "/dev/sdX none swap sw 0 0" | sudo tee -a /etc/fstab
$ cat /proc/swaps Filename Type Size Used Priority /dev/sdX partition 2097148 0 -1
$ free -h total used free shared buff/cache available Mem: 1.9G 155M 1.2G 8.8M 566M 1.6G Swap: 2.0G 0B 2.0G
Ensure that the Swap line reflects the correct size and usage of your swap partition.
Now, your system is set up with a new swap partition, ensuring smoother performance even when RAM usage is high. Remember that the speed of accessing data from swap, especially if it's on a traditional HDD, is slower than RAM. Therefore, always monitor your system's RAM usage and consider adding more physical RAM if frequently full.
Comment anonymously. Login not required.