If you are a Linux user you have definitely heard about Linux Swap, but you may not know
what it is exactly and how you can use it.
Time to change that!
I mention Linux swap and swappiness in almost every video
on things to do after installing Linux.
You can watch these videos in this playlist.
You probably know that Linux swap is somehow related to RAM
and swappiness can affect your system performance.
In this video I will explain you what it is and how to use it in little more details.
You will learn: • What is Linux Swap?
• Do you need Linux Swap?
• What are Swap Partition and swap file?
• How to create a Swap File.
• What is swappiness and how to adjust it?
What is Linux Swap?
The Linux Kernel usually uses your RAM memory to store temporary information.
When there is no enough RAM space, the Linux Kernel takes some of this infromation from
RAM and writes it to the swap space on a hard drive.
This is called the swapping process.
This way your Linux system can release some RAM space
and doesn't crash due to lack of memory.
So, Linux swap is a very useful way to extend the RAM because it provides the necessary
additional memory when the RAM space has been exhausted and a process has to be continued.
That is why when you install a Linux distribution, the installation wizard usually asks you to
assign a space for the swap partition.
Do you need Linux Swap?
This is a question many novice users ask themselves when they begin to discover Linux.
In fact, this will depend on the use and amount of RAM your computer has.
Nevertheless, having a swap space is desirable even if you have a lot of RAM.
For example, usually when your RAM gets full and the Linux kernel has no space to write
into, your system will crash.
On the other hand, if you have a swap space, it will be used by the Linux kernel and your
system will keep working, though much slower.
So, it is safer to have a swap space.
Note: swap space has one disadvantage - it is much slower than RAM.
So, adding a swap space won't make your computer faster, it will only help to overcome some
limitations posed by the RAM size.
There are two option for a swap space: a swap partition and swap file.
Swap Partition
Swap Partition is just part of your hard drive which is reserved for a swap space
It is sort of hard coded in your hard drive. So, is usually not that easy to change.
I recommend that you create the Swap partition during the installation of your Linux distribution.
• If your computer has 1Gb of RAM or less,
then the Swap partition should be twice the size of the RAM.
• If you have between 2gb and 4gb of RAM,
the size of the Swap partition should equal the RAM.
• For RAM of 8 GB, swap can be half of RAM.
• Finally, If you have more than 8gb of RAM, then it is enough to have 2-4Gb.
But everything depends on your use case.
You can check type and size of your Swap with this command:
This is the size of my swap partition.
Linux Swap File
Alternatively to a swap partition, you can create a Linux Swap File after the installation.
The modern Linux Kernel allows Swapping to a Swap file instead of a Swap partition.
A Swap file has an advantage over a swap partition that you can change the size of your swap
any time simply by changing a swap file size.
If you want to create a swap file, run this command first:
Note: this command will to create a 1Gb swap file.
Replace 1G with the value you want.
Next, you have to set the correct permissions.
Then, format the file to swap.
Finally, enable the swap.
If you want the changes to be permanent, you
need to edit the /etc/fstab file
open it
and add the following.
Ctrl+O to save the changes and
Ctrl+X to exit
In the end, check the status of the swapfile:
here it is
How to remove or edit a Linux Swap File
In case, you need to remove or edit a Linux Swap File for any reason,
you first, deactivate the swap.
And delete the actual Linux Swap File.
If you what increase or decrease a swap file
size, you created a new swap file of desired size
If you what only to removed the swap file, remove swapfile entry in the /etc/fstab file.
Swappiness and how to adjust the Swappiness value?
Swappiness is a property of the Linux Kernel
to define how often the Swap space will be used.
As you already know, RAM is faster than a hard drive.
So, every time your system need to use swap,
you will notice that some processes and applications will run slower.
However, you can adjust the system to use much more RAM than swap.
This can help improve overall system performance.
Normally, the default swappiness value is 60.
The smaller this value, the more of your RAM will be used.
Swap tendency is defined by this formula.
swappiness is here in this formula.
I am not going into technical details of this formula, you can pause and read it
if you are curious.
So, to check your curremt the swappiness value, run this command:
You should see the value of 60.
If you want to modify the default value, you need to edit the file /etc/sysctl.conf.
And add the following:
vm.swappiness=10
10 is the most commonly recommended value.
Also, this setting is recommended when you have more than 4Gb of RAM.
Save the file and close it with Ctrl+O and Ctrl+X keys shortcuts.
In order to apply the changes, you need to reboot the system.
Now, the swappiness values is ten.
This way your Linux kernel will use more RAM and less swap,
but it still will swap when your RAM memory gets critically full.
Linux swap is a technical concept but knowing how it works can improve the performance of the system.
if you have anything to add to this video
Let me know in the comments section.
Thank you for watching.
Không có nhận xét nào:
Đăng nhận xét