Useful Tips

SWAP - how to create, enable, clean, and disable the swap file on Linux

Pin
Send
Share
Send
Send


  • Nordweb on How to back up your phone firmware?
  • Victor on How to remove root rights? - Complete removal of root rights SuperSU
  • Stas on recording How to backup your phone firmware?
  • VLADIMIR Zaretskyi on Firmware firmware Xiaomi for Ubuntu
  • Sagit to write Xiaomi smartphone firmware in Ubuntu

What is Swap (swap file)

Swap is a memory mechanism during which inactive processes are unloaded from RAM to a hard disk, thus unloading it. This is useful if the amount of memory is strictly limited, in simple terms, if the memory is catastrophically small or not enough.

Swap can be presented in two versions:

  1. separate disk partition
  2. file on disk.

It is noteworthy that if your system runs on an SSD, then the performance of the swap file will not be much inferior to the access speed of the OP.

What is SWAP?

SWAP (swap) is a virtual memory mechanism in which part of the data from random access memory (RAM) is transferred to storage on the HDD (hard disk), SSD (solid state drive), flash drive or other secondary storage. As a rule, swapping occurs when the RAM is full, and it requires additional space to work.

Choose the size of the swap file

As Vladimir Pavlovich said - “Swap file size must be equal to the amount of RAM, or exceed it twice“. From the foregoing, we conclude that with a memory size of 1024mb, the size of the paging file will be 1024mb / 2048mb.

Using the dd console utility, fill the newly created file with zeros:

We initialize the Swap file with the mkswap command:

Congratulations, the Swap file is ready to use.

Turn on Swap with the swapon command:

Check if this is true:

When a swap may be needed

Often, the database suffers first from a lack of free memory. You may encounter problems like:

  1. Constant site crash: Error connecting to the database - means, for example, that MySQL has crashed,
  2. Upon careful examination of the /var/log/mysql.log logs, an InnoDB error is detected: Fatal error: cannot allocate memory for the buffer pool. It indicates that the database does not have enough RAM allocated for it to create a buffer.

With these symptoms and mistakes, swapping can help.

Benefits of SWAP

Comparison of the cost of RAM and SSD
(prices are valid on July 7, 2017)IhorFirstvds
RAM, on average for 1 gigabyte100 rubles per month170 rubles per month
SSD, on average per 1 gigabyte10 rubles per month13 rubles per month

As can be seen from the table, the savings will come out about 10 times.

Set priorities for memory usage in the system

Since now the system actually has two memory options - RAM and Swap, it will be nice to determine how the system will use them. Using the sysctl utility with the start parameter vm.swappiness, we specify at what stage of loading RAM (RAM) Swap will be used. The sysctl value can vary from 0 to 100. If you specify a maximum value of 100, the system will use only the OP and vice versa, if you specify 0, it will use only Swap.

I am of the opinion that it is worth choosing vm.swappiness = 30. In this case, when loading RAM at 70%, inactive processes will be unloaded in Swap.

To disable the swap file, use the command:

On this, the question of how to create a Swap swap file on Linux can be considered closed.

Check for swap in the system

To get started, you need to make sure that the swap is not connected yet:

If the command returned an empty result or something like:

- it means the swap is most likely absent.

Additionally, check the command:


If the table has 0 in the swap line, then there is no swap.

What size to create swap

Proceed from the principle - as much as you need, allocate as much. As a rule, you can start with the size of the RAM, or its doubled amount. For example, if you have 2 gigabytes of RAM in the system, you can make a swap of 2-4 gigabytes in size, as a rule, this should be enough. But, you can adjust its size for yourself.

Creating a SWAP File

Suppose we want to create a 4 gigabyte swap file.
Further, there are 2 approaches, traditional slow and new fast:

Quick way We use fallocate:

The result will be an empty string, this is normal.
Unlike dd, the result will be obtained almost immediately, and I recommend it. The traditional, slow way Or use the command:

The syntax of the command is simple:

  • dd - the team is designed to copy something byte-by-byte somewhere,
  • if = / dev / zero - indicates the source, i.e. to where we are copying from, in this case from / dev / zero - this is a special file on UNIX-like systems, which is a source of zero bytes,
  • of = / swapfile - indicates the destination path where we copy the data,
  • bs = 1G - the number of bytes to be written at a time. In our case, 1 gigabyte. Designations: G - gigabytes, M - megabytes, K - kilobytes, and so on,
  • count = 4 - how many bs-sized blocks will be created, in our case 4.

Be very careful with the command syntax, because if, for example, you make a mistake in defining the path for of = (where to save the file), you can damage the data on disk.

Now that the file is created, check the result:

As a result, we will see:
-rw-r - r-- 1 root root 4.0G Jul 07 16:16 / swapfile
As you can see, the file was created correctly and with the right size.

How to connect a SWAP file

To begin with, we will restrict access rights to the newly created file.

Allowing other users to read or write to this file will be a huge security risk, so limiting the command below is strictly necessary:

As a result, we should see something like this:
-rw ------- 1 root root 4.0G Jul 07 16:16 / swapfile
Rights are spelled out correctly.

Now you need to make a swap file from swapfile:

As a result, we will see something like the following:
Setting up swapspace version 1, size = 4193300 KiB
no label, UU>

Now, the file is ready as a swap. Connect it to the system:

If a similar error occurs at this stage, it means that most likely the swap is not allowed to be connected to the system. This limitation is often placed on VDS with OpenVZ virtualization. I recommend using IHOR, servers from 100 rubles per month and KVM virtualization allow you to enable Swap.

That's it, now the swap is up and running. It remains to verify the correct operation of the very first command:

The swap will also be visible in standard commands:

Everything, now for sure, the swap in the system is ready and will be used if necessary.

Pin
Send
Share
Send
Send