leonhess
Posts: 2
Joined: Sat Jun 29, 2019 11:21 pm

Permanently disable swap on Raspbian Buster

Sat Jun 29, 2019 11:30 pm

I want to permanently disable the swap on the newest Raspbian Buster.

Using

Code: Select all

sudo swapoff -a
i can disable the swap. But once I reboot it is enabled again.

Google says i should comment out the swap partition in

Code: Select all

/etc/fstab
but my fstab doesnt have any swapfile line:

Code: Select all

proc            /proc           proc    defaults          0       0
PARTUUID=e02d4f3c-01  /boot           vfat    defaults          0       2
PARTUUID=e02d4f3c-02  /               ext4    defaults,noatime  0       1
## a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
This is the same on a freshly installed version of Buster and a dist-upgraded version.
Is this broken in the new Raspbian version or is there another way to permanently disable?

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

Re: Permanently disable swap on Raspbian Buster

Sun Jun 30, 2019 1:28 am

on previous Raspbian, the better way to disable swap is to edit:

Code: Select all

/etc/dphys-swapfile 
not tried yet with buster though.... maybe a little later....
"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!"

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

Re: Permanently disable swap on Raspbian Buster

Sun Jun 30, 2019 11:30 am

leonhess wrote:
Sat Jun 29, 2019 11:30 pm
Google says i should comment out the swap partition in

Code: Select all

/etc/fstab
but my fstab doesnt have any swapfile line:

Code: Select all

proc            /proc           proc    defaults          0       0
PARTUUID=e02d4f3c-01  /boot           vfat    defaults          0       2
PARTUUID=e02d4f3c-02  /               ext4    defaults,noatime  0       1
## a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
This is the same on a freshly installed version of Buster and a dist-upgraded version.
Is this broken in the new Raspbian version or is there another way to permanently disable?
That's correct. There are no swap partitions as raspbian uses a swap file instead.

Try runing

Code: Select all

sudo dphys-swapfile swapoff
as suggest by the comments in fstab.

If that reverts to on after a reboot try adding the command as an @reboot cron job or to /etc/rc.local
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

leonhess
Posts: 2
Joined: Sat Jun 29, 2019 11:21 pm

Re: Permanently disable swap on Raspbian Buster

Mon Jul 01, 2019 10:14 am

So i tried

Code: Select all

sudo dphys-swapfile swapoff && \
  sudo dphys-swapfile uninstall && \
  sudo update-rc.d dphys-swapfile remove
But that was undone after

Code: Select all

apt update
apt upgrade
one way i found worked is to set

Code: Select all

CONF_SWAPSIZE=0
in

Code: Select all

/etc/dphys-swapfile

powerpete
Posts: 4
Joined: Thu Jun 14, 2018 9:36 am

Re: Permanently disable swap on Raspbian Buster

Tue Jul 02, 2019 12:43 pm

This solved it for me:

Code: Select all

sudo systemctl disable dphys-swapfile.service

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Permanently disable swap on Raspbian Buster

Fri Aug 02, 2019 7:51 am

powerpete wrote:
Tue Jul 02, 2019 12:43 pm
This solved it for me:

Code: Select all

sudo systemctl disable dphys-swapfile.service
free -m confirms a 0 swap file size after a reboot, from 93MB before!
Some of that returns to the RAM though...

Thanks!

Dan1jel
Posts: 18
Joined: Wed Aug 08, 2018 6:21 pm
Location: Sweden
Contact: Twitter

Re: Permanently disable swap on Raspbian Buster

Fri Aug 16, 2019 5:04 pm

ProDigit wrote:
Fri Aug 02, 2019 7:51 am
powerpete wrote:
Tue Jul 02, 2019 12:43 pm
This solved it for me:

Code: Select all

sudo systemctl disable dphys-swapfile.service
free -m confirms a 0 swap file size after a reboot, from 93MB before!
Some of that returns to the RAM though...

Thanks!
I have a hard time to reboot alot (almost every 2-3 days) because my swap is or are at 100mb. Don't raspberry NEEDS a swap file? Or could I disable this with no problems ahead? I'm using my raspberry as a headless medical center with transmission and Plex installed.

thatchunkylad198966
Posts: 124
Joined: Thu Jul 04, 2019 10:21 am
Location: UK, Birmingham

Re: Permanently disable swap on Raspbian Buster

Fri Aug 16, 2019 6:36 pm

After I run the following commands and I've rebooted, no more swap.

Code: Select all

sudo dphys-swapfile swapoff && \
sudo dphys-swapfile uninstall && \
sudo systemctl disable dphys-swapfile
pi@raspberrypi:~ $ free -h
total used free shared buff/cache available
Mem: 3.8Gi 615Mi 1.8Gi 96Mi 1.4Gi 2.9Gi
Swap: 0B 0B 0B
pi@raspberrypi:~ $
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux
pi@raspberrypi:~ $
One man's trash is another man's treasure! :) Pi's I have; Pi Zero, Pi Zero W, Pi 2 x2, Pi 3 x2, Pi 4 4GB x2.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 9:32 am

Disabling swap file, will speed up system response time.
If you're running headless, it's recommended.
If you're running a GUI, it's recommended to do this only if you're having 512MB or more of RAM installed; as 256MB of RAM would be nearly 75% used by the GUI and system; leaving very little for programs.

On the Pi Zero W, with 512MB of RAM, and no Swap, I'm not able to browse on websites anymore, because it's lacking RAM.
But I can do anything else.
In fact, just Raspbian full desktop with some programs installed, idling has about 223MB free.
If you're not running any graphical applications from terminal, and you're running 256 or less MB of RAM, you take some of the VRAM.
By default 32<72MB is used for shared VRAM.
Since I'm not watching movies, I've set mine to 24MB (with GUI). Without GUI, you can go as low as 16MB, saving you between 16-56MB of RAM.

You can check your available RAM by typing:

Code: Select all

free -h
You can change your VRAM by typing:

Code: Select all

sudo raspi-config
clicking 'advanced options' (7),
Memory Split (A3),
And changing the value between 16-128, 16 being little video memory, for console, 32 being the minimum for Raspbian to boot normally, and 128 being most Video memory allocated for graphical tasks and 3D gaming.
8MB is possible, but the system would just crash on boot.
Last edited by ProDigit on Sat Aug 17, 2019 11:06 am, edited 1 time in total.

User avatar
PeterO
Posts: 5086
Joined: Sun Jul 22, 2012 4:14 pm

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 9:39 am

ProDigit wrote:
Sat Aug 17, 2019 9:32 am
Disabling swap file, will speed up system response time.
Are you sure about that ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

thatchunkylad198966
Posts: 124
Joined: Thu Jul 04, 2019 10:21 am
Location: UK, Birmingham

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 10:56 am

PeterO wrote:
Sat Aug 17, 2019 9:39 am
ProDigit wrote:
Sat Aug 17, 2019 9:32 am
Disabling swap file, will speed up system response time.
Are you sure about that ?
PeterO
Yeah because if you're using "swap" on a SDCARD, it can cause slowness and corruption. it's all over Google.
One man's trash is another man's treasure! :) Pi's I have; Pi Zero, Pi Zero W, Pi 2 x2, Pi 3 x2, Pi 4 4GB x2.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 11:10 am

PeterO wrote:
Sat Aug 17, 2019 9:39 am
ProDigit wrote:
Sat Aug 17, 2019 9:32 am
Disabling swap file, will speed up system response time.
Are you sure about that ?
PeterO
Why else would you disable the swap file?
Speed and card wear are the 2 main reasons.
Everything now is from RAM instead of the slower SD Card; not to mention, file access on an SD card is very slow, when the system is simultaneously accessing the swap file.

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

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 11:17 am

If your system needs to use the swap file disabling it will only slow things down and make your system less responsive.

If your system isn't using the swap file disabling it will make no difference.

Having a swap file present does not mean it is being used.
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

User avatar
PeterO
Posts: 5086
Joined: Sun Jul 22, 2012 4:14 pm

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 11:52 am

ProDigit wrote:
Sat Aug 17, 2019 11:10 am
PeterO wrote:
Sat Aug 17, 2019 9:39 am
ProDigit wrote:
Sat Aug 17, 2019 9:32 am
Disabling swap file, will speed up system response time.
Are you sure about that ?
PeterO
Why else would you disable the swap file?
Speed and card wear are the 2 main reasons.
Everything now is from RAM instead of the slower SD Card; not to mention, file access on an SD card is very slow, when the system is simultaneously accessing the swap file.
You don't seem to understand what swap is and what it is used for.

If your system is normally using swap then you are trying to make it do too much,
Until it runs out or RAM the presence of swap will make no difference to the system performance.
At the point that it starts to swap it will continue to work but at lower performance. This should be obvious and at that point you should reduce the system load (close some windows, close some browser tabs etc etc).
Without the swap the system will become unstable as the kernel starts killing processes to get back some free memory.

Also see thagrol's reply.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 11:56 am

thagrol wrote:
Sat Aug 17, 2019 11:17 am
If your system needs to use the swap file disabling it will only slow things down and make your system less responsive.

If your system isn't using the swap file disabling it will make no difference.

Having a swap file present does not mean it is being used.
Of course it is used, why else do you think it's there?
If you don't need it, just remove it.
There is no reason to believe it will slow down, as everything is loaded in RAM, and as long as you're not running out of RAM, it'll actually work faster.

It's been proven over and over again. You can check the thousand of articles about the performance improvement of this method.

User avatar
PeterO
Posts: 5086
Joined: Sun Jul 22, 2012 4:14 pm

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:00 pm

ProDigit wrote:
Sat Aug 17, 2019 11:56 am
thagrol wrote:
Sat Aug 17, 2019 11:17 am
If your system needs to use the swap file disabling it will only slow things down and make your system less responsive.

If your system isn't using the swap file disabling it will make no difference.

Having a swap file present does not mean it is being used.
Of course it is used, why else do you think it's there?
If you don't need it, just remove it.
There is no reason to believe it will slow down, as everything is loaded in RAM, and as long as you're not running out of RAM, it'll actually work faster.

It's been proven over and over again. You can check the thousand of articles about the performance improvement of this method.
1000's of articles that you might have misunderstood !
Show us one that proves your point...
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:11 pm

I'm going to stop the argument right here, Peter,
Not going to argue with someone who wants to believe in fairytales.
It's well enough documented. If you want to sow discord and misinformation, go ahead.
Or if you're so sure you're right, you could perhaps explain what you mean, because I don't think a lot of people would agree with the assessment that a swap file speeds up programs.

It's counterintuitive, and a well known fact since the 1990s.

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

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:19 pm

A swap file was never intended to speed up programs. It's purpose is to allow you to run more programs than you have RAM for.

Neither PeterO or I made the claim that a swap file (or a swap partition) speeds up programs. We both made the claim (correctly) that unused swap has no effect on performance.

Of course swapping slows things down but the alternative is an unstable or unusable system. Though if your system needs swap you're doing something wrong.

Think of swap space like a condom: better to have it and not need it than to need it and not have it.

As for "sow[ing] discord and misinformation" that appears to be arising as a result of your position and (mis)understanding not ours.

Run

Code: Select all

free -m
to see how much swap is actually being used. For comparison, here's the output from my 4B 1GB NAS/DNS/DHCP/PXE/VPN gateway server:

Code: Select all

              total        used        free      shared  buff/cache   available
Mem:            974         104         590           7         279         800
Swap:          2546           0        2546
It has a 2.4GB swap partition but as you can see it is not being used so has no impact on performance.
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

PiGraham
Posts: 3666
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:37 pm

This is an interesting article on swap space.
Linux Performance: Why You Should Almost Always Add Swap Space

We know that using swap space instead of RAM (memory) can severely slow down the performance of Linux. So then, one might ask, since I have more than enough memory available, wouldn’t it better to remove swap space completely? The short answer is, No. There are performance benefits when swap is enabled, even when you have more than enough ram.


It’s normal and can be a good thing for Linux systems to use some swap, even if there is still available RAM. The Linux Kernel will move memory pages which are hardly ever used into swap space to ensure that even more cachable space is made available in-memory for more frequently used memory pages (a page is a piece of memory). Swap usage becomes a performance problem when the Kernel is pressured to continuously move memory pages in and out of memory and swap space.
...

To summarize:
— Swap I/O scales very poorly. If memory pages cannot be swapped only when the server is idle, then you should tune or disable swap. This is usually not the case, thus the “almost always” title of this blog post.
— With swap disabled, performance issues become noticeable very fast and the OOM killer may get you! :)

https://haydenjames.io/linux-performanc ... wap-space/

It may not negatively affect performance to have swap enabled, but I wonder if increases wear on an SD card even if you have enough RAM.

User avatar
PeterO
Posts: 5086
Joined: Sun Jul 22, 2012 4:14 pm

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:38 pm

ProDigit wrote:
Sat Aug 17, 2019 12:11 pm
I'm going to stop the argument right here, Peter,
Not going to argue with someone who wants to believe in fairytales.
Well your refusal to provide any evidence to support your beliefs leads to only one conclusion....

However , take a read of this https://haydenjames.io/linux-performanc ... wap-space/ (PiGraham beat me to it !)

It's well enough documented. If you want to sow discord and misinformation, go ahead.
Really ? You're going to accuse me of spreading "FUD" rather than produce evidence to back up yorur claims ?
Or if you're so sure you're right, you could perhaps explain what you mean, because I don't think a lot of people would agree with the assessment that a swap file speeds up programs.
Where did I say that it did ? But now we see where you're getting it all wrong. A swap file isn't there "to speed up programs". It's there to delay the point at which the system runs out of memory and becomes unstable. On a desk top system it might give you time to close a few applications in an orderly fashion before the kernel starts killing them off for you. It might also let your system cope with a peak in load that would otherwise cause it to become unstable or crash.
It's counterintuitive, and a well known fact since the 1990s.
Every Unix and Linux system I've used since the early 1980s has had swap space. I've managed some large systems where swap has allowed them to keep running during peaks in load which would have otherwise brought them to a standstill.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

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

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:44 pm

PeterO wrote:
Sat Aug 17, 2019 12:38 pm
Every Unix and Linux system I've used since the early 1980s has had swap space. I've managed some large systems where swap has allowed them to keep running during peaks in load which would have otherwise brought them to a standstill.

Yep. and MSDOS and Windows from the early 90s at least.
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

User avatar
PeterO
Posts: 5086
Joined: Sun Jul 22, 2012 4:14 pm

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 12:45 pm

PiGraham wrote:
Sat Aug 17, 2019 12:37 pm
This is an interesting article on swap space.

It may not negatively affect performance to have swap enabled, but I wonder if increases wear on an SD card even if you have enough RAM.
I doubt you could measure the effect. I think writting all those log files in /var causes more wear and tear to the SD card.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 1:21 pm

Most wear and tear comes from the controller going haywire, and not being able to execute 2 commands at a time (especially small writes). On most older USB and as cards, it's not the memory cells that disintegrate, but the controller. Which is why many older SD cards,or MicroSD cards break down all of a sudden, and won't work at all.

The swap file is definitely being used, as even a bare system uses 80MB of swap file.

Back in the day, swap files became a thing, to extend the ram memory, but over time RAM memory became sufficient, and there no longer was the need for it.
If you have 512mb of ram, and a gui, you may need a swap file. But with 1 GB and Raspbian, you can do a lot before running out of memory.

This thread exists, because the writer realized the cons of a swap file, and probably realized he didn't need it.

There is no proof that the system does not access swap, when not needed. It looks quite the opposite, as I'd this was the case, swap size would be zero.

Removing a swap file, will reduce the drag and delays (latencies) associated with having a swap file.
It's not as much speeding up the system, as it is reducing the delay caused by having a swap file.

I really can't explain any better, but if you're not Willing to do the research, and just GOOGLE the topic, I really can't do much about that.

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

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 1:27 pm

ProDigit wrote:
Sat Aug 17, 2019 1:21 pm
I really can't explain any better, but if you're not Willing to do the research, and just GOOGLE the topic, I really can't do much about that.
Couple of reasons that won't help your (or my) position:
  • Traditionally it's your job to support your argument with evidence not mine.
  • There's no guarantee google will return the same results for me as it did for you and in the same order. We might not use the same search terms and google does result filtering based on your search history too.
  • I don't use google. (Because of thier logging but that's an entirely different discussion)
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

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

Re: Permanently disable swap on Raspbian Buster

Sat Aug 17, 2019 1:35 pm

ProDigit wrote:
Sat Aug 17, 2019 1:21 pm
There is no proof that the system does not access swap, when not needed. It looks quite the opposite, as I'd this was the case, swap size would be zero.
That's a false assumption. If swap space were created on the fly as needed that would slow things down even further and lead to disc fragmentation and slower swap.

Swap space is pre-allocated so that it is A. contiguous on the storage medium, and B. does not required additional disc access to create or expand it when used.

As I said before. The existance of a swap file/partition doews not mean that it is being used though as with any other disc volume/partition there may be a small amount of config data present on it.

Again, run

Code: Select all

free -h
or

Code: Select all

free -m
on a system with swap enabled to see how much is actually used. As shown in my example, swap space can be configured and exist on disc without being used by the system.
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Return to “Troubleshooting”