Linux Swap – An Alternative to PC Memory Not Enough [MiniTool Wiki]
What Is Linux Swap
Linux Swap (swap partition), similar to the virtual memory of Windows, is to virtualize part of the hard disk space into memory when the memory is insufficient. In this way, the memory insufficient issue can be solved.
Android is a Linux-based operating system, so you can also use swap partitions to improve system efficiency.
The swap partition refers to exchange. When the memory is insufficient, the operating system first stores unused data in memory into the swap space hard disk. Through this way, it frees up some memory to let other programs to run. This is the effect of swap partition. It is true for the virtual memory (pagefile.sys) of Windows.
Swap is a virtual memory partition under Linux. It is similar to the exchange file of Windows operating system, but it is a contiguous amount of disk space and is invisible to users. However, you need to notice that though this swap partition can be used as virtual memory its speed is much slower than that of physical memory.
Therefore, if you pursue a faster speed, you can rely on swap. However, the best way is still to expand physical memory. After all, swap partition is a temporary solution.
When to Use Linux Swap
When running an application, it uses RAM (Random-Acsess Memory).Your system will utilize available RAM to manage when only a few applications running. If the computer runs too many applications or the running applications requires much RAM, your system will fall into trouble.
What’s worse, if an application needs more memory when the entire RAM is in use, the application will crash. When the RAM is exhausted, swap acts as a respirator for your system. If that situation happens, the Linux system uses some of the hard disk memory and allocates it to the running application.
However, if an application relies too much on the swap, its performance will decline because it cannot read the data with the same speed with RAM. In other words, a little swap is beneficial, but it will be useless if applied too much.
Linux Swap Types
Linux provides two types of swap. Most of Linux installations will create a swap partition by default, but you can also use a specially configured files as exchange file. Swap partition – a standard disk partition that is designed as swap partition by mkswap command just as its name implies.
If there’s no available disk space to create a new swap partition in a volume group, you are able to use the exchange file, where you can create a logical volume for the swap space. It’s a regular file created and pre-allocated to the specified size.
The Best Linux Swap Size
As mentioned earlier, too much Linux swap is not helpful for improving the performance of application. So, to know the suitable Linux swap size is necessary and important. The following table will show you the recommended swap space according to the different amounts of RAM.
In fact, the swap space is determined by the number of RAM in the system and whether your computer needs abundant memory for your system to hibernate. Only if you instruct your computer to hibernate, the swap partition is used as the target for memory content.
|Amount of system RAM||Recommended swap space||Recommended swap with hibernation|
|Less than 2GB||2 times the amount of RAM||3 times the amount of RAM|
|2GB – 8GB||Equal to the amount of RAM||2 times the amount of RAM|
|8GB – 64GB||0.5 times the amount of RAM||1.5 times the amount of RAM|
|More than 64GB||Workload dependent||Hibernation not recommended|
The numbers in the table is just for your reference. If you want to improve the performance of your Linux, you are able to follow the above suggestion.
If you don’t have enough memory, you can use swap partition as an alternative. However, when the memory is not abundant and there’s no swap partition, it will result in the inability to allocate memory for requests that require more memory pages.
As the final solution, the Kernel will deploy OOM killer to nuke high -memory processes (often MySQL, java, etc.).