Home > Article > Operation and Maintenance > Is linux swap partition necessary?
Linux swap partition is not necessary; Linux system can run perfectly without swap partition. In some cases, like embedded solutions, Linux can run without swap and the swap partition has all the advantages. As much physical memory as possible is the best solution to avoid swapping.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux systems can run perfectly without a swap partition.
When you install Linux, the installation process may add a swap partition to the hard drive as well as the Linux partition itself. But what is this swap partition used for, and is it really necessary?
What is exchange?
When a multitasking operating system runs out of physical memory, it cannot start new processes or allocate more memory to existing processes. To solve this problem, a special block of space on local storage (usually the hard drive) is set aside as a temporary holding area for the operating system. This frees up memory for new work. When the inactive process becomes active again, it is swapped back to memory from local storage. This process is called swapping.
Is swap space really necessary?
Is swap space technically required?
No. Linux can work without it, and in some cases, like embedded solutions, Linux can run without swap. If the operating system gets into a situation where it doesn't have enough physical RAM, the machine will run without any problems.
Is swap space actually required?
While swapping slows down the computer (because reading and writing to the hard drive is much slower than accessing RAM), and having as much physical memory as possible is the best solution to avoid swapping, there are Legal situations require an exchange. For example, during system boot, there may be some processes running (thus consuming system memory) but effectively sleeping for a long period of time. Or allocated memory, written to and then never used again. In these cases, the swap algorithm will begin migrating that memory to the hard drive, freeing up valuable system memory. This effectively ensures that the maximum amount of RAM is available at all times, thus improving performance.
Expand knowledge
On the machine
For the sake of completeness, it is worth mentioning that Linux also A subsystem called Out of Memory (OOM), instead of stopping the kernel when memory is exhausted, will start killing processes to free up resources. Its actions are configurable.
How much swap space should I allocate?
There are no absolute rules, and since hard disk space is cheap ($/gigabit), it's best to allocate too much space. As a rule of thumb, you should have two to three times as much swap space as physical memory.
Adding swap without extending the swap partition
There are two ways to add swap space to the system without changing the existing swap partition. One way is to use a swap partition on another drive, the other is to use a swap file.
To use another hard drive, create a partition of type To use swap space immediately To permanently add swap space to your system, you need to edit the /etc/fstab file and add something similar to this:
Line no swap for /dev/sdb2 0 0
If your system is using unique identifiers to name disks, use the blkid command to obtain a list of device IDs. The line to mount the swap space in /etc/fstab will look like this. :
UUID = 036da155-1ea1-4392-b8d4-700f65aa1ead none swap sw 0 0
To use the file for swap, create a large blank file (using dd) on top of the file you want to use Swap file system. For example, to create a 1GB file, type = 10mh1112
dd if = / dev / zero of = / store / swapfile bs = 1024 count = 1048576
Where /store/swapfile is the path to the swap file on the desired file system.
As with the attached swap partition, the file now needs to be prepared for use with mkswap and then with swapon. It also needs to be added to the /etc/fstab file. The process is exactly the same as above, but you need to use /store/swapfile instead of /dev/sdb2 as the swap space parameter.
Swappiness
The Linux kernel can be tuned to define how aggressively it attempts to swap processes out of memory. This trend is controlled by a kernel variable called swappiness. 0 means that the kernel will avoid swapping whenever possible, while 100 means that the kernel will be aggressive in how it uses swap space. The default setting for many Linux distributions is 60. The number can be changed dynamically using this command. :
sudo sysctl vm。swappiness = 10
To set the value permanently, you need to change (or add, if not already have one) the swappiness variable in /etc/sysctl . conf file.
Recommended learning: Linux video tutorial
The above is the detailed content of Is linux swap partition necessary?. For more information, please follow other related articles on the PHP Chinese website!