Category Archives: Storage

Moving a Linux Swapfile to a Faster Disk

If you read my blog, you know that I like to install Linux on USB sticks and work entirely on those without using an HD. Recently, I noticed some periodic speed problems. I had a hunch, and I think I’m right, the problem was my swapfile was too slow, and maybe too small.

Maybe there’s something about swapping to USB that’s problematic, maybe because it’s not a typical sequential read/write operation. If the problem goes away, that’s a good indication this wasn’t a waste of time. Or it might just be a matter of splitting up the workload. USB probably wasn’t designed for multitasking lots of little changes to lots of files at the same time.

First, I started to look at the bandwidth of USB 3.x versus the bandwidth of a typical HD. Without going into tons of research, SATA is just faster than a USB 3.x drive. (I don’t have the latest PCIe SSD technology yet, that’s another blog post for another day.) As far as the speed of USB sticks versus SD cards, there’s not a huge difference. The bottleneck is still the transfer rate of the USB port.

Working off USBs for a few years now, I don’t really notice the difference much (of not using an internal drive) until the system needs to swap, like when I’m watching a movie or a particularly nasty page full of Javascript comes up. Like *cough* Facebook. I considered moving everything over to the HD, but I decided it wasn’t worth the effort.

How hard would it be to move my swapfile from my USB partition to my (faster) HD? It wasn’t too hard.

Just to be clear, my entire operating system lives on a Samsung Fit USB, along with a swap partition (which will be deleted at the end of this blog post.) And my HD was completely empty before adding the swapfile, which I’ll get to in a minute. Not that it needs to be empty, I’ll be adding more files to it. I just wanted to clarify what I’m doing here.

The first thing I did, I created a partition on my HD using gparted. I used a GPT partition table, chose ext4, and named my partition with a label. Next I followed these instructions, since I’m using Manjaro. According to that link, a swap partition offers no speed advantage. So I decided to go with a swapfile instead of a swap partition.

That link also shows you a chart of recommended swap file sizes. Since I have 16GB of RAM, it recommends a max swap file of 32GB. I like to hibernate my system more often lately as we’re having more power outages here in Texas, so I figure more swap space is better. Basically, you can double the amount of RAM if you don’t have the chart handy.

The next step was to mount the HD. I’m writing this from memory, it might not be perfect. Remember, we added the label in gparted when we formatted the HD:

cd /mnt
mkdir pjhd
mount /dev/disk/by-label/pjhd /mnt/pjhd
cd pjhd
fallocate -l 32G swapfile
chmod u=rw,go= swapfile
mkswap swapfile
swapon swapfile


Now the HD is mounted and the swapfile is created. But we want “pjhd” to mount automatically next time we boot, otherwise we won’t have our swapfile. (If it fails and you have no swapfile, it’s not the end of the world.) So we need to edit our /etc/fstab file to make this permanent.

I commented out my old swap partition and added this to the bottom:

# This first mounts the PC's hard drive to /mnt/pjhd where the swapfile is located
# Mounting a Hard Disk Permanently in Linux
# https://www.cyberpratibha.com/use-of-fstab-option-for-mounting-disk-in-linux-permanent/
/dev/disk/by-label/pjhd /mnt/pjhd ext4 defaults 0 0
# Followed instructions here https://wiki.manjaro.org/index.php?title=Swap#Using_a_Swapfile
/mnt/pjhd/swapfile none swap defaults 0 0


If it’s not obvious, this first mounts the internal HD at /mnt/pjhd and then (the last line) activates my 32GB swapfile which resides on the HD. We’re not totally done though. The last step is to run “gparted” and delete the old swap partition from the USB. Then resize the main USB partition to pick up some extra space.

To test it out, I opened up Thinkorswim (the Linux Java version is a monster) and it’s running smoother/faster. Before it had a lot of hangups.

Update: The “freeze up” problem is gone. Moving the swap file worked. However, the system acts weird now coming out of hibernation. The swap file isn’t found coming out of hibernation, but my apps restore OK. Strange. If I figure it out, I’ll do a follow-up post. Also I purchased a much faster USB drive (1TB Samsung) that has very high throughput. We’ll see how that goes.