The Swap partition is something that is debated by computer enthusiasts today within Linux. The Swap partition allows the system to suspend when not in use, hibernate, and it is instrumental in swapping out pages when RAM space is sparse. Swap is the block of space reserved on a hard drive/solid state drive that allows caching when the memory is filled during heavy read/write jobs or during normal use when such an event occurs. Regardless of how much RAM you might have, it is a good idea to at least have one an half times that in swap, however, it is possible to run your computer just fine with the bare minimum of 2GB of swap. Swap is reminiscent of the Windows Page file, however, Windows usually creates a separate file for hibernating and suspend called Hiberfil.sys.
Also known as virtual memory, Swap files are small portions on the hard disk which do not count as a separate partition, however, they are bits of space cordoned off by the command fallocate –length swapsize /swap file(Screen shots below). This cordoned off space lets the system use everything but that space for storage, but it sets that apart to use in the event that a program or multiple programs take all of the available space in memory, this isn’t the same as taking all the memory as the system also sets aside a cache of RAM for its own purposes. The larger the desktop, the larger the amount of RAM it might set aside. Windows and Macs do this also. The RAM set aside is mostly for the Kernel space. Kernels these days attempt to use as minimal an amount as possible, however, today, most computers contain anywhere from 16GB to 64GB of Memory or more depending upon their use case.
Issuing the free command or free -h will allow you to determine how much memory/swap is being used on your Linux device. Using the command swapon -s will alert you to the device or partition that is being accessed as swap. Usually swap will come with a priority as well. This priority tells the Linux kernel to use the device with the highest priority first, much like a first come, first serve sort of thing, although, this setting can be manually changed by the user with swapon -p followed by the number. Swappiness is another factor to take into consideration when creating the swap file. Will my system use the adequate amount of memory before swapping to the disk? Swappiness in Linux desktops usually start at around 60 percent. This is too much for modern desktops and old ones alike. Setting the swappiness value lower is key for greater performance over all, when using a small swap file, it is recommended that the swappiness is set to something like 5 or below. Swappiness can be set from anywhere from 1 to 100 so using the lowest number possible is best.
To create a swap file do the following:
Then to make it mount at startup:
I recently added a function in my scripts that do this for you should the distribution not do it automatically at install. You can find those here.