6 min read
Published on May 09, 2025
Recently, I started daily driving Linux Mint. It's been a mostly smooth ride, but yesterday, I learned a crucial lesson the hard way: what happens when you run out of RAM. Spoiler alert: it's not pretty.
Coming from the cozy worlds of Windows and macOS, I was used to the OS handling memory like a champ. If RAM got tight, the system would magically shuffle things around to swap or even gracefully terminate a misbehaving app. But on Linux, I have a lot more power (and responsibility). My system seems to freeze a lot and would require a manual reboot to get back to its normal state. After some debugging, I realized that my swap size is just 2Gb. When the RAM runs out, the OS will move inactive data to the swap. In my case, the swap was too small to make a meaningful difference.
This sent me down the rabbit hole of figuring out how to boost my swap space. The most obvious route seemed to be resizing the existing swap partition. However, that involves the headache of unmounting my current disk, which usually means fiddling with a bootable USB. While totally doable, I wasn't in the mood for that kind of adventure. Instead, I stumbled upon the magic of swap files.
Essentially, swap files are just regular files on your filesystem that the OS can use to offload memory when your physical RAM is full. Now, some hardcore Linux gurus argue that dedicated partitions are faster than swap files. But from what I've gathered, for most users, the performance difference isn't really a deal-breaker, especially with modern SSDs. So, I decided to give swap files a whirl, and here's how I did it on my Linux Mint setup.
First things first, let's see what we're working with. Pop open your terminal and type:
❯ swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-2 partition 1.9G 0B -2
Pay close attention to that TYPE
column. In my case, I only had a swap partition
and no existing swap file
.
Important Note: If you do see an existing swap file listed (it would say file
under TYPE
and likely be named /swapfile
or something similar), you'll need to turn it off before you can resize or replace it. You can do that with:
sudo swapoff /swapfile
# where '/swapfile' is the swap file that we are using
Only do this if you actually have an existing swap file you intend to modify or remove. If you're just adding a new swap file alongside a partition (like I did), you can skip this swapoff command for now.
Alright, let's create the file that will become our new swap space. I'm going to use the fallocate
command because it's generally much faster at creating large files than the older dd
command. Plus, it seems to be the preferred method these days.
I'll create my swap file in the root directory (/) and name it swapfile.
# Navigate to the root directory (optional, just shows where we are putting it)
# cd /
sudo fallocate -l 40G /swapfile
Here I've created a 40Gb file. This might be too much for some people. In my case, I have a lot of disk space, so why not.
We've got a big empty file, but the OS doesn't know it's meant for swapping yet. We need to format it specifically for that purpose using mkswap
:
sudo mkswap /swapfile
# You'll see output similar to this:
# mkswap: /swapfile: insecure permissions 0644, fix with: chmod 0600 /swapfile
# Setting up swapspace version 1, size = 40 GiB (42949668864 bytes)
# no label, UUID=280dbdc8-897c-425c-b10a-ee2c72d517a9
Notice that warning about "insecure permissions"? We'll fix that next.
It's crucial that only the root user can read and write to the swap file. Let's set the correct permissions:
sudo chmod 0600 /swapfile
Time to tell Linux to actually start using our new swap file:
sudo swapon /swapfile
Let's make sure our new swap file is active and recognized by the system. You can use either free -h
or swapon --show
(or both!):
❯ free -h
total used free shared buff/cache available
Mem: 30Gi 13Gi 12Gi 1.2Gi 6.1Gi 17Gi
Swap: 41Gi 0B 41Gi
at 04:41:52 PM
❯ swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-2 partition 1.9G 0B -2
/swapfile file 40G 0B -3
Look at that! My new 40GB swap file is listed, and the total swap in free -h
reflects the addition. You'll notice the PRIO
(priority) column. Linux uses this to decide which swap space to use first if you have multiple. Lower numbers (more negative) mean lower priority. By default, new swap files often get a lower priority than existing partitions, which is usually fine.
Okay, so our swap file is working, but if we reboot now, the system will forget all about it. We need to tell Linux to activate this swap file every time it starts up. We do this by adding an entry to the /etc/fstab
file.
Safety First! Let's back up the current fstab file, just in case something goes sideways:
sudo cp /etc/fstab /etc/fstab.bak
Now, open /etc/fstab
with your favorite text editor (I'm a vim guy, but nano is great too: sudo nano /etc/fstab
). Add the following line to the end of the file:
/swapfile none swap sw 0 0
Save the file and exit your editor.
Now, go ahead and reboot your system. Once it's back up, you can run swapon --show
or free -h
again to confirm that your swap file is automatically active. Success!
And there you have it! Creating and enabling a swap file on Linux Mint (or most Linux distros, really) is pretty straightforward once you know the steps. My system feels much more stable now, knowing I have that extra breathing room if my RAM usage spikes.
Speaking of RAM adventures, I've been hearing a lot about zRAM
lately. Apparently, some distributions like Pop!_OS configure it out of the box. I heard zRAM
is a better solution than swapfiles. I'll definitely be trying that out sometime soon and will probably write about that adventure too.