[Raspberry Pi] Resizing or disable Swap size

Raspberry Pi Swap Size

Users who stake on Raspberry Pis earlier than the Raspberry Pi 3 (such as the Original Raspberry Pi, Raspberry Pi Zero, and potentially the Raspberry Pi 2) may benefit from a significant increase in performance by enabling and/or increasing the size of the Raspberry Pi’s swap file.


Note: Using a swap file could potentially result in an increase in writes & reads to your Raspberry Pi’s SD card, possibly lowering the lifespan of the card. However in most cases Raspbian ships with a default swap file with a size of 100 MB and in this case we are simply increasing that to 2048 MB.


First check the default Swap size should be 100 MB.

1
2
3
4
$ free -m
total used free shared buff/cache available
Mem: 924 287 107 23 530 652
Swap: 100 1 99

Edit the file /etc/dphys-swapfile and modify the variable CONF_SWAPSIZE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
# use under either modified/non-advertising BSD or GPL license

# this file is sourced with . so full normal sh syntax applies

# the default settings are added as commented out CONF_*=* lines


# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap

# set size to absolute value, leaving empty (default) then uses computed value
# you most likely don't want this, unless you have an special disk situation
# CONF_SWAPSIZE=100

# Generally, when the memory is less than 2G, the swap partition should be twice the memory.
CONF_SWAPSIZE=2048

# set size to computed value, this times RAM size, dynamically adapts,
# guarantees that there is enough swap without wasting disk space on excess
#CONF_SWAPFACTOR=2

# restrict size (computed and absolute!) to maximally this limit
# can be set to empty for no limit, but beware of filled partitions!
# this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
# but is also sensible on 64bit to prevent filling /var or even / partition
#CONF_MAXSWAP=2048

Restart dphys-swapfile.service.

1
2
sudo /etc/init.d/dphys-swapfile restart
[....] Restarting dphys-swapfile (via systemctl): dphys-swapfile.service

Check Swap size have been up to about 2048 MB.

1
2
3
4
$ free -m
total used free shared buff/cache available
Mem: 924 287 107 23 530 652
Swap: 2047 1 2046

That should be enough swap to complete any future work.

Disable Swap

Disable and remove Swap by run the follow commands.

1
2
3
4
$ sudo dphys-swapfile swapoff
$ sudo dphys-swapfile uninstall
$ sudo update-rc.d dphys-swapfile remove
$ sudo apt purge dphys-swapfile

Then reboot your Raspberry Pi to make it enable.

1
$ sudo reboot

Last check the Swap size should be 0 MB.

1
2
3
4
$ free -m
total used free shared buff/cache available
Mem: 924 315 120 18 488 621
Swap: 0 0 0

References

[1] Enabling & Increasing Raspberry Pi Swap - Neblio - https://nebl.io/neblio-university/enabling-increasing-raspberry-pi-swap/

[2] Increasing Swap on a Raspberry Pi - Pi My Life Up - https://pimylifeup.com/raspberry-pi-swap-file/