Create or Resize Swap file/Partition

To create a swap file or resize the swap file.

  1. Check current swap usage.
    Open a terminal and enter the following command to check if you already have a swap file or partition in use:

    swapon --show

  2. Create a new swap file (if you don't have one):

    sudo fallocate -l 2G /path/to/swapfile
    sudo chmod 600 /path/to/swapfile
    sudo mkswap /path/to/swapfile

    Replace /path/to/swapfile/partition with the actual location where you want to create the swap file (e.g. /swapfile, /Partition-ID).

  3. Activate the swap file:
    Once the swap file is created, activate it using the following command:

    sudo swapon /path/to/swapfile

    "Again, replace /path/to/swapfile/partition with the actual location of your swap file".

  4. Verify the swap.

    swapon --show

  5. Make the swap file permanent.
    By default, the swap file will be active until you reboot the system. To make it permanent, you need to add an entry in the /etc/fstab file.

    sudo nano /etc/fstab

    Add the following line at the end of the file:

    /path/to/swapfile none swap sw 0 0

    Save the file.

  • Resize the swap file.
    If you want to resize the swap file later, you can follow these steps:

    • Disable the current swap file:

      sudo swapoff /path/to/swapfile

  • Resize the swap file using the fallocate command (as shown in step 2) with a different size.

    • Enable the newly resized swap file:

      sudo swapon /path/to/swapfile

Swap files are generally used when there is not enough physical RAM available for running applications. It's essential to consider your system's specific requirements when creating or resizing swap files. Also, ensure you have enough free disk space to accommodate the swap file size.

2 Likes

This does not appear to work when ZFS is selected on install.
swapon: /swapfile: skipping - it appears to have holes.

According to the ZFS manual, swapfiles are not supported:

Do not swap to a file on a ZFS file system. A ZFS swap file configuration is not supported.

The classic approach of having a dedicated partition should do the trick.

For reference:

I use this to change the size of my swap file. Useful if you use Chrome. I have 8GBs and the default 2GB swap file is too small.

sudo swapoff -a ; sudo fallocate -l 8G /swapfile ; sudo chmod 600 /swapfile ; sudo mkswap /swapfile ; sudo swapon /swapfile ; sudo swapon --show

Edit for size.

Works instantly. May take a while for use to show up

Reviving a super old thread here, but @Ocka I see you allocated 2G for your swapfile. For context I have a lot of disk space, and a lot of RAM as well(32 GB) so space is not an issue.

Is 2G part of the demonstration, or is this mostly up to us? For example, at least with swap partitions, I had thought about creating a 8G partition. Could that translate easily to a swap file? Thank you for your help

2G in the first post is just an example size. with 32gb ram u can set whatever, 8G is fine.

-sudo swapoff -a
-sudo fallocate -l 8G /swapfile
-sudo chmod 600 /swapfile
-sudo mkswap /swapfile
-sudo swapon /swapfile
-swapon --show

put /swapfile in /etc/fstab so it comes back after reboot. if ur root is zfs skip the file and use a swap partition or zram instead.

1 Like

A note: What makes RAM what it is, is that it is very fast. It operates differently than a permanent storage device.
Using Swap can help someone with limited RAM to prevent freezing, crashes or data loss.

But for people that have 32 Gigs or 64 Gigs, Swap is superfluous. It will not do anything for you, at all.
The system reallocates RAM, so even if you check and your RAM usage is hovering at 98%, it is rotating out the dumps and keeping your processes steady.
With limited or low RAM, like say, 4 Gigs, that could be a problem if a chunk of memory happens to be larger than the dump space allows for it to fit in.
You have eight times that amount.

Swap writes temporary memory to your disk, not RAM, so accessing it is slower, read and write is slower and it adds to SSD read/write cycles.
If you already set up Swap with the two gigs, I would not bother changing it or worrying about it. You will very likely never even use it.

2 Likes

Thank you @Aravisian! I am a first time linux user and came across swap in my reading.I haven't set up swap with the two gigs, I was going to wait for anything on this thread. That being said, I think just to practice and see how it looks I'll go ahead and do it. I really appreciate your help.

1 Like