Welcome! Log In Create A New Profile

Advanced

How to create and use a Swap file

Posted by bodhi 
How to create and use a Swap file
December 27, 2022 03:55PM
This instruction will be revised to add more detailed discussion. Below are the basic steps to create a swap file and activate it. Use either section A for a Debian way to manage swap file, or section B if you prefer to manually create the swap file and activate it.

A. Install Debian package to manage the swap file automatically.

(Thanks Trond (tme) for the recommendation).

apt-get install dphys-swapfile
And then adjust the swap factor in /etc/dphys-swapfile to 4.
CONF_SWAPFACTOR=4

TBD: more details will be added here.

B. Manual Steps to create a swap file and activate it.

1. Create the swap file of 512MB at the rootfs top directory
cd /
dd if=/dev/zero of=swapfile1 bs=1024 count=524288
mkswap swapfile1 
chmod 0600 swapfile1

Note that the above 512MB is just an example (for boxex that have 128MB RAM). The swap file size should be 4x the amount of RAM in your box.

2. Turn on swap
swapon /swapfile1

3. Check the swap space

free -h
total        used        free      shared  buff/cache   available
.....
Swap:          511Mi          0B       511Mi

swapon
NAME       TYPE SIZE USED PRIO
/swapfile1 file 512M   0B   -2


4. Add swapfile1 entry to fstab so that it will be automatically on during boot.

cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
LABEL=rootfs   /               ext3    noatime,user_xattr,acl,errors=remount-ro 0 1
/swapfile1     swap            swap    defaults          0       0
tmpfs          /tmp            tmpfs   defaults          0       0

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)



Edited 6 time(s). Last edit at 01/09/2024 06:02AM by bodhi.
tme
Re: How to create and use a Swap file
January 07, 2024 11:19AM
Hi bodhi,

I would recommend the package 'dphys-swapfile' rather than manually creating a swap file. By default it creates '/var/swap' twice the size of the onboard memory and enables swapping and paging to it. The 'dphys-swapfile' package is very small (~20 kB).

One advantage is that swap size increases if extra memory is added. Another is that '/etc/rc.local' does not have to deal with the swap file.

Regards,
Trond Melen
Re: How to create and use a Swap file
January 07, 2024 10:52PM
Hi Trond,

Thanks for recommending this. It would be a good alternative for creating a swap file (I'll add it to the 1st post). My intention for manual steps is educational, i.e. the users can see at a lower level what are the steps to create and use a swap file.

> I would recommend the package 'dphys-swapfile'
> rather than manually creating a swap file. By
> default it creates '/var/swap' twice the size of
> the onboard memory and enables swapping and paging
> to it. The 'dphys-swapfile' package is very small
> (~20 kB).
>

By the way, the approach of 2x RAM size for swap space is out-of-date for modern kernels. It should be 4x RAM (at the minimum) with BFQ IO scheduler.

> One advantage is that swap size increases if extra
> memory is added. Another is that '/etc/rc.local'
> does not have to deal with the swap file.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
tme
Re: How to create and use a Swap file
January 08, 2024 01:16AM
Hi bodhi,

Quote

It should be 4x RAM (at the minimum) with BFQ IO scheduler.

Good tip! Fixed:
$ diff /etc/dphys-swapfile.orig /etc/dphys-swapfile
20c20
< #CONF_SWAPFACTOR=2
---
> CONF_SWAPFACTOR=4

$ /sbin/swapon
NAME      TYPE SIZE USED PRIO
/var/swap file 1.9G 256K   -2

Regards,
Trond Melen
Re: How to create and use a Swap file
July 25, 2024 02:06PM
Just been updating some of my Pogo servers to Bookworm and I saw this post re Swapfiles. Hadn't seen dphys-swapfile before so that saved a lot of hassle. I am just upgrading the boot SDCARDs in the Pogo 4 devices to Sandisk 32GB cards designed for dashcams and surveillance cams. I am guessing they have inbuilt sector write balancing and other safeguards to prevent premature failure. Does anyone know if these cards (or similar types) of cards are more resilient than their counterparts when it comes to using a swap file on them?
Thanks - Bob
Re: How to create and use a Swap file
July 25, 2024 02:52PM
Bob,

> the Pogo 4 devices to Sandisk 32GB cards designed
> for dashcams and surveillance cams. I am guessing
> they have inbuilt sector write balancing and other
> safeguards to prevent premature failure.

I'd hope they have sophisticate write balancing. What you can also do is to over provision, i.e. leave about 2 to 4 GB unallocated. Those unallocated sectors will be used by the controller whenever there is a bad sector.

> Does
> anyone know if these cards (or similar types) of
> cards are more resilient than their counterparts
> when it comes to using a swap file on them?

Sandisk USB is among (if not) the best flash drive brand. But swapping to SD cards or USB flash drive should be kept to a minimum.

Wiki thread

Quote

Flash drives issues

How to prolong the life of your rootfs USB thumb drive

Memory & Swap Settings

Tuning for low RAM boxes. Also read several posts after for setups for logging to RAM.

Because yours is a dedicated usage I would set the swappiness to 1. And also you could use several swap files (but this is probably overkilled for your use case).

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: