One of the strengths of a computer or server lies in its amount of RAM (Random Access Memory). RAM is a type of computer memory used to store data used by the operating system and running applications. RAM is a volatile type of memory, meaning data stored in it will be lost when the computer is turned off.
In contrast to the hard disk, which can store data even though the computer has been turned off. However, the RAM access speed is greater than the hard drive, which causes the server or VPS rental price to be more expensive for more RAM.
The primary function of RAM is to store data used by the operating system and run applications so that the operating system can access this data quickly. If the operating system or application requires data not stored in RAM, then the data must be retrieved from the hard disk, which is much slower than accessing data in RAM.
Therefore, RAM is essential for the performance of a computer system. The more available RAM, the more data can be stored in RAM, so the operating system and applications can run faster. This happens because the RAM has no queue process (bottleneck).
However, of course, larger RAM costs more to spend. If you want to save money with small RAM but good performance, the solution is to tweak it by adding Swap memory, often called virtual memory.
What is Swap Memory?
Swap memory is an area on the hard disk used as a substitute for RAM when the RAM capacity is total. Swap memory is used by the operating system to store data that is not currently being used so that the RAM can be used to run the currently running application.
However, as previously explained, the read and write speed of RAM is far above the speed of the hard disk, so the performance of swap memory cannot match the performance of actual RAM (physical RAM).
To manage swap memory, the operating system uses a paging algorithm that allows data not currently in use to be moved to swap so that the RAM can be used to run the currently running applications. This paging algorithm divides currently unused data into small blocks called pages, then stores those in the Swap.
When the operating system needs data stored in the Swap, it will read the page back to RAM to be used by the application currently running.
But keep in mind using a swap can reduce system performance because the hard disk is slower than RAM. Therefore, the operating system will try to use as much RAM as possible and only use a swap when the RAM is already full.
In addition, Swap memory can interfere with complex drive performance because the capacity for read and write speed will be used by Swap Memory. Therefore, the Swap memory must be adjusted to the application's needs to be run.
Advantages of using Swap Memory
Here are some of the advantages that you can get by using swap memory on Linux:
- Improves system performance: By using swap memory, the operating system can manage memory more efficiently and prevent application crashes due to memory shortages. This will improve overall system performance.
- Enhances multitasking capabilities: Swap memory allows multiple applications to run simultaneously. This allows users to run multiple applications such as web browsers, media player applications, text editors, and more simultaneously without performance degradation.
- Allows use of systems with limited RAM: Swap memory is very useful for systems with limited RAM capacity. By using swap memory, you are like having additional RAM, but in virtual form.
- Improves system stability: Swap memory can provide additional stability to systems experiencing excessive memory usage. This allows the system to avoid problems such as kernel panics and blue screens.
- Increases computing capability: Swap memory allows the user to add main memory to the system, thus increasing the computing capability of the system. This can help the system to handle a variety of more demanding tasks.
Disadvantages of Using Swap Memory
While swap memory is helpful in increasing system performance and enabling the use of more applications, there are some drawbacks to be aware of in using swap memory in Linux:
- Decreased performance: Swap memory can reduce system performance because the hard disk is slower than RAM. If the operating system uses Swap too often, system performance will be noticeably slower.
- Hard disk space usage: Swap memory uses hard disk space to store data, reducing the available space on the hard disk. If you have limited hard disk capacity, using swap memory can be a problem.
- Lack of swap memory: If the operating system uses Swap too frequently, the available swap memory may run out. This will cause the operating system to crash or freeze due to a lack of memory.
Therefore, it is necessary to pay attention to the disadvantages of swap memory in Linux before using it in the operating system.
What is the recommended Swap amount?
The amount of recommended Swap that is suitable depends on the application's needs and usage to be run on the system. So, for example, if your server uses 1 GB of RAM, the recommended swap size is around 1.5-2 GB. However, the recommended swap size is around 1.5 to 2 times the RAM size.
Configuring Swap Memory on Linux
Configuring Swap Memory on Linux is relatively easy; we only need to type a few commands without installing anything. When this article was written, we tested it on Ubuntu 20.04 and Centos 8 64 Bit operating systems with 1 GB of RAM.
Follow these steps to configure Swap with 2GB capacity.
Step #1: Create a Swap Partition
Create a Swap partition and allocate 2GB to it; use the command below to create a swap partition with the name /swapfile.
sudo fallocate -l 2G /swapfile
Step #2: Set Access Rights
Set the permissions of the swap file so that it can only be read and written by the root
user.
sudo chmod 600 /swapfile
Step #3: Format the File as Swap
Format the file /swapfile to file type Swap with the following command;
sudo mkswap /swapfile
Step #4: Enable Swap
Activate the file /swapfile
so that it can be read as swap memory by the operating system with the following command;
sudo swapon /swapfile
Step #5: Boot Configuration
Add the swap file so that it can always be activated when booting by adding a line to the /etc/fstab
file.
/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step #6: Manage Swap usage
Next, set how the operating system should use Swap in the sysctl
with the following command;
sudo sysctl vm.swappiness=10
The number 10 indicates that the operating system will tend to use RAM rather than swap unless the RAM is full.
Done. Now your Linux system already has 2GB of Swap memory. To verify whether the Swap has been read or not, use the following command;
swapon --show
If the output displays the Swap file that has been created, it means that the Swap configuration has been successful.
NAME TYPE SIZE USED PRIO
/swapfile file 2G 242.4M -2
We can also see the use of swap with the free -m command. The output is something like the one below.
total used free shared buff/cache available
Mem: 976 618 85 42 272 160
Swap: 2047 242 1805
Conclusion
Now you understand the concept of Swap memory on the Linux operating system and how to configure it. From the discussion above, we can conclude that swap memory is needed to speed up server performance for specific conditions. However, we still have to adjust to situations when to use Swap and when we don't need to use Swap.
The configuration method is also relatively easy without installing additional applications. With this article, we hope you can understand more deeply the Swap concept and how to configure it to suit the needs of the application that will be used on the server.
I hope this article helps.