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