How to add swap space in Linux?

Athreya aka Maneshwar - Sep 14 - - Dev Community

Tired of the same old Windows routine?

Tiling window managers like i3 and Sway offer a good workflow that can increase productivity instead of using Alt+Tab n no of times to switch between windows.

Recently, I convinced four of my friends to give Linux a try, and they've been loving it!

However, one common question that's popped up is how to create or increase swap space.

I got a bit tired of repeating myself, so I thought, 'Hey, why not write a quick guide?' That way, I can just share the link whenever someone asks. And who knows, maybe it'll help someone else out there too.

A Bit About Swap (For the Uninitiated)

Swap space is Linux's secret weapon for maintaining system stability and performance. It acts as a virtual memory extension, allowing processes to keep chugging along even when physical RAM is maxed out. Think of it as a temporary storage space for less-used data, freeing up RAM for more demanding tasks.

While swap space can be a lifesaver, it's important to remember that it's not a substitute for adequate physical RAM.

What is Swap Space?

Swap space, also known as swap memory or paging space, is a designated area on your hard drive (HDD or SSD) that the operating system uses to temporarily store inactive or less frequently used data from RAM. This frees up RAM for more critical processes that require more processing power.

The rate and aggressiveness of swapping are determined by a parameter called "swappiness." Operating systems like Windows and Linux typically provide a default amount of swap space, but you can adjust it to suit your needs. Disabling swap space altogether is possible, but it can lead to the kernel forcibly killing processes to make room for new ones.

List and Delete

To delete or disconnect an existing swap file in Ubuntu, follow these steps:

1. Check the swapfile which is being used:

cat /proc/swaps

lovestaco@i3nux-mint:~$ cat /proc/swaps
Filename                Type        Size        Used        Priority
/swapfile                               file        2097148     995840      -2
Enter fullscreen mode Exit fullscreen mode

Use the filename for turning off and deleting the file.
If there is no swap file, skip this section and move to Create and Use section.

2. Turn off the swap file:

First, you need to deactivate the swap file before you can remove it.

 sudo swapoff <filename>
Enter fullscreen mode Exit fullscreen mode
sudo swapoff /swapfile
Enter fullscreen mode Exit fullscreen mode

Replace /swapfile with the path of your existing swap file if it differs.

3. Delete the existing swap file:

Once the swap is turned off and removed from the fstab file, you can safely delete the swap file.

sudo rm <filename>
Enter fullscreen mode Exit fullscreen mode
sudo rm /swapfile
Enter fullscreen mode Exit fullscreen mode

Replace /swapfile with the correct path if necessary.

4. Verify that the swap is removed:

After turning off and deleting the swap file, check the swap status to confirm it's no longer in use:

sudo swapon --show
Enter fullscreen mode Exit fullscreen mode

This command should return no results if no other swap files or partitions are active.

You have now successfully disconnected and deleted the swap file.

Create and Use

To create an 8GB swap file in Ubuntu using the method you provided, follow these steps:

1. Create the swap file (8GB):

sudo dd if=/dev/zero of=/swapfile bs=1024 count=8388608
Enter fullscreen mode Exit fullscreen mode
Explanation:
  • if=/dev/zero: Input file is /dev/zero (a special file that provides as many null bytes as are read from it).
  • of=/swapfile: Output file where the swap will be stored.
  • bs=1024: Block size of 1KB.
  • count=8388608: Number of blocks (8388608 blocks * 1KB = 8GB).
Option 2: 4GB Swap File
sudo dd if=/dev/zero of=/swapfile bs=1024 count=4194304
Enter fullscreen mode Exit fullscreen mode
  • count=4194304: Creates a 4GB swap file (4194304 blocks * 1KB = 4GB)
Option 3: 16GB Swap File
sudo dd if=/dev/zero of=/swapfile bs=1024 count=16777216
Enter fullscreen mode Exit fullscreen mode
  • count=16777216: Creates a 16GB swap file (16777216 blocks * 1KB = 16GB)

2. Set the correct permissions for the swap file:

sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode

3. Set up the swap file:

sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode

4. Enable the swap file:

sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode

5. Make the swap file permanent by adding it to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Enter fullscreen mode Exit fullscreen mode

6. Verify that the swap is active:

sudo swapon --show
Enter fullscreen mode Exit fullscreen mode

You now have an 8GB swap file set up and activated on your system!
Enjoy the memory!

Also checkout: Thinking of switching? Tips from a user of 26 years.

. . . . . . . . . . . . .
Terabox Video Player