In some cases on VPS there is a situation when the operating system does not have enough physical memory to process all applications. In such cases, you can increase the amount of operating memory (RAM). Another option is to configure SWAP on the VPS.
What is SWAP memory
Swap is an area on a hard disk designed to store temporary operating system files when RAM is insufficient. It allows more efficient use of computing resources by virtually expanding the amount of operating memory. However, this procedure may affect hard disk performance.
There are two ways to connect Swap memory:
- using a SWAP file (this method is used in already installed systems when SWAP needs to be implemented);
- using a SWAP partition (when installing new servers - the setup is more complicated, but allows you to allocate a separate disk partition for SWAP memory).
When an operating memory swap is needed
Swapping is necessary under certain circumstances. When the operating system needs more memory than is currently available, swapping allows the kernel to free up the least-used memory pages and allocate them to the current application or process.
Also, a significant number of memory pages used by programs during the startup phase can be freed and moved to swap space, since these pages are only initialized and never used again. In this way, the system can optimize the use of operating memory.
How to check if SWAP is present in the system
A command designed to handle swap memory:
It not only provides the user with accurate information about the current swap space usage, but also helps to determine whether the space is a partition or a file.
Additionally, it is checked with the command:
Another way to get information about swapping in Linux is to use the
/proc/meminfo file, which stores information about system usage for running processes. You can also check swap using server monitoring tools like top, htop and others.
Advantages and disadvantages of SWAP
In the case of VPS, where there is no high competition for memory resources, the benefits of using this technology will be insignificant. The most you can do is to move data from RAM to swap data rarely used by applications. This will slightly free up valuable operating memory.
In addition, SWAP technology is useful on virtual servers (VPS) where there are periodic temporary spikes in operating memory utilization - it increases resilience to these kinds of resource anomalies.
A bit of history
In the past, SWAP technology was actively used when the cost of disk storage was much lower than the cost of operating memory. If the server lacked resources, it could temporarily use the allocated disk space. However, nowadays the cost of operating memory has decreased significantly, and there is no economy.
If the swap file or partition is stored on a hard disk drive (HDD), the speed of reading data from the HDD is much slower than from operating memory. Solid State Drive (SSD) is better, but the response time is still significantly faster than operating memory. So if you decide to save money on operating memory and rely entirely on SWAP, we strongly advise against it. SWAP is not a replacement for operating memory, but merely a supplement to it. Otherwise, there will be a decrease in performance.
As mentioned above operating memory is faster after all. Even though you have a state-of-the-art NVMe drive on your machine, RAM will still process information faster. Therefore, swapping should not be abused.
It is worth noting that swap can delay the call of Out-Of-Memory Killer on your operating system, and in certain situations activation of this process is necessary to terminate an application that poses a threat to the kernel and may cause a crash. Although these situations are quite rare, especially on modern versions of Linux, they are still possible.
The use of SWAP technology in modern systems raises doubts about its usefulness. Despite the opportunity to save on operating memory and protect against unexpected server crashes, the advantages of Swap are relative and have a number of disadvantages. Data is processed more slowly due to slower disk access speeds. This can lead to degradation of overall system performance. Therefore, the decision to enable SWAP is based on the specific requirements and characteristics of each individual system.
Tip from EuroHoster - always increase RAM according to system consumption. Never use SWAP if the RAM size is lower than recommended. This is not a solution!
During normal system operation, SWAP is not used at all or is used very little. If you observe persistently high SWAP consumption, we strongly recommend increasing the RAM on the server.