clockworkpi

[SWAP] Simple method to install a Swap file

A swap file can be very handy in the GameShell, especially when compiling new games… I’m still fairly new to Linux. I was considering adding a swap partition, but remembering the hassle when expanding the partition in CW 1.0, I decided that a swap file would be the simpler and safer route to go. A little research resulted in the following procedure:

  1. Create an empty file (1K * 1M = 1GiB).
sudo mkdir -v /var/cache/swap
cd /var/cache/swap
sudo dd if=/dev/zero of=swapfile bs=1K count=1M
sudo chmod 600 swapfile
  1. Convert newly created file into a swap space file.
sudo mkswap swapfile
  1. Enable file for paging and swapping.
sudo swapon swapfile

Verify by: swapon -s or top :

top -bn1 | grep -i swap

Should display line like: MiB Swap: 1024.0 total, 1024.0 free To disable, use sudo swapoff swapfile command.

  1. Add it into fstab file to make it persistent on the next system boot.
echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
  1. Re-test swap file on startup by:
sudo swapoff swapfile
sudo swapon -va

Note: This procedure was taken directly from the AskUbuntu.com.
Shutdown and Restart now take a little longer… It’s not really a problem for me.

2 Likes

Thank you so much for this!! This is a much needed tutorial that anyone compiling things should be aware of, especially when say, rebuilding Retroarch. No longer will we need to reduce the -j4 jobs down to two!
I’d be happy to have this almost as standard. Or even have a scripted toggle in settings to enable/disable the swap file.
Also haha I had no idea you were new to Linux. I just assumed you were a coder. Thanks again! I am bookmarking this!

Hehe… I was a coder for Mac OS. I have a fair amount of experience with Darwin, Apple’s variant of Unix; Very similar, but not quite Linux. It does make it easier for me to understand what I’m dealing with, but crippling when I expect something to work the same…

1 Like

Cool, thanks for this, @lasvegas!

Since I wasn’t sure about having the swap on all the time, I created four tiny scripts based on your instructions. I’ll share here in case anyone wants similar reusable tools. (Your step 5 would only work if in that same swap directory from before.)

swapsetup.sh (only run once)

sudo mkdir -v /var/cache/swap
cd /var/cache/swap
sudo dd if=/dev/zero of=swapfile bs=1K count=1M
sudo chmod 600 swapfile
sudo mkswap swapfile
sudo swapon swapfile
top -bn1 | grep -i swap

swapon.sh (to turn on)

sudo swapon /var/cache/swap/swapfile

swapoff.sh (to turn off)

sudo swapoff /var/cache/swap/swapfile

swapcheck.sh (to see if/how much in use)

top -bn1 | grep -i swap

1 Like

have a 512MB file is enough for big compile like retroarch/kernel/mesa/mupen

swap files can be show using swapon --show, and file creation by using fallocate command

i personally directly put the swap file in /

here is on / off oneliner :

sudo su - -c "fallocate -l 512M /swap && chmod 600 /swap && mkswap /swap && swapon /swap" && swapon --show

sudo su - -c "swapoff /swap && rm -f /swap"

arch port directly include an enabled 512MB swap file

we may have a look at Zswap to save a bit our sd cards life

2 Likes
sudo apt-get install dphys-swapfile
sudo vim /etc/dphys-swapfile

set CONF_SWAPSIZE to the value you want

sudo systemctl restart dphys-swapfile