n67
Posts: 105
Joined: Mon Oct 30, 2017 4:55 pm

swap in Raspbian: More trouble than it is worth?

Tue Nov 21, 2017 12:51 pm

The default setup of Raspbian includes 100 megs of swap, in file: /var/swap. However, the fact is that if you ever end up using this swap, you're already dead. A reboot is imminent. This is because the file is on the SD card and SD is slow (very slow).

So, I wonder why this is in there. Isn't it a relic of some earlier installation (perhaps the so-called "upstream") ?

Notes:
1) I have put "swapoff -av" into /etc/rc.local - this seems to make the system a little peppier. The idea is that you'd rather it manage the RAM memory better (which it seems to do if it has no swap to fall back on) and to basically crash rather than struggle if/when it really does run out of memory. This is preferable - a quick death rather than a slow, painful one.

2) Yes, of course, if you are booting from a USB hard disk - or have one available for use - then you can put your swap file there and things will be better. But that's not relevant to my case or to most installations.

User avatar
RaTTuS
Posts: 9585
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: swap in Raspbian: More trouble than it is worth?

Tue Nov 21, 2017 12:59 pm

swappiness is very low so it wont use it unless it really needs to ,
no swap just means the system will OOM sooner
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

n67
Posts: 105
Joined: Mon Oct 30, 2017 4:55 pm

Re: swap in Raspbian: More trouble than it is worth?

Tue Nov 21, 2017 1:38 pm

RaTTuS wrote:
Tue Nov 21, 2017 12:59 pm
swappiness is very low so it wont use it unless it really needs to ,
no swap just means the system will OOM sooner
Exactly. That's what you want to happen.

So, the question remains: Why do they leave it in the image?

jahboater
Posts: 1924
Joined: Wed Feb 04, 2015 6:38 pm

Re: swap in Raspbian: More trouble than it is worth?

Tue Nov 21, 2017 2:12 pm

n67 wrote:
Tue Nov 21, 2017 12:51 pm
1) I have put "swapoff -av" into /etc/rc.local - this seems to make the system a little peppier. The idea is that you'd rather it manage the RAM memory better (which it seems to do if it has no swap to fall back on) and to basically crash rather than struggle if/when it really does run out of memory. This is preferable - a quick death rather than a slow, painful one.
I might do the same.

However there are occasions when a program briefly over-commits memory and some swap is enough to keep it going for those few seconds. Its also somewhere to put stuff thats loaded but rarely used, otherwise it would compete with more important data for memory.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4731
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: swap in Raspbian: More trouble than it is worth?

Tue Nov 21, 2017 2:45 pm

RaTTuS wrote: swappiness is very low so it wont use it unless it really needs to ,
That's no longer the case. Dom has found that performance is better at the default swappiness.

Swap is not just an attempt to bail the system out if there's a process taking up too much RAM. Sometimes, keeping the infrequently accessed stuff in swap is better than keeping it in RAM or discarding it altogether.

https://askubuntu.com/questions/184217/ ... 221#184221

n67
Posts: 105
Joined: Mon Oct 30, 2017 4:55 pm

Re: swap in Raspbian: More trouble than it is worth?

Wed Nov 22, 2017 12:33 am

Incidentally, if anyone wants to try this at home (running w/o swap), it turns out that you can't just put that one line in rc.local, because under systemd rc.local runs very early in the process. So, you have to wait for the swap to be activated before you can deactivate it.

Here's what my rc.local looks like now:

Code: Select all

exec > /tmp/rc-local.out 2>&1
echo "PATH = $PATH"
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

until grep -q /var/swap /proc/swaps
do
    sleep 2
done
swapoff -av
exit 0
I added the two lines at the top (the exec and the echo). I highly recommend this to anyone who is dealing with rc.local - since w/o it, debugging is very difficult. I think that whenever we see any sort of "I put it in rc.local but it doesn't work" thread on this forum, we need to make this recommendation. It will help people a lot.

Anyway, I also added the until loop - that waits until swap is activated. Then, once that works, we can deactivate it.

Note incidentally, that the original "My IP address is" stuff doesn't work anymore if your Internet connection is wireless (it might work if you have ethernet; I'm not sure....), since, again, the wireless connection isn't up at the point where that code is executed. If you did want that part to work right, you'd have to include a similar "until" loop, to wait for the wlan0 connection to be up - then the "hostname -I" command would get the right thing.

Finally, note that all testing is with Jessie - if it is different under the later version, feel free to post that.

User avatar
RaTTuS
Posts: 9585
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: swap in Raspbian: More trouble than it is worth?

Wed Nov 22, 2017 9:18 am

disable the systemd service don't badger around with custom scripts ...

Code: Select all

sudo systemctl stop dphys-swapfile
sudo systemctl disable dphys-swapfile
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

hommar
Posts: 92
Joined: Sat Mar 25, 2017 1:55 pm
Location: Russia, Yekaterinburg

Re: swap in Raspbian: More trouble than it is worth?

Wed Nov 22, 2017 3:10 pm

or remove

Code: Select all

sudo apt-get remove dphys-swapfile
PS IMHO ZRAM is better idea for small swap

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4731
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: swap in Raspbian: More trouble than it is worth?

Wed Nov 22, 2017 3:38 pm

ZRAM showed improvements in some areas and degradation in others. We didn't find a compelling reason to use zram or zswap.

jahboater
Posts: 1924
Joined: Wed Feb 04, 2015 6:38 pm

Re: swap in Raspbian: More trouble than it is worth?

Wed Nov 22, 2017 4:05 pm

:) Or, since its an educational platform, encourage people to learn how not to write bloated code! :)

User avatar
DougieLawson
Posts: 30420
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: swap in Raspbian: More trouble than it is worth?

Thu Nov 23, 2017 8:16 am

Or use a USB hard drive as a swap device. My RPi3Bs with WD drives fly and can run a kernel build with make -j6 in about 1hr 40.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

n67
Posts: 105
Joined: Mon Oct 30, 2017 4:55 pm

Re: swap in Raspbian: More trouble than it is worth?

Thu Nov 23, 2017 11:52 am

DougieLawson wrote:
Thu Nov 23, 2017 8:16 am
Or use a USB hard drive as a swap device. My RPi3Bs with WD drives fly and can run a kernel build with make -j6 in about 1hr 40.
From the OP:
2) Yes, of course, if you are booting from a USB hard disk - or have one available for use - then you can put your swap file there and things will be better. But that's not relevant to my case or to most installations.

hommar
Posts: 92
Joined: Sat Mar 25, 2017 1:55 pm
Location: Russia, Yekaterinburg

Re: swap in Raspbian: More trouble than it is worth?

Thu Nov 23, 2017 4:21 pm

DougieLawson wrote:
Thu Nov 23, 2017 8:16 am
Or use a USB hard drive as a swap device. My RPi3Bs with WD drives fly and can run a kernel build with make -j6 in about 1hr 40.
I test

Code: Select all

date  >> /tmp/time.txt
make -j4 zImage modules dtbs
date  >> /tmp/time.txt
1h15m

Code: Select all

23 20:01:12 +05 2017
23 21:16:31 +05 2017
I use tmpfs+zram+squashfs+overlayfs in slow USB storage
(I use this USB storage as CD-Live in PC for recovery)

Return to “Raspbian”

Who is online

Users browsing this forum: The_Heman and 25 guests