Latest news from 5wire Networks

How to add swap space to Linux

Swap space acts as an extension of your computer’s physical memory (RAM) on Linux. When RAM becomes full, inactive data is temporarily moved to the swap space to free up memory for running programs.

Here’s how to add swap on Linux:

1. Check for Existing Swap:

Before creating new swap space, it’s wise to check if your system already has one. Open a terminal and run the following command:

swapon --show

This will display information about any active swap partitions or files. If the output is empty, your system likely doesn’t have swap.

2. Allocate Swap Space:

There are two main ways to create swap space: using a dedicated partition or a swap file. Here, we’ll focus on creating a swap file.

First, decide on the size of your swap file. A good rule of thumb is to allocate swap space equal to your physical RAM size.

Next, use the fallocate command to create a file for swap. Replace /swapfile with your desired filename and 1G with your chosen size in Gigabytes (GB).

sudo fallocate -l 1G /swapfile

3. Set Up Swap File:

Now, format the created file for swap usage:

sudo mkswap /swapfile

4. Enable Swap:

The swap file is ready, but it’s not active yet. Use the swapon command to enable it:

sudo swapon /swapfile

5. Make Swap Persistent (Optional):

By default, the swap file disappears after a reboot. To make it permanent across reboots, edit the /etc/fstab file:

sudo nano /etc/fstab

Add a new line at the end of the file with the following format, replacing /swapfile with your filename:

/swapfile swap swap defaults 0 0

Save and close the file.

6. Verify Swap:

You can verify if the swap is active using the swapon --show or free -h commands. The output should show your swap details.

Additional Notes:

  • Consider disabling automatic swap on boot if you’re using a separate swap partition. This can be done by commenting out the relevant line in /etc/fstab.
  • You can adjust the system’s “swappiness” value, which controls how aggressively the system uses swap space. A value of 10 indicates moderate swap usage. Edit /etc/sysctl.conf and add the line:
vm.swappiness=10

Remember to save the changes and run sudo sysctl -p for the modification to take effect.

25% DISCOUNT FOR LIFE

Life time discount when you purchase your first order. Applies to Web Hosting, Reseller Hosting, and Cloud Servers.

Free Domain included on yearly Web Hosting plans.

Use code WEBOFF at the checkout!

Terms and Conditions apply. Voucher code is only valid for new customers.