Do linux swap partitions have to be available?

11-17-2023

Designed for programming, automatic code writing robot, free to open. This article introduces the knowledge about linux exchange partition. Many people will encounter such difficulties in the operation of actual cases. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Linux swap partition is not necessary; Linux system can run perfectly without swapping partition. In some cases, like embedded solutions, linux can run without swapping, and swapping partition with as much physical memory as possible is the best solution to avoid swapping.

Operating environment of this tutorial: linux7.3 system and Dell G3 computer.

Is linux swap partition necessary? Linux system can run perfectly without swap partition.

When you install Linux, the installation process may add swap partitions to the hard disk and the partition of Linux itself. But what is this exchange partition for? Is it really necessary?

What is exchange?

When the multitasking operating system runs out of physical memory, it cannot start a new process or allocate more memory to an existing process. In order to solve this problem, a special space block on local storage (usually hard disk drive) is set aside as a temporary storage area operating system, which releases the memory of new work. When an inactive process becomes active again, it will be swapped back from local storage to memory. This process is called exchange.

Is it really necessary to exchange space?

Is exchange space technically necessary?

No. Linux can work without it, and in some cases, like embedded solutions, Linux can run without switching. If the operating system enters a situation that it doesn't have, if there is enough physical RAM, the machine will run without any problems.

Is swap space actually necessary?

Although swapping slows down the computer (because reading and writing hard disk is much slower than accessing RAM), and having as much physical memory as possible is the best solution to avoid swapping, there are legitimate situations where swapping is needed. For example, during system boot, there may be some processes running (thus consuming system memory), but they can effectively hibernate for a long time. Or allocated memory, write it and then never use it again. In these cases, the swap algorithm will start to migrate the memory to the hard disk, thus releasing valuable system memory. This actually ensures that the maximum amount of RAM is available at any time, thus improving performance.

Extended knowledge

On machine

For the sake of completeness, it is worth mentioning that Linux also has a subsystem called memory loser (OOM). Instead of stopping the kernel when memory runs out, it will start killing processes to release resources. Its action is configurable.

How much swap space should I allocate?

There is no absolute rule, because hard disk space is cheap (USD/Gigabit), it is 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.

Add a swap without expanding 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, and the other is to use a swap file.

To use another hard disk drive, create a partition of this type. To use swap space immediately and permanently add swap space to the system, you need to edit the/etc/fstab file and add something like this:

The line of/dev/sdb2 has no exchange sw 0 0.

If your system is naming disks with unique identifiers, use the blkid command to get the ID list of devices. The line that installs swap space in/etc/fstab will look like this.

UUID = 036da155-1ea1-4392-b8d4-700f65aa1ead none swap sw 0 0

To exchange files, create a large blank file (using dd) on the file to be used for exchange. For example, to create a 1GB file, type = 10mh2112.

dd if = / dev / zero of = / store / swapfile bs = 1024 count = 1048576

Where/store/swapfile is the path of the swap file on the required file system.

As and additional swap partitions, the file now needs to be prepared with mkswap and then used 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

You can adjust the Linux kernel to define how to actively try to swap processes out of memory. This trend is controlled by a kernel variable called swappiness. 0 means that the kernel will avoid swapping as much as possible, while 100 means that how the kernel will use swap space is positive. The default setting for many Linux distributions is 60. You can use this command to change the number dynamically.

sudo sysctl vm。 swappiness = 10

To set the value permanently, you need to change (or add, if not) the swappiness variable in/etc/sysctl. Conf file.

That's all for the content of linux exchange partition. Thank you for reading. If you want to know more about the industry, you can pay attention to the Yisu Cloud website, and Xiaobian will output more high-quality practical articles for everyone!

Ask AI for details

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us