Raspberry Pi Lab Press: The Swap partition is a special-purpose partition on a disk. When the physical memory of the system is not enough, part of the space in the physical memory is released for use by the currently running program. Those freed spaces may come from programs that have not operated for a long time. These freed spaces are temporarily saved to the Swap partition. When those programs are about to run, the saved data is restored from the Swap partition to memory.
Allocating too much Swap space will waste disk space, and too little Swap space will cause system errors. Generally, when the memory is less than 2G, the swap partition should be twice the size of the memory. For Raspberry Pi, it can be set to 2GB. Of course, the microSD card capacity and usage also need to be considered.
wrong approach
The following is the wrong way to do it! Wrong way! Wrong way! Important things said three times! ! !
For a long time, I thought that the swap of the Raspberry Pi was similar to that of ordinary Linux, so I modified the size of the swap partition of the Raspberry Pi according to the methods on some websites.
The familiar routine begins!
First, check the memory size:
1
2
3
4
|
$
free
-m
total used
free
shared buff
/cache
available
Mem: 976 35 226 12 714 864
Swap: 99 0 99
|
Then:
1
2
3
4
5
6
|
$
sudo
dd
if
=
/dev/zero
of=
/swapfile
bs=1024 count=102400
$
sudo
mkswap
/swapfile
$
sudo
chown
root:root
/swapfile
$
sudo
chmod
0600
/swapfile
$
sudo
swapon
/swapfile
$
sudo
vi
/etc/fstab
|
Next, add the following to the end of the fstab file:
1
|
/swapfile swap swap defaults 0 0
|
Check if there is swap space
1
|
$
free
-m
|
After restarting, you will find that the swap of the Raspberry Pi is back to restore.
the right way
If you did as above, just delete the swap partition:
1
2
|
$
sudo
swapoff
/swapfile
$
sudo
rm
/swapfile
|
Then delete the line you added in the /etc/fstab file.
Let's start to formally modify the size of the Raspberry Pi's swap:
1
|
sudo
vi
/etc/dphys-swapfile
|
Modify the value of CONF_SWAPSIZE to the size you want. Generally, when the memory is less than 2G, the swap partition should be twice the size of the memory!
Then, restart the dphys-swapfile file service:
1
|
sudo
/etc/init
.d
/dphys-swapfile
restart
|
Last view size:
1
2
3
4
|
$
free
-h
total used
free
shared buff
/cache
available
Mem: 976M 66M 801M 12M 108M 849M
Swap: 2.0G 0B 2.0G
|
postscript
While modifying the /etc/fstab file, I found these two sentences in the comments below:
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that
不过当时并不知道 dphys-swapfile 是什么,也不知道它在哪。后来通过搜索 dphys-swapfile 查阅到这篇文章 How to change Raspberry Pi’s Swapfile Size on Raspbian,终于找到了正确修改树莓派交换分区的方法。