Linux basics: SWAP file
19-05-2024
Last updated
19-05-2024
Last updated
Swap is a portion of the servers hard drive which is allocated to temporarily hold data that is not currently used by the server RAM. It acts as additional virtual memory for when the physical RAM is full.
I will be adding 4GB swap to my Ubuntu instance with 8GB RAM.
Check if any swap space is currently enabled on the server
If there's no output on your terminal, it means no swap space is active on the server.
Decide on the size of the swap file you want to create
Secure the swap file
Set the swap area
Enable the swap file
Verify that the swap is active
To ensure the swap file is used on boot, you need to add it to /etc/fstab
.
Backup the current file
Add the swap file to the /etc/fstab
file:
Set the swappiness value (Optional)
Swappiness controls how much your server makes use of the allocated swap space. Lower values results in the server using swap less often, which can make it faster. Higher values make the system use swap more often, which can keep more RAM free.
Check the server RAM and SWAP
Enabling swap allows you to fine-tune how your system balances performance and processes that require memory in either form of RAM or Swap. Having swap space is a practical strategy for maintaining smooth and efficient operations on your Linux server.