Lecture
One of the easiest ways to increase server activity and prevent out of memory application errors is to add swap space. Swap is a partition on your hard disk in which the operating system can temporarily store data that it can no longer hold in RAM.
In principle, this makes it possible to increase the amount of information stored in the working memory, but with some reservations. The hard disk space will be used mainly when RAM is not enough to transfer data.
The information recorded on the hard disk will be loaded more slowly than the information stored in the RAM. The operating system runs the application data in RAM, and swap uses to store older data. In general, having a swap space in case the RAM overflows is a great way to play it safe.
This guide covers creating and activating a swap file on a Ubuntu 14.04 server.
Before you begin, you need to clarify whether swap space is activated on this system. In general, there may be several swap files or swap partitions within one system, but one should be enough.
To find out if there is active swap space on the current system, type:
sudo swapon -s
Filename Type Size Used Priority
If (as shown above) only the table header is returned, there is no active swap space on this system.
Another more familiar way to check swap space is the free utility, which shows the system memory usage. To find out the current state of memory and swap in megabytes, type:
free -m
total used free shared buffers cached
Mem: 3953 154 3799 0 8 83
-/+ buffers/cache: 62 3890
Swap: 0 0 0
As you can see, the total use of swap on the network is 0, which coincides with the result of the previous command.
As a rule, a specially designated area of the disk is used as swap space. However, changing the partitioning scheme is not always possible. In this case, the swap file can be created in an existing partition.
Before you start creating such a file, you need to know the current state of the disk. To obtain this information, enter:
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda 59G 1.3G 55G 3% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 2.0G 12K 2.0G 1% /dev
tmpfs 396M 312K 396M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 2.0G 0 2.0G 0% /run/shm
none 100M 0 100M 0% /run/user
As you can see in the first line of the result, 55 gigabytes is available on the hard disk, which is more than enough for a newly created medium-sized VPS; however, actual memory usage and server settings may vary.
There are many popular opinions about the "correct" size of the swap-space. In general, it depends on the personal preferences of the user and the requirements of the application. As a rule, the swap-space should be equal to or twice the amount of system memory.
In this matter, you need to focus on free disk space. For example, if the system's RAM is 4 gigabytes, and twice the swap space (i.e., 8 gigabytes) takes up a substantial part of the disk space, you need to create a swap of no more than 4 gigabytes.
After reviewing the important points of swap installation and finding out the available hard disk space, you can start creating a swap file in this file system.
In the manual, this file is called swapfile and is located in the root directory (/). The file must have the amount of swap space needed. There are two main ways to create a swap file.
As a rule, to create a file with a pre-allocated space, use the dd command — a universal utility that writes data from one location to another.
It can be used to write zeros to a file using a special Linux pseudo-device, located in / dev / zero, which simply yields the required number of zeros. Typically, this pseudo-device is used to create files with a specific length.
To specify the file size, use the combination bs (block size) and count (number of blocks). The values that are assigned to each parameter can be chosen almost arbitrarily. What matters is the result of multiplying these values.
For example, to create a 4GB file, you can assign bs = 1G, and count = 4:
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB) copied, 18.6227 s, 231 MB/s
Before pressing ENTER, check the command: if the parameter of of (which is decoded as “output file” - the output file) is set incorrectly, it can destroy the data of another file.
By typing the following command, you can see that 4 gigabytes have been allocated:
ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Apr 28 17:15 /swapfile
As you can see, the execution of this command takes a lot of time. In this example, it took the system 18 seconds to create the file. This is because she needed to write 4 gigabytes of zeros.
There is a faster way to write swap files; To try to create a file faster, delete the file you just created and explore the instructions in the following section:
sudo rm /swapfile
The fallocate program allows you to create such a file faster. This command instantly creates a file of a pre-selected size, without having to write dummy content.
So, to create a file in 4GB, you can type:
sudo fallocate -l 4G /swapfile
The result will return almost immediately. To make sure that the required amount of space is allocated for this file, type:
ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Apr 28 17:19 /swapfile
As you can see, everything was done properly.
The file has now been created, but the system does not yet know that it should be used as a swap space. You need to format this file and then activate it.
But before you need to change the rights to the file, so that only root has read permissions. A swap file with rights to read and modify by other users is a huge security risk to the server. To transfer all privileges to the root user, type:
sudo chmod 600 /swapfile
Make sure that the file permissions are now set correctly:
ls -lh /swapfile
-rw------- 1 root root 4.0G Apr 28 17:19 /swapfile
As you can see, only the root user has read and write permissions.
Now that the file is protected, you can point the system to swap space with the command:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=e2f1e9cf-c0a9-4ed4-b8ab-714b8a7d6944
Now the file can be used as swap. Activate it by typing:
sudo swapon /swapfile
Make sure that the procedure was successful by checking the system reports on the swap, as at the beginning of the article:
sudo swapon -s
Filename Type Size Used Priority
/swapfile file 4194300 0 -1
Now the system reports the activated swap file. You can also use the free utility to confirm the result:
free -m
total used free shared buffers cached
Mem: 3953 101 3851 0 5 30
-/+ buffers/cache: 66 3887
Swap: 4095 0 4095
As you can see, the swap space was added successfully; now the operating system can use it if necessary.
So, now the paging file is activated, but when you restart the server will not start it automatically. To change this, edit the fstab file with root privileges by opening it in a text editor:
sudo nano /etc/fstab
At the end of the file, enter a line that will automatically run the file you created:
/swapfile none swap sw 0 0
Save the changes and close fstab.
There are several options that can affect system performance when running swap.
The swappiness parameter sets the frequency with which the system uses swap space. The value of swappiness is expressed as a number from 0 to 100, which expresses percentages. If this value is close to zero, the kernel will not transfer data to the swap unless absolutely necessary. Remember, interaction with the swap file is “costly” for the server because it requires more resources than interaction with the RAM, which, consequently, can lead to a significant decrease in performance. The system, as a rule, works more productively, if it does not really rely on swap.
If the swappiness value is closer to 100, the system will transfer a large amount of data to the swap in order to save more free space in the RAM. Depending on the memory profile of the application and the purpose of using the server, this may be better in some cases.
To view the current swappiness value, enter:
cat /proc/sys/vm/swappiness
60
In general, a value of 60 is not so bad, but for a virtual dedicated server system it is better to set a value that is closer to 0.
To change the value of swappiness, use the sysctl command.
For example, to set the value to 10, you need to type:
sudo sysctl vm.swappiness=10
vm.swappiness = 10
This value will be valid until the next reboot. To preserve the value even after the server has been restarted, open the /etc/sysctl.conf file in a text editor:
sudo nano /etc/sysctl.conf
and add the following line to it:
vm.swappiness=10
Then save and close the file.
Another important option is vfs_cache_pressure. This parameter controls the kernel's propensity to recover memory, which is used to cache directories and inodes of objects.
Basically, this is file system access data. As a rule, they are not so easy to find, and they are often requested, so using the cache memory is very convenient in this case. You can find out the current value of this parameter by requesting the proc file system again:
cat /proc/sys/vm/vfs_cache_pressure
100
According to the current settings, the system removes inodes from the cache too quickly. Setting a lower value (for example, 50), the system will store information longer:
sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50
Again, this value is valid only for the current session. To make it permanent, you need (as is the case with swappiness) to open the sysctl configuration file in a text editor:
sudo nano /etc/sysctl.conf
and add the line to it:
vm.vfs_cache_pressure = 50
Save and close the sysctl.
The instructions in this manual will help you learn how to use memory efficiently. In addition, swap space can prevent some common system problems.
Faced with an OOM error (out of memory) or finding that the system cannot launch the necessary applications, it is better to try optimizing the application configurations or updating the server. Setting up a swap, however, gives you more flexibility and saves time when working with a less powerful server.
Comments
To leave a comment
LINUX operating system
Terms: LINUX operating system