Follow by Email

Blog Archive

Search This Blog

Setup and deactivate swapping on Raspberry Pi

Raspberry Pi Swapping is the offloading of non-used or seldom used parts of the main memory to a separate memory area on a hard disk. Either in a separate partition or in a file. Both are the swap memory. Under Windows, the swap memory is also known as paging file.
Share it:
Set up and deactivate swapping on Raspberry Pi
Raspberry Pi swapping

Swapping is the offloading of non-used or seldom used parts of the main memory to a separate memory area on a hard disk. Either in a separate partition or in a file. Both are the swap memory. Under Windows, the swap memory is also known as paging file. The main difference is that the swap file in Windows can in principle grow arbitrarily, while the swap memory, whether as a partition or file, has a static size.

Swapping makes sense for two reasons:
  • Linux can use a swap space to swap out rarely used storage areas out of RAM. This is the case when loading programs whose code is only partially used. Then there are code parts in the RAM that are rarely or not retrieved from memory, but still occupy the memory space. Swapping allows Linux to swap parts of the used memory into swap space.
  • When Linux runs out of memory, it tries to move active programs into the swap space. As a result, Linux delays the time it has to deliver an "out of memory". But this helps only briefly and only leads to the fact that the computer is noticeably slower.
Just because there is a swap space does not mean it's always written on it. How heavily a Linux "swaps" depends on the "swappiness". This is a value ranging from 0 "only if it does not work" to 100 "at every opportunity". However, there is no optimal value for all cases, but you have to decide differently depending on the application, usage and hardware. So you have to consider a few factors.

A typical desktop computer inevitably requires more memory, because sometimes more applications run parallel depending on usage. It may be useful to work on a desktop computer with high swappiness (60 or 70). In normal operation, unused parts of large programs would not occupy the main memory but would be stored in the swap memory. As a result, the physically available main memory would be as large as possible during normal operation. That is, you can increase the number of applications that can run in parallel before running out of memory.

On a server, swap space can be useful, but also useless. On a server, it is recommended to always turn off everything. This saves memory and processor performance. You could even do without swapping altogether here. It looks different, if many dormant processes occupy space in the main memory, then swapping with a high swappiness is even recommended.
Another reason for a rather low swappiness value is a slow hard drive. Because swapping always slows down the overall system, swapping expresses itself on a slow disk as a further degradation of performance. Here you work more with a swappiness between 0 and 30.

Especially with Raspberry Pi, swapping is actually counterproductive. Swapping increases the number of write accesses to a storage medium on which the swap memory is located. The operating system and the swap memory are located on Raspberry Pi on an SD card, which only tolerates a limited number of write accesses, before individual memory cells break. SD cards are not suitable for swapping at all.
Another reason to avoid swapping is the limited speed of the SD card. That makes the swapping on Raspberry Pi even slower.
Even SSDs can cope with only a limited number of write cycles. Swapping to an SSD or SD card reduces their lifetime and can lead to premature data loss.

This should lead to the decision to choose the lowest possible swappiness and a small swap memory for SD cards and SSDs, or to do without swapping at all. Of course you can switch off swapping completely.
If you want to switch off swapping completely, it must be clear to you that without swapping the state "out of memory" will be reached much sooner. Before doing so, make sure that enough physical memory is available for the intended application.

Note: The swap space and paging file are sometimes referred to as virtual memory. However, both forms of storage are not a substitute for memory or memory expansion. Swap space is an additional storage area that acts as a buffer to keep as much physical RAM as possible free for critical applications and data, and to delay the time that memory expires.
task
  • Find out how much swapping is being used on your Raspberry Pi.
  • Determine the value for the swappiness and set it to a meaningful value.
  • Switch off the swapping completely.
  • Set up the swapping again.
Solution: Information about swapping
The following command shows if the service is running for swapping and how big the memory area is.
sudo service dphys-swapfile status

Solution: Empty swap space
To determine whether the swap memory is needed at all, disable the swap memory. This empties him.
sudo swapoff -a

With "free" you can see that the swap memory size is 0 kbytes. If you do not have to be patient because with a larger swap space it can take several minutes to empty.
Then you turn it back on.
sudo swapon -a

With "free" you can check if the swap memory is refilled. If the goal is to avoid swap storage, then you still have to set the swappiness to a very low value.
Solution: Set swap memory size
The available swap space is set to 100 MB for a freshly installed Raspbian. Yes, after application this can be a lot or a little. There is a rule that the swap space should be twice the size of the physical memory. This rule is nonsense. The useful size of the swap memory depends on the application.
Raspberry Pi has no reason to play around with this attitude. If the RAM of Raspberry Pi is insufficient, the swap memory will not help.

If there is still a need for more buffers, you can easily change the swap size in the / etc / dphys-swapfile file.
sudo nano / etc / dphys-swapfile

Simply enter another value instead of the "100". Save, close and restart.

Solution: Disable swapping completely
It is only sensible to deactivate swapping completely if Raspberry Pi is used in such a way that the main memory is available even without swapping.
This is ensured by the fact that the reserved swap space remains unused in normal use. This should be achieved by first setting the swappiness to "1" or even "0". Only when the swap area remains permanently unused, the complete deactivation can be useful.

First we stop the swapping service:
sudo service dphys-swapfile stop

Then we check if the swapping is switched off:
free

If the "Swap" line only has "0" values, we can disable the swap service.
sudo systemctl disable dphys-swapfile

Or remove completely.
sudo apt-get purge dphys-swapfile

A restart is not necessary.

Solution: Set up swapping
Once you've determined that the swapping is a good idea and makes sense, you can set it up again.
sudo apt-get install dphys-swapfile

After installation, a default value is set for the swap file, which must be adapted to your own needs. If necessary, the value for the swappiness.
The service will be activated automatically and will start automatically at the next reboot.

Start and stop services for swapping (also automatically)
Start swapping:
sudo systemctl start dphys-swapfile

Stop ongoing swapping:
sudo systemctl stop dphys-swapfile

Swapping should start automatically in the future:
sudo systemctl enable dphys-swapfile

Swapping should NOT start automatically in the future:
sudo systemctl disable dphys-swapfile
Share it:

Raspberry Pi

Post A Comment:

0 comments: