• Welcome to Peterborough Linux User Group (Canada) Forum.
 

Why you should have swap even if you have lots of memory

Started by Jason, September 18, 2021, 10:17:44 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Jason

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.
* Zorin OS 17.1 Core and Windows 11 Pro on a Dell Precision 3630 Tower with an
i5-8600 3.1 GHz 6-core processor, dual 22" displays, 16 GB of RAM, 512 GB Nvme and a Geforce 1060 6 GB card
* Motorola Edge (2022) phone with Android 13

fox

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 24.10 on 2019 5k iMac
Ubuntu 24.04 on Dell XPS 13

Jason

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.
* Zorin OS 17.1 Core and Windows 11 Pro on a Dell Precision 3630 Tower with an
i5-8600 3.1 GHz 6-core processor, dual 22" displays, 16 GB of RAM, 512 GB Nvme and a Geforce 1060 6 GB card
* Motorola Edge (2022) phone with Android 13

fox

Quote from: Jason 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. ....

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 24.10 on 2019 5k iMac
Ubuntu 24.04 on Dell XPS 13

Jason

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:

swapon -show

On my system, that shows:

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:

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:
/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:

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.
* Zorin OS 17.1 Core and Windows 11 Pro on a Dell Precision 3630 Tower with an
i5-8600 3.1 GHz 6-core processor, dual 22" displays, 16 GB of RAM, 512 GB Nvme and a Geforce 1060 6 GB card
* Motorola Edge (2022) phone with Android 13

ssfc72

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 20.3 on a Dell 14" Inspiron notebook, HP Pavilion X360, 11" k120ca notebook (Linux Lubuntu), Dell 13" XPS notebook computer (MXLinux)
Cellphone Samsung A50, Koodo pre paid service

Jason

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.
* Zorin OS 17.1 Core and Windows 11 Pro on a Dell Precision 3630 Tower with an
i5-8600 3.1 GHz 6-core processor, dual 22" displays, 16 GB of RAM, 512 GB Nvme and a Geforce 1060 6 GB card
* Motorola Edge (2022) phone with Android 13