Author Topic: Why you should have swap even if you have lots of memory  (Read 288 times)

0 Members and 1 Guest are viewing this topic.

Offline Jason

  • Administrator
  • Master
  • *****
  • Posts: 3823
  • Humanist. Skeptic. Husband.
Why you should have swap even if you have lots of memory
« on: September 18, 2021, 10:17:44 pm »
I found this article when looking for an answer to whether I could dispense with my swap partition. It's quite interesting although you don't really need to read all of it unless you're into performance tuning. Scanning it briefly, particularly the beginning is good enough.

The TL;DR (Too long; didn't read - Internet speak for a summary) of it is that it might be useful depending on the scenarios you use your computer for. Swap isn't just for getting additional "memory" for programs, it's also about swapping less-used processes to the disk drive so you have more real memory available. It's also might stop the kernel from killing programs if you get close to the limit of RAM. You can also configure the swap beyond just setting the size of it.

So, how do you handle swap on your system? Do you notice is being used?

On my system with 4 GB of RAM and shared RAM for video, it can end up being used a lot. Of course when it does, the system can slow to a crawl. But at least, it takes me longer to get there. zRAM is also handy for low-memory systems and I use that.
"With all its sham, drudgery, and broken dreams, it is still a beautiful world." - Max Ehrmann, Desiderata

Offline fox

  • Member
  • Master
  • *
  • Posts: 2007
Re: Why you should have swap even if you have lots of memory
« Reply #1 on: September 19, 2021, 06:50:19 am »
My computers all have 8-12 gb RAM. I don't run a lot of apps concurrently. Newer versions of Ubuntu don't install swap partitions anymore by default, and I don't think I come close enough to using all of my RAM to need one.
Ubuntu 21.10 on 2019 5k iMac
Ubuntu 20.04 on Dell XPS 13 2 in 1

Offline Jason

  • Administrator
  • Master
  • *****
  • Posts: 3823
  • Humanist. Skeptic. Husband.
Re: Why you should have swap even if you have lots of memory
« Reply #2 on: September 19, 2021, 05:42:43 pm »
You probably don't need it, but as an experiment, create a swap file and watch it in Task Manager to see if it's used at any point. If it is, than chances are your desktop can benefit from it. If not, it's just a file so you can easily remove it. You probably have enough disk space that it won't be a big deal. I believe swap is also needed if you want to use hibernation. The existing session is stored into swap until you come out of hibernation.

I'm going to be checking into it on the PLUG server. It only has 1 GB of a RAM and for some reason, the database server keeps crashing occasionally. I'm wondering if it runs out of RAM because of too many requests sometimes. We obviously don't use much but a flurry of spam bots may cause issues.

In any case, I know a few us run servers so it can definitely be useful there.

Btw, when I installed Linux Lite, which is a version of Ubuntu, a swap file was created. I don't know if that's something the Linux Lite people added or if it's something that is created if you only have 4 GB of RAM.

Also, I just remembered something you might find interesting. Even though the documentation for this Toshiba laptop, says it can only have a maximum of 6 GB of RAM, Linux says I can have 10 GB through a hardware info CLI tool. I was thinking of getting an 8 GB stick to try it, but not sure if I can return it if it doesn't actually support it. An 8 GB stick isn't that cheap for older computers.
"With all its sham, drudgery, and broken dreams, it is still a beautiful world." - Max Ehrmann, Desiderata

Offline fox

  • Member
  • Master
  • *
  • Posts: 2007
Re: Why you should have swap even if you have lots of memory
« Reply #3 on: September 19, 2021, 06:03:14 pm »
You probably don't need it, but as an experiment, create a swap file and watch it in Task Manager to see if it's used at any point. ....

I think that modern versions of Ubuntu create a swap file by default, but I have no idea where it is or how I see if it's working. What is Task Manager? When I searched it on Gnome, it came up with HTop and System Monitor.
Ubuntu 21.10 on 2019 5k iMac
Ubuntu 20.04 on Dell XPS 13 2 in 1

Offline Jason

  • Administrator
  • Master
  • *****
  • Posts: 3823
  • Humanist. Skeptic. Husband.
Re: Why you should have swap even if you have lots of memory
« Reply #4 on: September 20, 2021, 01:01:41 am »
System Monitor is what it's called in Ubuntu, I guess. It's Task Manager in Xfce. Htop works, too. It's a command-line program. Both should show your swap and how much of it you're using if it's there and enabled. In the terminal, you can enter this command to see if you have it working as well:

Code: [Select]
swapon -show
On my system, that shows:

Code: [Select]
Filename                     Type          Size    Used    Priority
/swapfilefile                 file         1358916 0       -2
/dev/zram0                    partition    490920  208680 5
/dev/zram1                    partition    490920  209292 5
/dev/zram2                    partition    490920  212688 5
/dev/zram3                    partition    490920  212092 5

Note that I'm using zram on my system which is something that you have to add separately. What it does is compress files that are then cached in the swap.

That command also turns on swap on and swapoff disables it. /etc/fstab lists present swap whether it's a file or a partition. If it appears there, then the swap is automatically configured to start at bootup. But you can also start it at any time with:

Code: [Select]
swapon -a
That command option reads /etc/fstab and creates all swapfiles and mounts any swap partitions. Swap is enabled.

Unless swap is in /etc/stab, it won't be automatically enabled at boot. In my fstab file the swap line looks like this:
Code: [Select]
/swapfile                                 none            swap    sw              0       0
If it's a file (as above), you don't need to mention the drive since it's on /.

If you don't have swap set up, it's recommended to create it with dd like this:

Code: [Select]
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Or create a partition for it. If it's a file, multiply the block size bs (it's in bytes) by the count to get the swap size, in this case, a gigabyte. You don't have to specify the block size and if you don't, it's set to 512 bytes. The math with 1024 is easier.

For kicks, I turned off swap on my system temporarily. It was using about 85% of RAM but otherwise was running fine with swap (in other words, it wasn't using swap as extra RAM but for caching less-used files so I didn't run out of RAM as easily. When I turned swap off, my system practically froze. I couldn't move the mouse. I dropped to a virtual terminal, logged in (which took more than a minute for it to respond to my login/password), and re-enabled swap. Everything is fine again.
"With all its sham, drudgery, and broken dreams, it is still a beautiful world." - Max Ehrmann, Desiderata

Offline ssfc72

  • Member
  • Master
  • *
  • Posts: 1855
Re: Why you should have swap even if you have lots of memory
« Reply #5 on: September 20, 2021, 02:55:01 am »
Thanks for the swap file/partition info Jason.
I just installed Mint 20.1 to a partition on my notebook computer and it did not make me create a swap partition.
I just ran the swapon -show command and it shows no hard drive space used for the swapfile.
Mint 19.1 on a Dell 14" Inspiron notebook, HP Pavilion X360, 11" k120ca notebook (Linux Lubuntu), Dell 13" XPS notebook computer (MX Linux)
Cellphone Samsung A50, PCMobile pay as you go

Offline Jason

  • Administrator
  • Master
  • *****
  • Posts: 3823
  • Humanist. Skeptic. Husband.
Re: Why you should have swap even if you have lots of memory
« Reply #6 on: September 20, 2021, 06:46:13 pm »
I guess unless you're using what is now considered a low memory system, less than a 4 GB, the distros don't bother with creating swap. They should still make one for hibernation or at least ask if you want that ability. Unlike suspend, it uses no power but will bring you back to your work session just like suspend does.
"With all its sham, drudgery, and broken dreams, it is still a beautiful world." - Max Ehrmann, Desiderata