“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Hello, I’m looking at the mountains.

I have been using Ubuntu for nearly a year. After the recent reinstallation of 16.04, every day till 5:00 PM, I would find hundreds of megabytes written into Swap space, 8G system memory, SSD hard disk, I5 processor, mid-range configuration, and no large software started. IDEA is used for development, although there is no impact, but in line with a heart of knowledge, Google, the first article is “All About Linux Swap Space”, big tone, direct translation. (This article was written in May 2016, five years ago, but the Linux architecture has not changed, so this article is still correct.)

linux-swap-and-drop_cache

Linux calls random storage RAM memory pages. Swap technology is to copy a page of memory to swap space on a preset hard disk to release the occupied memory of the page. The sum of physical memory and swap space is the total amount of virtual memory available. Switching techniques are important for two reasons. First, when the system needs more memory than physical memory, the kernel can write the less used memory pages to swap space, freeing up memory for the current application (process) to use. Second, the memory pages used by an application at startup may only be used during initialization and will never be used again. The operating system can write these pages into swap space and use the free memory for other applications or as disk caching. But there are also downsides to switching technology. The read/write speed of a hard disk is slower than that of the memory. Memory read/write speed can be measured in nanoseconds, but hard disk speed can be measured in milliseconds. Hard disk access speed is thousands of times slower than memory access speed. The more swaps that occur, the slower the system runs. Sometimes there will be excessive swapping or jitter of pages written to and written to as the system tries to keep applications running and find free memory. In this case, the only solution is to increase RAM. Linux has two forms of swap space: swap partitions and swap files. A swap partition is a separate hard drive with no files or content. A swap file is a special file in the file system, separate from the system and data files. To view the swap space, run the swapon -s command. The following output is displayed:

Filename  Type       Size       Used Priority
/dev/sda5 partition  859436  0       -1
Copy the code

Each line lists the swap space being used by the system. The ‘Type’ field here indicates that the swap space is a partition and not a file, and ‘Filename’ indicates that the swap partition is disk SDA5. The ‘Size’ field indicates the disk Size, in KB, and the ‘Used’ field indicates how much swap space is Used. The ‘Priority’ field indicates the swap space usage Priority of the Linux system. One important feature is that if you mount two (or more) swap Spaces with the same priority (preferably two different devices) on a Linux system, Linux will use them interchangeably, which can improve swap performance.

Swap partition

To add an additional swap partition to your system, you first need to prepare one. The first step is to make sure the partition is marked as a swap partition, and the second step is to format it to the swap file system. Mark the partition as swap and run as root:

fdisk -l /dev/hdb
Copy the code

Replace ‘/dev/hdb’ with the disk of your swap partition. The output is similar to:

Device Boot    Start      End           Blocks  Id      System
/dev/hdb1       2328    2434    859446  82      Linux swap / Solaris
Copy the code

If the partition is not marked as swap, you need to declare this using the command fdisk and parameter t. Be careful with partitions. You never want to delete important partitions or change the identity of the system partition. Data on the swap partition is lost, so each change requires multiple confirmations. Also note that Solaris uses the same ID as the Linux swap space, so be careful not to kill the Solaris partition. If the partition has been marked as swap, you need to run the mkswap command as root:

mkswap /dev/hdb1
Copy the code

If it runs without errors, your swap space is used. Activate now:

swapon /dev/hdb1
Copy the code

Swapon -s can be used to confirm whether it is running. In order to automatically mount swap space when the system starts up, you need to add a few columns of configuration in the ‘/etc/fstab’ file. Swap space is a special file system and many parameters are not available. Such as:

/dev/hdb1       none    swap    sw      0       0
Copy the code

Check your swap space without restarting, you can run swapoff -a, then run swapon -a, then check by swapon -s.

Swap file

Like swap partitions, Linux supports swap files, which you can create, prepare, and mount as swap partitions. The advantage of swapping files is that you don’t need to find an empty partition or add an extra swap partition disk.

Create an empty file using the dd command. Create a 1GB file, for example:

dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Copy the code

‘/swapfile’ is the name of the swapfile, and 1048576 of ‘count’ is the file size, in kilobytes. To prepare the swap file, use the mkswap command, similar to preparing a partition, this time using the same swap file:

mkswap /swapfile
Copy the code

Similarly, mount the swap file using the swapon command:

swapon /swapfile
Copy the code

In ‘/etc/fstab’ type the following:

/swapfile       none    swap    sw      0       0
Copy the code

Swap the size of space

If you have a lot of memory, chances are no swap space and the system will work fine. But if physical memory runs out, the system will crash because it has no other relief, so it’s best to provide swap space, especially since disk is much cheaper than memory. The key question is how much memory? Older UniX-like operating systems required two or three times as much swap space as physical memory. Today’s extended versions (like Linux) don’t need this much, but if you configure it, they’ll use it too. The important principles are as follows:

  1. For desktop systems, using twice as much swap space as the system memory will allow you to run a large number of applications (which may have a lot of spare ones), leaving more RAM for major applications;
  2. For servers, use a small amount of swap space (usually half of physical memory) so you can monitor the size of swap space to alert you to the need for additional RAM;
  3. For older desktops, use as much swap space as possible

A new kernel parameter ‘swappiness’ has been added to the Linux 2.6 kernel. Administrators can use this parameter to change the Linux switching mode. Parameter values range from 0 to 100. Essentially, the larger the value, the more pages will be swapped; The smaller the value, the more applications reside in memory while swap space is free. Kernel maintainer Andrew Morton has said that he sets swappiness to 100 on his desktop, saying, “My opinion is that it’s wrong to reduce swapping by kernel parameters. You don’t need hundreds of megabytes of useless applications hogging up memory. Put it on disk and save the memory for something useful.” One flaw in Morton’s idea is that if the memory swap is too fast, the application response will be slow, because when the application window is clicked, the application is reading into memory from the swap space, and it will feel slow. The default ‘swappiness’ value is 60. You can use the root command to adjust the parameters (for reboot) :

echo 50 > /proc/sys/vm/swappiness
Copy the code

If you want to make the parameters permanent, you need to change the ‘vm.swappiness’ parameter in ‘/etc/sysctl.conf’.

conclusion

Managing swap space is an important aspect of system administration. There are many benefits to good planning and proper use of switching technology. Don’t be afraid to experiment, and always monitor your system to make sure you’re getting the results you need.

Write in the last

As of now, both memory and SSD are starting to drop in price, and it’s pretty easy to save a machine up to 8GB (RAM)+ 120GB (SSD), so swap space is pretty much useless on an individual user’s desktop, but as mentioned above, if you don’t swap space, the machine will die when you run out of memory. SSD is not recommended to be divided into multiple partitions. Therefore, swap file is recommended. In addition, you can create more swap files to improve swap performance.

Recommended reading

  • What are microservices?
  • Microservices programming paradigm
  • Infrastructure for microservices
  • Feasible solutions for service registration and discovery in microservices
  • From singleton architecture to microservice architecture
  • How to effectively use Git to manage code in microservices teams?
  • Summary of data consistency in microservice systems
  • Implementing DevOps in three steps
  • System Design Series how to Design a Short Chain Service
  • System design series of task queues
  • Software Architecture – Caching technology
  • Software Architecture – Event-driven architecture

Hello, I’m looking at the mountains. Swim in the code, play to enjoy life. If this article is helpful to you, please like, bookmark, follow. Welcome to follow the public account “Mountain Hut”, discover a different world.