What is Swap?
Swap is a partition that helps RAM by creating virtual memory when RAM is getting full. In other words, When a computer runs out of RAM, the operating system will swap data between RAM and a designated area on the disk, called the swap space
For creating a swap partition, First we have to add one disk to our server. We will add 10 GB disk to our system.
As always we have to stop our virtual machine server to add new hard disk of 10 GB.
We have to click on “Edit virtual machine settings“.
We have to click on ‘Add‘ button and select ‘Hard Disk‘.
Click on Next.
We will go with the recommend option of SCSI. Click on Next.
We will go with the default option of “Create a new virtual disk“. Click on Next.
We will provide 10 GB of space in front of “Maximum disk size“. Click on Next.
Click on Finish and Power on the machine.
Now our new hard disk of 10 GB is attached with our server to make a partition for swap.
Let’s check our partition detail by ‘fdisk -l‘ command.
We got our 10 GB of hard disk with the name “/dev/sdc“.
Now, We have to make the partition of 5 GB for the swap. There would be the 2 steps that is creating the partition and formatting the partition.
We will create the partition by “fdisk /dev/sdc” command.
For new partition, We will press n.
We will go with the primary option by pressing p.
We will go with partition number 1 as it is our first partition.
For the first sector, We will go with default and press enter.
Now, We have to give the size by adding +5G for 5 GB.
Next, We have to change the partition type to swap from Linux by pressing t and type 82.
We have to press w for writing and quitting from the hard disk. If you want to learn more about partitions then click on the link.
We have to settle our partition by “udevadm settle” command.
Now we will check our partition by “fdisk -l” command.
As we can see that we get “/dev/sdc1” of 5 GB and of swap type.
We can check our current RAM space by “free -m” command.
As we can see, There is no swap memory available for my server and we have already created a partition for swap memory.
We will format the partition with ‘mkswap (partition name)’ command with our new partition name that is “mkswap /dev/sdc1“.
Our new swap space is created and now we have to activate it by “swapon (partition name)” command that is “swapon /dev/sdc1“.
We can check our swap partition by “swapon -s” command.
Now last step is to make our swap partition permanent by adding it in “/etc/fstab” file.
We can save “/etc/fstab” file and check it by “mount -a” for errors.
That’s all!
Thanks for Learning with us. You can downaload the iso file from here.
FAQ’s
Q. Why do I need swap memory?
A. Swap memory helps prevent your system from crashing when physical RAM is exhausted. By providing additional virtual memory, swap ensures that your system can continue running even when there are more processes or data than RAM can handle. Without swap space, systems with insufficient RAM may terminate processes or freeze.
Q. How does swap memory work?
A. When the physical RAM is full, the operating system moves less frequently used data (pages) from RAM to the swap space on disk. This process is called “paging” or “swapping out.” When the data is needed again, it is swapped back into RAM. This allows the system to maintain stability without relying solely on physical memory.
Q. Is swap space the same as virtual memory?
A. Yes, swap space is part of a system’s virtual memory. Virtual memory is a memory management technique that extends the available memory beyond physical RAM, allowing the operating system to use swap space when necessary. Swap space acts as the storage medium that facilitates this process.
Q. How much swap space should I allocate?
A. The recommended amount of swap space depends on your system’s RAM and workload. Common guidelines include:
1. For systems with 4GB or less of RAM: 2 times the amount of RAM.
2. For systems with 4GB–8GB of RAM: 1.5 times the amount of RAM.
3. For systems with more than 8GB of RAM: At least 8GB, or an amount based on your workload (e.g., heavy applications or large datasets). However, these are just guidelines. Modern systems with plenty of RAM may not need as much swap, and SSDs can mitigate some of the speed differences.
Q. Can I increase or decrease swap space?
A. You can create or resize swap files or partitions by adjusting swap space size.
Q. What is “swap usage” and how do I check it?
A. Swap usage refers to the amount of swap space that the system is currently using. You can check swap usage using system monitoring tools like vmstat, top and free -h
Discover more from Root Learning
Subscribe to get the latest posts sent to your email.
Another nice stuff after partitions