In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This tutorial discusses the concept of swap files in Linux, why it is used, and its advantages over traditional swap partitions. You will learn how to create swap files and resize them.
What is Linux Exchange File?
Swap files allow Linux to emulate disk space as memory. When your system starts running out of memory, it uses swap space to swap some of its contents onto disk space. This frees up memory for more important processes. When memory is free again, it swaps data back from disk. I recommend reading this article to learn more about Swap Space on Linux.
Traditionally, swap space is a separate partition on disk. When installing Linux, simply create a separate partition for swapping. But that trend has changed in recent years.
With swap files, you no longer need separate partitions. You create a file in the root directory and tell your system to use it as swap space.
With dedicated swap partitions, resizing swap space is a daunting and impossible task in many cases. But with swap files, you can resize them at will.
The latest versions of Ubuntu and some other Linux distributions have started using swap files by default. Even if you don't create swap partitions, Ubuntu will create a swap file of around 1GB itself.
Let's look at more information about exchanging files.
Check the swap space for Linux
Before you start adding swap space, it's a good idea to check if swap space is already available in your system.
You can check it with the free command on Linux. In my case, my Dell XPS has 14GB of swap capacity.
free -h total used free shared buff/cache availableMem: 7.5G 4.1G 267M 971M 3.1G 2.2GSwap: 14G 0B 14G
The free command gives you the size of the swap space, but it doesn't tell you whether it's a real swap partition or a swap file. Swapon commands are better in this respect.
swapon --showNAME TYPE SIZE USED PRIO/dev/nvme0n1p4 partition 14.9G 0B -2
As you can see, I have 14.9GB of swap space on a separate partition. If it is an exchange file, the type should be file rather than partition.
swapon --showNAME TYPE SIZE USED PRIO/swapfile file 2G 0B -2
If your system does not have swap space, it should display the following:
free -h total used free shared buff/cache availableMem: 7.5G 4.1G 267M 971M 3.1G 2.2GSwap: 0B 0B 0B
The swapon command does not display any output.
Creating swap files on Linux
If your system doesn't have swap space, or if you think there isn't enough swap space, you can create swap files on Linux. You can also create multiple swap files.
Let's see how to create swap files on Linux. I use Ubuntu 18.04 in this tutorial, but it should work with other Linux distributions as well.
Step 1: Create a new interchange file
First, create a file with the required swap space size. Suppose I want to add 1GB of swap space to my system. Use the fallocate command to create a file with a size of 1 GB.
sudo fallocate -l 1G /swapfile
It is recommended that only root be allowed to read and write the swap file. When you try to use this file for swap zones, you'll even see warnings like "unsafe permissions 0644, advice 0600."
sudo chmod 600 /swapfile
Note that the name of the swap file can be arbitrary. If you need more than one swap space, you can give it any suitable name, such as swap_file_1, swap_file_2, etc. They are just a file of predefined size.
Step 2: Mark the new file as swap space
You need to tell Linux that the file will be used as swap space. You can do this with the mkswap tool.
sudo mkswap /swapfile
You should see output like this:
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)no label, UUID=7e1faacb-ea93-4c49-a53d-fb40f3ce016a
Step 3: Enable Swap Files
Your system now knows that the file swapfile can be used as swap space. But it's not finished yet. You need to enable the swap file so that the system can start using it as a swap.
sudo swapon /swapfile
Now, if you check Swap Space, you should see that your Linux system recognizes and uses it as Swap Space:
swapon --showNAME TYPE SIZE USED PRIO/swapfile file 1024M 0B -2
Step 4: Make the change permanent
Everything you've done so far has been temporary. Restart the system and all changes will disappear.
You can make changes persistent by adding the newly created swap file to the/etc/fstab file.
Before making any changes to the/etc/fstab file, it is best to make a backup.
sudo cp /etc/fstab /etc/fstab.back
Now add the following line to the end of the/etc/fstab file:
/swapfile none swap sw 0 0
You can do this manually using a command-line text editor, or with the following commands:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Now everything is ready. Even after restarting your Linux system, your swap files will be used.
Adjust the happiness parameter
The swappiness parameter determines how often swap space is used. Happiness values range from 0 to 100. A higher value means that swap space will be used more frequently.
The default happiness for Ubuntu desktops is 60, while the default happiness for servers is 1. You can check happiness with the following command:
cat /proc/sys/vm/swappiness
Why should servers use low happiness values? Because swap space is slower than memory, you should use as much memory as possible for better performance. On servers, performance factors are critical, so happiness should be as low as possible.
You can change happiness dynamically using the following system commands:
sudo sysctl vm.swappiness=25
This change is only temporary. To make it permanent, edit the/etc/sysctl.conf file and add the happiness value to the end of the file:
vm.swappiness=25
Resizing Swap Space on Linux
There are several ways to resize swap space on Linux. But before you see that, you should know a few things about it.
When you ask the system to stop using swap files for swap space, it transfers all data (memory pages, to be exact) back into memory. So you should have enough free memory before you stop swapping.
This is why it is a good practice to create and enable another temporary swap file. This way, when you close the original swap space, your system will use temporary swap files. You can now resize the original swap space. You can delete temporary swap files manually or leave them there and delete them automatically the next time you start them.
If you have enough free memory or created temporary swap space, close your original swap file.
sudo swapoff /swapfile
You can now change the file size using the fallocate command. Let's say you change its size to 2GB:
sudo fallocate -l 2G /swapfile
Now mark the file as swap space again:
sudo mkswap /swapfile
And enable swap files again:
sudo swapon /swapfile
You can also choose to have multiple swap files at once.
Delete swap files in Linux
You may have reasons not to use swap files on Linux. If you want to delete it, the process is similar to the resize swap process you just saw.
First, make sure you have enough free memory. Now close the swap file:
sudo swapoff /swapfile
The next step is to delete the corresponding entry from the/etc/fstab file.
Finally, you can delete the file to free up space:
sudo rm /swapfile
Did you use the swap space?
I think you now have a good understanding of the swap file concept in Linux. Now you can easily create swap files or resize them as needed.
If you have anything to add or questions about this topic, please leave a comment below.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.