Is Installing a Swapfile ok?

Oh dear! I’ve now spent three days learning and setting up the system and now I’m doing it all over again! :joy:

Arguably there is very little upside to running a swap file if you are using nvme drives. See:

https://askubuntu.com/questions/652337/why-are-swap-partitions-discouraged-on-ssd-drives-are-they-harmful

Personally, I do not use swap files/ partitions on machines with modern ssd/ nvmes. I have seen no problems. When I use swap is with older devices for speed boost.

Basically it boils down to your use-case.

1 Like

While I agree in essence, your reasoning is flawed.

If one wishes to hibernate a system, whatever is in RAM must be written to disk – to the swap partition (or file). Therefore there must be sufficient swap space for that to occur.

If you don’t care about hibernation then you should just use zram and be done with it. Also there is nothing wrong with swap file.

1 Like

@Tut_tut

Create a new Swap File

OK. I’ve written this procedure for you to follow; simple copy/paste instructions. This should create the new swap file needed.

Note: Some of these commands may take a while to complete, so try not to cancel them prematurely.

  1. Remove the existing swap file:
sudo swapoff /swapfile
sudo rm -f /swapfile
  1. Create and initialize a new 16 GiB swap file:
  • The formula to calculate the count= value is 1024 x RAM;
    Example: 1024 x 16 = 16384.
sudo dd if=/dev/zero of=/swapfile bs=1M count=16384 status=progress
  • OR a 20 GiB swap file, instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=20480 status=progress
  1. Set permissions and format the swap file:
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
  1. Add a reference to the swap file in /etc/fstab:
  • First check if the swap file line exists;
  • If it does, then skip to Step 5:
cat /etc/fstab
  • If it’s not there, add it with:
sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab"
  1. Reboot

Done.

I hope this helps. Cheers.

1 Like

The good thing of having a swap file instead of a swap partition is that you can always easily adapt it’s size to your needs.

@soundofthunder gave you step by step instructions to get to a larger one

But you don’t really need to do this unless you want to set up hibernation or are otherwise sure that your current 512 MB swap file is not big enough.

If I where you, I’d opt for a swap file as big as my RAM.
But with the size of solid state drives, that much swap seems like a waste of disk space.

I have got 8 GB of RAM, 4 GB of swap (file)
Swap rarely gets used with what I do and hibernation works just fine with this size
even though it is only half the recommended size.

Systems on nvme solid state drives are booting so fast that hibernation isn’t really needed.

I still have a rotating disk. Plenty of space (1TB) - but speed matters here and does make a difference.

The argument is sound, up to the point when you have 5-6 GB ISO file, or Video file being edited (or, whatever) that happens to be in RAM when you decide to hibernate the machine…

Oops! :scream_cat:

I don’t hibernate, either. I have 64GiB RAM and a 32GiB swap partition, but I carefully calculated and anticipated my needs beforehand.

I’ll add this here for the OP to find, if he also has no desire to use hibernation, and wishes a smaller swap file:

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress

This command creates only an 8GiB swap file (and replaces the respective command in my previous instructions (if needed).

… I know.

And that’s the point.
I know. You know. But he doesn’t.

… and I did not intend to persuade him to do as I did

Apparently I never tried to hibernate with too much of my RAM being used so that it would fail.
I don’t know what would happen then - probably an fsck on the next reboot and perhaps some of the more recent changes lost due to it.

To be very honest - there is not that much (time) difference between coming out of hibernation and a clean boot - the reading back of 4 GB from swap takes it’s sweet time.

I appreciate you taking the time and effort to guide him to change what he has got now.
Very nice of you!

I already like this forum! You are really nice and knowledgeable helpers! I now have a 16 GB swap file using the instructions written especially for me! Many many thanks! :pray:

LC_ALL=C cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a device; this may
# be used with UUID= as a more robust way to name devices that works even if
# disks are added and removed. See fstab(5).
#
# <file system>             <mount point>  <type>  <options>  <dump>  <pass>
UUID=22F4-54A6                            /boot/efi      vfat    umask=0077 0 2
UUID=d8ff5d2f-cd13-4534-b598-7ae3f9f4950e /              ext4    defaults,noatime 0 1
/swapfile                                 swap           swap    defaults,noatime 0 0
tmpfs                                     /tmp           tmpfs   defaults,noatime,mode=1777 0 0
1 Like

You’re very welcome.
Don’t forget to tick my post (the one with the instructions). :slight_smile:

Yes, it’s OK to install a swap file but it feels old-school to me

I don’t understand why people don’t use zram instead, it’s more dynamic and costs less hdd space.

    Install zram-generator using one of the methods listed above.
    Create a zram-generator.conf config file.
    Run systemctl daemon-reload to create new device units.
    Run systemctl start /dev/zram0 (adjust the name as appropriate to match the config).
    Call zramctl or swapon to confirm that the device has been created and is in use.

Once installed and configured, the generator will be invoked by systemd early at boot, there is no need to do anything else.

source: GitHub - systemd/zram-generator: Systemd unit generator for zram devices

other sources:

https://wiki.archlinux.org/title/Zram

I want hibernation - can’t be done with dynamic swap files/zram. :man_shrugging:

It’s probably good for the OP though …

Did I ?
Usually I suggest zram which should be coupled with disabling zswap, as the two together creates problems.

ex:

Continuing the discussion from Extremely weird issue involving RTX 3060:

If you decide to use zram instead of a swap file & don’t want to have to fiddle around with a configuration file (as required with zram-generator), then a good alternative in the AUR is systemd-zram (and there are plenty of other zram enablers available in the AUR repository too). I recently installed systemd-zram with its default settings (lzo compression, cache size 75% of RAM size) & it works just as it should.

After I installed systemd-zram all I had to do was run (as root) systemctl enable --now systemd-zram to start it immediately and also have it run automatically at boot.

https://aur.archlinux.org/packages/systemd-zram

Edit: Make sure that you also follow @cscs’s previous instruction to disable zswap in the boot options.

But its from the AUR and has not been maintained in 5 years.

Which doesnt mean it does not work. But you might keep it in mind in case something slips.

Unless Linux gets rid of LZO compression, I don’t see that there’s much that can slip (and, even if that happened, the user would just have to edit one line in the systemd-zram.service file to change to another compression algorithm).

I wasn’t criticising zram-generator - I tried it myself but my ADHD-addled brain must have missed a set-up step somewhere as it wouldn’t start, so I went with an alternative (systemd-zram) that required no setting-up (apart from enabling/starting the service) & instantly did the job for me. And, at the moment, it is working fine:

free --human
               total        used        free      shared  buff/cache   available
Mem:            28Gi       5.0Gi        19Gi       592Mi       4.7Gi        23Gi
Swap:           21Gi        82Mi        21Gi

When I used Arch, just over one year ago, I used to use:

AUR (en) - zramswap

… also from 2019

but what it relies on hardly ever changes

1 Like

I thought so, and it was actually zram. :slight_smile:

I still had zswap in my mind after seeing it briefly in the inxi output. I dismissed soon after and decided to speed things along a little, as nobody seemed to be making much progress.

Yes, I thought so too; especially considering not much else seemed to be in OPs best interest. However, thanks for highlighting zram as a possible alternative. I’m sure others reading might be inspired, as the use of systemd units is slowly beginning to gain some traction for various tasks.

Unfortunately, the dynamic nature of zram isn’t ideal when taking into consideration hibernation, as @Nachlese noted earlier. The OP hasn’t reacted to hibernation when it was mentioned several times, but has allowed swap space for it, so I presume needs have been met.

I was considering using zram myself for a side project, using a 250GiB SSD, where available space actually matters.

1 Like

I do keep forgetting to mention zram does not support hibernation.
I might guess most people dont use it either, but its an important caveat.

2 Likes

This topic was automatically closed 3 hours after the last reply. New replies are no longer allowed.