Generally speaking, TF cards are used in Raspberry Pi 3 or ealier models. After Raspberry Pi 4, there is built-in storage, or EMMC, TF cards can also be used of course. But no matter which one, it is inevitable to be frequently read and written by the system. EMMC or TF cards are easily damaged by frequent reading and writing.
So, what steps can we take to reduce the use of built-in storage on your Raspberry Pi?
Place all unimportant reading and writing in memory.
Put
in memory:/var/run
if ! grep -q "/var/run" $FSTAB; then
echo "none /var/run tmpfs size=2M,noatime 0 0" >> /etc/fstab
fi
Create a memory disk to temporarily store temporary files that are frequently read or write:
if ! grep -q "/media/ram0" $FSTAB; then
echo "none /media/ram0 tmpfs size=64M,mode=777,noatime 0 0" >> /etc/fstab
fi
Put log directories in memory:
if ! grep -q "/var/log" $FSTAB; then
echo "none /var/log tmpfs size=4M,noatime 0 0" >> /etc/fstab
fi
if ! grep -q "/run/log" $FSTAB; then
echo "none /run/log tmpfs size=2M,noatime 0 0" >> /etc/fstab
fi
In Linux, swap is virtual memory. If the physical memory is not enough, the disk will be used as virtual memory.
If your Raspberry Pi does not occupy much memory during daily use, it is recommended to turn off swap.
Execute the following command to uninstall swap
dphys-swapfile swapoff
dphys-swapfile uninstall
systemctl disable dphys-swapfile.service
Many articles have mentioned that using fstrim for solid-state storage can extend its life. The principle is that when a file is deleted, in order to optimize performance, the solid-state storage (EMMC/TF card) will not immediately say that this block can be rewritten. If you don't mention this, the system will only write to the remaining space. Since each block of solid-state storage can be read and written a limited number of times, so from a probability point of view, these blocks will die faster.
Then there must be a time to talk about this matter, when? That's when using fstrim. In fact, fstrim rearranges the deleted space on the disk and tells the system that it can be rewritten.
It's quite simple to turn on fstrim in RaspberryPi:
systemctl enable fstrim.timer