SWAP Memory in Linux

Short version: SWAP is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the SWAP space. Swap space can take the form of either a dedicated swap partition or a swap file. In most cases, when running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file.

Bogdan Tudorache
5 min readJan 27, 2023

If you’re interested just in the code, skip the long version.

UPDATED: Based on feedback from Reddit

Long Version: Swap memory, also known as virtual memory or swap space, is a portion of a hard drive that is used as an extension of a computer’s physical memory (RAM). The main purpose of swap memory is to provide additional memory resources to the system when the physical memory (RAM) is full.

When the system runs out of physical memory, it will start to use the swap memory. The operating system will move inactive parts of the physical memory to the swap space, freeing up RAM for active processes. When a process needs more memory than is available in RAM, the operating system can move inactive parts of the physical memory back to RAM and give the process the memory it needs.

This process is called “swapping” and it can slow down the system performance because accessing the hard drive is slower than accessing RAM. However, swap memory allows the system to continue running even when the physical memory is full, preventing a system crash or a process from being terminated due to lack of memory.

Swap memory can be created as a separate partition on a hard drive or as a file on a file system. The amount of swap memory that you should use depends on the amount of physical memory in your system and the workload that you expect to run on it. Typically, it is recommended to have at least 2GB of swap memory, but it can vary depending on the use case.

It’s important to note that swap memory is not the same as a RAM disk, which is a portion of RAM that is used as a fast-access storage.

Adding a Swap file

Create a file that will be used for swap:

sudo fallocate -l 2G /swapfile

Only root user can be able to write and read the swap file. So we must set the permissions as such:

sudo chmod 600 /swapfile

Use the mkswap utility to set up the file as Linux swap area:

sudo mkswap /swapfile

To make the change permanent open the /etc/fstab file and append the following line:

vi /etc/fstab
/swapfile swap swap defaults 0 0

Reboot for changes to take into effect

sudo reboot

Check that the swap is active after logging in:

sudo free -h

Result:

**Congrats, you’re done!**

Alternatively you can also temporarily enable swap using the following command: swapon

swapon is a Unix/Linux command used to enable swapping on a specified file or device. In a computer operating system, swapping is a technique for increasing the amount of available memory (virtual memory) by temporarily transferring data from the RAM to disk.

The swapon command is used to specify the file or device to be used as swap space. Once activated, the specified file or device becomes available as swap space that can be used by the operating system for memory management purposes.

This will be available until the next reboot.

To begin with you have to know that swaponis used for a partition of your server’s hard drive. So how do we check what partitions we have installed? Nothing more simple, we use the ‘lsblk’ command, and keep it mind it should have at least a couple of Gb.

Enable swapon :

# sudo swapon <device>
# Where <device> is the file or partition to be used for swap.
# For example, to enable swap on /dev/sda3,
# you would run the following command:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo swapon /swapfile

Adjust Swappiness value

Swap memory is used as an overflow area for physical memory, so when the system runs out of physical memory, it will start to use the swap memory.

The kernel uses a "swappiness" value to determine how aggressively it should use swap space, which is a value that ranges from 0 to 100.

Setting it to 0 means that the kernel will avoid using swap space as much as possible, while setting it to 100 means that the kernel will aggressively use swap space. So, it is not only used when the amount of physical RAM memory is full, but also based on the swappiness value.

Swappiness can have a value between 0 and 100.

The default swappiness value is 60.

  • You can check the current swappiness value by typing the following command:
cat /proc/sys/vm/swappiness
  • While for Linux systems that is acceptable, if you do not want your system to aggressively use swap memory (not as fast as RAM) you can set it to 10 by using below command, but keep in mind that this will make the change only for the current running session.
sysctl vm.swappiness=10
  • Alternatively you can append the following line to the end of the /etc/sysctl.conf file : vm.swappiness=10 and to make this parameter persistent across reboots you must restart the Linux machine
  • Restart to keep changes persistent
sudo reboot now

Remove Swap file

First we have to deactivate the swap by typing:

sudo swapoff -v /swapfile

Remove the swap entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Last but not least, delete the actual swap file with the rm command:

sudo rm -rf /swapfile

Conclusion

We have learned how to create a swap file, activate/deactivate it and configure swap space on your Linux system.

Bogdan Tudorache | Founder @ berrynews.org

If you like the article and would like to support me, make sure to:

👏 Clap for the story (50 Claps) to help this article be featured

🔔 Follow me Bogdan Tudorache

📰 Find more tech content in Tech & ML Articles

🔔 Connect w/ me: LinkedIn | Reddit

Midjourney generated image on minimalism Swap Memory

--

--

Bogdan Tudorache

Consistency and Continuity. You can expect weekly tech articles from me. I am a developer, founder and integration engineer