beta-tester
Posts: 1434
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

do i need SWAP on RPi4 with 4GB?

Tue May 26, 2020 4:58 pm

here my silly question...
do i need swap on a RPi4 with 4GB RAM?
it seems i do, because over the time the swap tells it is using some of the 100 MB, but i never saw, that my 3.8GB RAM ever ran out.
i tried to set the swap to 0MB but after that i noticed some bigger delays (and hangs) in some programs that was not the case when swap is set to 100MB...
but why do i need swap, then i have plenty of free RAM available?
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

bjtheone
Posts: 1304
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: do i need SWAP on RPi4 with 4GB?

Tue May 26, 2020 6:28 pm

It is not ever going to run out of RAM as the memory management will start swapping before that, until you get to the point where all swap is exhausted. At that point it will start more enthusiastic memory management (aka killing processes to free up resources). You can play with the amount of swap and the "swapiness" factor to determine how aggressively it attempts to manage memory usage.

I would say that for most use cases the default of 100 MB for a 4 GB model works well. I use mine as a desktop replacement and it works just fine. Because I am interested (not obsessive) I tend to have HTop running and especially if I have Chromium running for days with lots of active tabs, I will end up using up most of the ram and all of the swap.

LTolledo
Posts: 5378
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: do i need SWAP on RPi4 with 4GB?

Tue May 26, 2020 9:03 pm

the answer will be revealed after several monitoring of your system using htop
if you see the default swap spike even up to 50% you might consider editing swap settings
if not then you don't need.
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

beta-tester
Posts: 1434
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: do i need SWAP on RPi4 with 4GB?

Wed May 27, 2020 10:31 am

bjtheone wrote:
Tue May 26, 2020 6:28 pm
It is not ever going to run out of RAM as the memory management will start swapping before that, until you get to the point where all swap is exhausted. At that point it will start more enthusiastic memory management (aka killing processes to free up resources). You can play with the amount of swap and the "swapiness" factor to determine how aggressively it attempts to manage memory usage.
my RPi4 is running a PXE-server 24hours a day, 7days a week...
the swap settings are the default ones from the Raspbian distribution.
the swap is not used that many times over the week...
even when i use around 10 browser tabs in firefox, it grows not higher than up to 30%...40%.
(i am even not sure if it is firefox that makes that use of the swap, i have also other status windows open)

when i connect a PXE-client the RPi (PXE-server) used jumps up to nearly 100% swap usage, but the RAM is not even close to ran out of memory...
RPi-swap.jpg
RPi-swap.jpg (64.28 KiB) Viewed 4703 times
this is what i don't understand.
conky it tells me, that 800MB RAM of 3800MB total RAM is used...
i don't see a reason, why the swap is at neatly 100%.
it is stressing my SD card for no reason.

that is what i want to prevent.
but when i srink the swap size to 0MB the file browser (PCManFM) as an example is reacting with very low performance (very big delays or not responding)

it seems specially network stuff is using swap a lot...
NFS, SAMBA, HTTP, ...

if i have to use swap, then i don't want it on any flash storage...
can i move the swap completely into RAM?
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

Moonmarch
Posts: 679
Joined: Thu Mar 07, 2019 1:34 am

Re: do i need SWAP on RPi4 with 4GB?

Wed May 27, 2020 9:47 pm

I found this forum topic in the Stack Exchange website that explains swap file in Linux. Here is a link to the forum topic on the Stack Exchange website:

How to empty swap if there is free RAM? - Ask Ubuntu
https://askubuntu.com/questions/1357/ho ... s-free-ram

Here is my theory about RAM on the RPI computer. Raspbian OS is developed for Raspberry Pi computers with 1 GB of RAM or less. 1GB of RAM was the RAM amount standard until the RPI4 was released. The RPI4 can be purchased with 4 GB of RAM. Is Raspbian optimized to use 4 GB of RAM? I'm not a computer software programmer, I wouldn't know. I'm not going to read the entire forum discussion in the Stack Exchange website link that I posted. I admit I'm tired of reading about Linux, can I understand the context, of course. Am I going to use the all of this knowledge that I found, that is about Linux, I would say no, not really. I only search for answers to problems that I consider "hazardous." Anything I consider to be reckless behavior.

Here is what I have to say about the RPI computer, Raspbian runs on the RPI3 without problems. This is the reason why I have not upgraded to the RPI4. How do I know if performance improves on the RPI4 with new updates being released. I watch YouTube videos, and read online news articles about the RPI4. While upgrading to the RPI4 does not seem very significant compared to performance on the RPI3. For people starting out with the RPI computer, new customers should purchase the RPI4.

gkreidl
Posts: 6345
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 4:37 am

**deleted text **

1) Raspbian has not been "developed" for the 1GB RAM Raspberry Pi. It is plain Debian rebuilt.
2) The Rpi 4 is definitely much faster than a RPi 3. And some programs, which need lots of RAM, have really started to work (browsers especially).

But back to topic: There is definitely a bug in Buster's swapping software. Swap is even being used, when swappiness has been set to "0" and lots of RAM is available. I have reported this a few times already, but nobody seems to care.

[moderator: watch your language, and threat others like you want to be treated yourself, don't force me to delete what you wrote!]
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

beta-tester
Posts: 1434
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 5:57 am

gkreidl wrote:
Thu May 28, 2020 4:37 am
But back to topic: There is definitely a bug in Buster's swapping software. Swap is even being used, when swappiness has been set to "0" and lots of RAM is available. I have reported this a few times already, but nobody seems to care.
i am not sure, but i did not used the swappiness parameter to "turn off" swap.
(i didn't even know, that there is such an option)

to bring swap to zero, i modified the file

Code: Select all

/etc/dphys-swapfile
and setted its parameter

Code: Select all

CONF_SWAPSIZE=0
and to be sure i added the boot parameter

Code: Select all

noswap=1
at the end of

Code: Select all

/boot/cmdline.txt
after that, the size of swap is shown as 0 in conky, so i assume it is really zero.

i will now try to use all three options
CONF_SWAPSIZE=0 to /etc/dphys-swapfile,
noswap=1 to /boot/cmdline.txt
vm.swappiness = 0 to /etc/sysctl.d/9999-swappiness.conf
and will see if i get in trouble (performance issues) again in some situations...

@Moonmarch: in the past i used a RPi3B, then a RPI3B+ because of its bit faster ethernet connection (~300Mb/s) and now a RPi4B.
the RPi4B has, specially for my pex-server purpose, two big advantages...
a real 1Gb/s network adapter and 2x real USB3.0.
(makes the pxe-client boots and responds way faster than before)
there is not that bottleneck of the previous RPI's of sharing one single USB2.0 line with slow network adapter and 4x USB2.0 ports.

and as bonus of the RPI4B, it has more RAM and horsepower...
i am now able to use the RPi4 also as web browser instead of turning on my big noisy PC only for a quick web search/checking news.
i also run other tiny stuff on the RPi4 now for what i had to turn on my big PC in the past.
the RPi4 is online all the time anyhow for pxe-server purpose...
so that RPi4B IS a big bonus for me.
now, my big noisy PC i only turn on, when i really need the big extra power of it.
Last edited by beta-tester on Thu May 28, 2020 7:59 am, edited 1 time in total.
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

gkreidl
Posts: 6345
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 7:42 am

I do want to keep swap space, but want to avoid swapping as long as possible. In the past (Stretch and older) setting swappiness to "1" did exactly that: avoid swapping except when it is really needed.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

beta-tester
Posts: 1434
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 7:55 am

gkreidl wrote:
Thu May 28, 2020 7:42 am
setting swappiness to "1" did exactly that: avoid swapping except when it is really needed.
yes, maybe the more smarter way... i will try it, and see if the swap is still going up to about 100% or how long it takes to do that
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

tpylkko
Posts: 416
Joined: Tue Oct 14, 2014 5:21 pm

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 9:15 am

One alternative solution is to use zram or zswap, which will compress the swap to RAM. In case you want to avoid ware to memory card, or the card is slow. OF course, the compression is going utilize cpu time, but this might be a good trade-off for some use cases.

gkreidl wrote:
Thu May 28, 2020 4:37 am


But back to topic: There is definitely a bug in Buster's swapping software. Swap is even being used, when swappiness has been set to "0" and lots of RAM is available. I have reported this a few times already, but nobody seems to care.
Is that how it works, though? Are you sure "0" is intended to "turn of swapping?" I looked at this man page and it only says that the lower the number is, the less "aggressive" it will be. It never says that it can be disable entirely with this. That would sound logical, so I am not saying that you are wrong, but I have never been able to see any definitive documentation saying that it works like that. Do you know something more about it than me?

http://man7.org/linux/man-pages/man5/proc.5.html

shouldn't there be another flag or a kernel boot parameter or something to disable the entire process of swapping?

muwlgr
Posts: 4
Joined: Sun Feb 24, 2013 9:30 am

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 12:03 pm

From my experience, if you look at 'free -h' printout after some long time of system operation, you need to have at least as much 'free' in 'Swap' as you have 'used' in 'Mem'.
And if your swap is full, that is, if you have 0 'free' in 'Swap', you should definitely increase your swap space. Initially, by the amount shown as 'used' in 'Mem'.
To save your flash storage from weardown, it is surely useful to set vm.swappiness = 1 in your sysctls. But this would not help you much if your memory pressure is high enough. See 'si/so' and 'bi/bo' columns in 'vmstat' printout to get overall statistics on how loaded is your storage with swap activity and other block i/o

ConfuSomu
Posts: 72
Joined: Tue Dec 04, 2018 8:47 am
Location: Canada

Re: do i need SWAP on RPi4 with 4GB?

Thu May 28, 2020 7:31 pm

beta-tester wrote:
Wed May 27, 2020 10:31 am
if i have to use swap, then i don't want it on any flash storage...
can i move the swap completely into RAM?
tpylkko wrote: One alternative solution is to use zram or zswap, which will compress the swap to RAM. In case you want to avoid ware to memory card, or the card is slow. OF course, the compression is going utilize cpu time, but this might be a good trade-off for some use cases.
A script that I would recommend for zram is https://github.com/novaspirit/rpi_zram/ ... er/zram.sh

You can then also disable dphys-swapfile with something like:

Code: Select all

dphys-swapfile swapoff # Disable
dphys-swapfile uninstall
apt-get -y remove dphys-swapfile # Remove package

To reduce furthermore disk writing, I add this to the fstab (/etc/fstab):

Code: Select all

tmpfs /tmp tmpfs defaults,noatime,nodiratime,size=32M 0 0
tmpfs /var/log tmpfs defaults,noatime,nodiratime,size=32M 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nodiratime,size=128M 0 0
tmpfs /home/pi/.cache tmpfs defaults,noatime,nodiratime,size=380M 0 0
Change the sizes as you all wish. You can change the tmpfs partitions sizes when booted, with mount -o remount,size=64M /tmp/, if you want a 64 MB /tmp/ partition until the next boot, for example.
If you are low on RAM, make sure to use zram! ;)
I found & use this zram setup script: https://github.com/novaspirit/rpi_zram
A post about zram: t=207304
Make sure to disable dphys-swapfile and mount the tmp folders in RAM. Also learn about SysRq!

beta-tester
Posts: 1434
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: do i need SWAP on RPi4 with 4GB?

Tue Jul 28, 2020 6:05 am

i found the cause of why the SWAP runs full even the (user-)RAM usage is so low...
the cause are the buffer/caches.

top does not show the processes who is using those buffers/caches?
i could not see any changes before and after i cleaned the buffers/caches in the process list

restarting the swap did not fix the issue, because it will not clean/flush all the buffers and caches.
also a "sync" does not clean those buffers and caches.

so the swap immediately runs full, as long those buffers and caches were not cleaned.

but when i clean the buffers/caches/... swap keeps empty for a long time until the buffers/caches were eating all the free RAM.

Code: Select all

t0: just after swap starts to fill up
t1: just after sync and sudo systemctl restart dphys-swapfile.service
t2: just after echo 3 | sudo tee /proc/sys/vm/drop_caches &> /dev/null
    procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu-------- -----timestamp-----
     r  b         swpd         free        inact       active   si   so    bi    bo   in   cs  us  sy  id  wa  st                CEST
t0:  0  0          768       648540      2484244       649800    0    0     6     9   10   14   8   2  90   0   0 2020-07-28 07:26:36
t1:  0  0            0       647180      2484992       650476    0    0     6     9   10   14   8   2  90   0   0 2020-07-28 07:26:37
t2:  0  0            0      3392056       310888       139248    0    0     6     9   10   14   8   2  90   0   0 2020-07-28 07:26:40
here the caches that gave the biggest amount of ram free

Code: Select all

    Cache                       Num  Total   Size  Pages
t1: buffer_head              595955 597184     64     64
t2: buffer_head                2753   4672     64     64

t1: radix_tree_node           27328  27456    304     26
t2: radix_tree_node            3036   7410    304     26

t1: ext4_inode_cache          19436  23628    744     22
t2: ext4_inode_cache           1621   3872    744     22

t1: ext4_extent_status        11277  13184     32    128
t2: ext4_extent_status         7041   7552     32    128

t1: proc_inode_cache           2810   3024    440     18
t2: proc_inode_cache           1720   2304    440     18

t1: dentry                    30235  39030    136     30
t2: dentry                    15577  26880    136     30

t1: kmalloc-rcl-64             7984  14208     64     64
t2: kmalloc-rcl-64             3769   4480     64     64
is there a knob, that i can tune to automatically clean/flush those buffers and caches (for example every 60s or so)?
i know it may decrease the performance of the RPi but i prefere to have a bit slower io performance than weare out the memory cells of my SD card.
Last edited by beta-tester on Tue Jul 28, 2020 1:15 pm, edited 1 time in total.
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

bjtheone
Posts: 1304
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: do i need SWAP on RPi4 with 4GB?

Tue Jul 28, 2020 11:18 am

muwlgr wrote:
Thu May 28, 2020 12:03 pm
From my experience, if you look at 'free -h' printout after some long time of system operation, you need to have at least as much 'free' in 'Swap' as you have 'used' in 'Mem'.
And if your swap is full, that is, if you have 0 'free' in 'Swap', you should definitely increase your swap space. Initially, by the amount shown as 'used' in 'Mem'.
To save your flash storage from weardown, it is surely useful to set vm.swappiness = 1 in your sysctls. But this would not help you much if your memory pressure is high enough. See 'si/so' and 'bi/bo' columns in 'vmstat' printout to get overall statistics on how loaded is your storage with swap activity and other block i/o
It somewhat depends on what sort of load you are running (many small processes, or a few very large) but as RAM sizes have increase this suggested rule does not seem to hold true. For example, I have a 4GB model, I can fill the RAM and run with 1 GB of swap, which I have not seen maxed out. I have an 8GB that I have not managed to max out yet (most RAM used I have noted was just over 6 GB). I run that one with the default 100 MB swap. Now with smaller memory footprints I would agree with you. I have a 3B+ that I run with an SSD and 1 GB of swap.

Return to “General discussion”