dashnick
Posts: 7
Joined: Sat Dec 02, 2017 7:10 pm

Default swap file size

Tue Jan 01, 2019 9:00 pm

I noticed my RPi 3 kept running out of memory, especially when trying to run Visual Studio Code.
I thought I should try to expand virtual memory, and noticed when following directions here

https://wpitchoune.net/tricks/raspberry ... _size.html

and editing `/etc/dphys-swapfile` that the default value is only set to 100 MB. I removed the 100 and left it blank as instructed in the file, and then it automatically set it to 2 GB (twice the RAM).

My question is should this default value be changed or removed? Seems like the config file hasn't been updated since 2010, and this 100 is a very old (and small) value.

W. H. Heydt
Posts: 12961
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Default swap file size

Tue Jan 01, 2019 9:47 pm

Using an SD card for swap space is--generally speaking--a bad idea. It has a limited number of writes per cell before any given cell fails. If you spend a lot of time writing to it, pretty soon you have a dead SD card. Better to find ways to run that don't require swap space at all.

Heater
Posts: 16304
Joined: Tue Jul 17, 2012 3:02 pm

Re: Default swap file size

Tue Jan 01, 2019 9:49 pm

If you find you need swap all the time you will also find that things are so slow as to not want to use them.

I'm surprised VS Code is taking so much space. On my PC it's usually only 4 or 5 hundred MB.
Memory in C++ is a leaky abstraction .

DirkS
Posts: 10389
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Default swap file size

Tue Jan 01, 2019 9:59 pm

Heater wrote:
Tue Jan 01, 2019 9:49 pm
I'm surprised VS Code is taking so much space. On my PC it's usually only 4 or 5 hundred MB.
Agreed. I use VSCode on a Pi3 with swap switched off and have no memory problems.
OP: could other software cause the memory problem (especially thinking of web browsers)

dashnick
Posts: 7
Joined: Sat Dec 02, 2017 7:10 pm

Re: Default swap file size

Tue Jan 01, 2019 10:04 pm

W. H. Heydt wrote:
Tue Jan 01, 2019 9:47 pm
Using an SD card for swap space is--generally speaking--a bad idea. It has a limited number of writes per cell before any given cell fails. If you spend a lot of time writing to it, pretty soon you have a dead SD card. Better to find ways to run that don't require swap space at all.
If this is the case, shouldn't the swap size be set to 0?

I mean it generally is okay unless I have a browser open as well. The thing was just crashing before, now at least it runs.

klricks
Posts: 7204
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Default swap file size

Tue Jan 01, 2019 10:10 pm

For the general user the use of swap should be avoided at all costs. It will make the RPi very slow and appear to be unresponsive.
Instead try adjusting the memory split. If not using video or camera then setting to value of 16 will give maximum memory to the CPU.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

dashnick
Posts: 7
Joined: Sat Dec 02, 2017 7:10 pm

Re: Default swap file size

Tue Jan 01, 2019 10:24 pm

klricks wrote:
Tue Jan 01, 2019 10:10 pm
For the general user the use of swap should be avoided at all costs. It will make the RPi very slow and appear to be unresponsive.
Instead try adjusting the memory split. If not using video or camera then setting to value of 16 will give maximum memory to the CPU.
I agree the swap file should be a last resort in this case, but it is better than the system just running out of memory and crashing. Maybe there should be a dialog like "your system has started to use the SD card for memory which is bad blah blah blah, try to close some applications"...?

User avatar
thagrol
Posts: 3274
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Default swap file size

Tue Jan 01, 2019 11:50 pm

Heater wrote:
Tue Jan 01, 2019 9:49 pm
I'm surprised VS Code is taking so much space. On my PC it's usually only 4 or 5 hundred MB.
Gods. That makes me feel old. I remember when 4MB RAM was considered a lot for a PC (and earlier but then I'll be feeling ancient)
Arguing with strangers on the internet since 1993.

W. H. Heydt
Posts: 12961
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Default swap file size

Tue Jan 01, 2019 11:57 pm

thagrol wrote:
Tue Jan 01, 2019 11:50 pm
Heater wrote:
Tue Jan 01, 2019 9:49 pm
I'm surprised VS Code is taking so much space. On my PC it's usually only 4 or 5 hundred MB.
Gods. That makes me feel old. I remember when 4MB RAM was considered a lot for a PC (and earlier but then I'll be feeling ancient)
I remember when 4MB RAM was more than you got on many mainframes. When I was working on S/360-30 systems, the hardware limit was 64K, and the first one I was on had 32K. When a new release expanded the system nucleus from 6K to 8K we spent weeks recompiling programs to eliminate double-buffering files so they would still run in the available memory. (And--for the benefit of the OP--no such animal as virtual memory or swap space.)

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

Re: Default swap file size

Wed Jan 02, 2019 4:49 am

On my USB thumbdrive and/or USB HDD/SSD boot RPi3B and RPi3B+ Raspbian Stretch with Desktop, I've set the swap to 1000.

on my microSD Raspbian stretch with desktop (RPiZW, RPiA+, RPi1B+, RPi2B, RPi3B, RPi3B+), it's set to default, not changed at all (nor planning to do so).

I use my RPi3B and RPi3B+ for web browsing (and accessing this forum), opening at least 5 tabs. thus I need big swap size.... or it'll crawl very^24 slowly.....
"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!"

fanoush
Posts: 574
Joined: Mon Feb 27, 2012 2:37 pm

Re: Default swap file size

Thu Jan 03, 2019 7:42 pm

dashnick wrote:
Tue Jan 01, 2019 9:00 pm
My question is should this default value be changed or removed? Seems like the config file hasn't been updated since 2010, and this 100 is a very old (and small) value.
To me it also seems to be odd choice. It is too small so it is more dangerous than useful. Once both memory and swap is full it goes to swapping hell. I even managed once to completely destroy one card because after like 10minutes of constant writing I pulled the plug and card became permanently read only. And it was just one more tab in Chromium.

If the swap is larger and not full you have more chance to notice and even with 500mb swap used the system still behaves better than with 100mb swap completely full. Or at least that was my experience.

dashnick
Posts: 7
Joined: Sat Dec 02, 2017 7:10 pm

Re: Default swap file size

Thu Jan 03, 2019 7:48 pm

fanoush wrote: If the swap is larger and not full you have more chance to notice and even with 500mb swap used the system still behaves better than with 100mb swap completely full. Or at least that was my experience.
Yeah running out of memory is clearly a worse option than using swap; again I think there should just be a warning when using swap memory, but pushing the max swap size to 2x RAM as the default (which would mean removing the value).

Any idea how I could make this feature request to Debian?

chwe
Posts: 159
Joined: Tue Jul 31, 2018 1:35 pm

Re: Default swap file size

Thu Jan 03, 2019 8:50 pm

did you guys ever played with zram? https://wiki.ubuntuusers.de/zRam/
actually RPis 4.14 kernel should allow it (https://github.com/raspberrypi/linux/bl ... onfig#L402). Never used it in desktop scenarios but for headless, it does a decent job especially when compiling hunge packages on an SBC.

disable swap and map 50% of ram as Zram... Some reports from the desktop guys would be interesting.

dashnick
Posts: 7
Joined: Sat Dec 02, 2017 7:10 pm

Re: Default swap file size

Thu Jan 03, 2019 8:53 pm

chwe wrote:
Thu Jan 03, 2019 8:50 pm
did you guys ever played with zram? https://wiki.ubuntuusers.de/zRam/
actually RPis 4.14 kernel should allow it (https://github.com/raspberrypi/linux/bl ... onfig#L402). Never used it in desktop scenarios but for headless, it does a decent job especially when compiling hunge packages on an SBC.

disable swap and map 50% of ram as Zram... Some reports from the desktop guys would be interesting.
Wow that looks cool, I'll give it a try.

User avatar
thagrol
Posts: 3274
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Default swap file size

Thu Jan 03, 2019 10:14 pm

chwe wrote:
Thu Jan 03, 2019 8:50 pm
disable swap and map 50% of ram as Zram... Some reports from the desktop guys would be interesting.
Won't that mean that your running apps have only 50% of RAM available to them so will be swapping more often with not only the normal swap overhead but the compression/decompression overhead of zram too?

Sure, it might help out with SD card lifetime but I can't see a performance benefit.

Or am I missing something? (Yes I am aware that RAM access is faster than disc access).
Arguing with strangers on the internet since 1993.

chwe
Posts: 159
Joined: Tue Jul 31, 2018 1:35 pm

Re: Default swap file size

Thu Jan 03, 2019 11:54 pm

yeah, you only have 50% of ram after its.. But the goal is then to let swap happening.. you've a bit of overhead but who cares? It's still a way faster than accessing some small junk files on swap. you can then start to play around with swappiness.. don't set it to low, we want that swapping happens (we don't have that much ram anymore so we need to put stuff on swap). Thanks to compression yeah, you only have 50% of ram after its.. But the goal is then to let swap happening.. you've a bit of overhead but who cares? It's still a way faster than accessing some small junk files on swap. you can then start to play around with swappiness.. don't set it to low, we want that swapping happens (we don't have that much ram anymore so we need to put stuff on swap).
Cause I use my boards headless I can't say how it performs with browser usage.. but give it a try and look how it performs.

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

Re: Default swap file size

Fri Jan 04, 2019 6:45 am

There are two additional solutions to this problem. You need memory but you don't want to wear out your memory card with swap because they cost money. Solution candidate number 1: use swap but place the swapfile on a USB hard drive to avoid SD wear. Solution candidate number 2: use an extra cheapo memory card and just replace it when it wears out.

The first one is probably better performancewise and less wasteful.

chwe
Posts: 159
Joined: Tue Jul 31, 2018 1:35 pm

Re: Default swap file size

Fri Jan 04, 2019 3:35 pm

IMO those are two 'solutions' to have even more trouble.. :lol:
swap is supposed to be fast! It's just a protection mechanism to avoid oom situations.. therefore swap has to be on the fastest memory (besides ram) available.. keep in mind we talk here about random access so random reads and writes are important not seq. speed. spinning rust is the worst when it comes to random access (a way worser than a A1 SD-Card) and the cheap 'memory cards' (usb stick or SD doesn't matter) are also known to be shitty when it comes to random access of small chunk files and that's normally the stuff you'll find in swap.

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

Re: Default swap file size

Wed Jan 09, 2019 11:58 pm

What you say about 4k random read / writes is true. However, most if not all the other solutions offered above incur performance hits or othet compromises also. Besides, when you've already made the decision to use swap, you have already decided to sacrfice some performance. Who knows, maybe some people have use cases where they need the extra memory but are not interested in performance? Moreover, other factors, such as price, should be considered. An old HDD with a USB adapter can be free, whereas a wearing out a high performance SD-card not so much.

User avatar
thagrol
Posts: 3274
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Default swap file size

Thu Jan 10, 2019 12:59 am

tpylkko wrote:
Wed Jan 09, 2019 11:58 pm
An old HDD with a USB adapter can be free, whereas a wearing out a high performance SD-card not so much.
Yep. And I still don't get the whole swap file in RAM thing. Sure it'll help reduce SD card wear but I really can't see any other benefit. Surely it's better to let stuff run in all the available RAM as there's less overhead and less swapping?

I'd hope that modern systems throw away pages that contain unmodified code and re-read from the file when needed rather than write to swap then read it back. Of course since I forget where I first heard that concept, and when, it may very well not be the case.

Using RAM for swap reminds me of this old joke from when Virtual Memory (aka swap) was being introduced in dekstop PCs: "Great! Virtual memoery. Let's create a really big RAM disc."
Arguing with strangers on the internet since 1993.

User avatar
bertlea
Posts: 309
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Default swap file size

Thu Jan 10, 2019 1:35 am

I think the main idea of zram is “data compression in RAM”. It allows more memory available by compressing data to RAM that are not immediately needed at the cost of some overhead or the compression / decompression and also need to allocate some RAM space (say 50%) to store compressed data.
I remember seeing an video in You-tube comparing the observed RAM usage in IOS and Android and found Apple actually able to compress the application’s memory when it moved to background (not running) with good efficiency.

Return to “General discussion”