Athreya aka Maneshwar
Posted on September 14, 2024
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
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>
sudo swapoff /swapfile
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>
sudo rm /swapfile
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
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
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
- 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
- count=16777216: Creates a 16GB swap file (16777216 blocks * 1KB = 16GB)
2. Set the correct permissions for the swap file:
sudo chmod 600 /swapfile
3. Set up the swap file:
sudo mkswap /swapfile
4. Enable the swap file:
sudo swapon /swapfile
5. Make the swap file permanent by adding it to /etc/fstab
:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
6. Verify that the swap is active:
sudo swapon --show
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.
Posted on September 14, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.