Create swap file to optimize virtual machine configuration or just immediately increase available swap space.
Debian version.
$ lsb_release -a
No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 10 (buster) Release: 10 Codename: buster
Display current memory and swap usage.
$ free -h
total used free shared buff/cache available Mem: 231Mi 50Mi 22Mi 0.0Ki 157Mi 171Mi Swap: 1.0Gi 6.0Mi 1.0Gi
Create swap file.
$ sudo dd if=/dev/zero of=/local.swap count=4 bs=1GiB
4+0 records in 4+0 records out 4294967296 bytes (4.3 GB, 4.0 GiB) copied, 59.6617 s, 72.0 MB/s
Set proper user and group.
$ sudo chown root:root /local.swap
Set proper permissions.
$ sudo chmod 600 /local.swap
Display swap file details.
$ ls -lh /local.swap
-rw------- 1 root root 4.0G Feb 16 20:42 /local.swap
Set up swap file.
$ sudo mkswap /swap.file
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes) no label, UUID=e7a648c2-a161-4818-932f-a08af41e88fd
Activate swap file.
$ sudo swapon /local.swap
Display swap summary.
$ sudo swapon --summary
Filename Type Size Used Priority /dev/sda5 partition 1046524 16424 -2 /local.swap file 4194300 0 -3
Display current memory and swap usage.
$ free -h
total used free shared buff/cache available Mem: 231Mi 44Mi 143Mi 0.0Ki 42Mi 178Mi Swap: 5.0Gi 16Mi 5.0Gi
Ensure that this solution is persistent across a reboot.
$ sudo echo "/local.swap swap swap sw 0 0" | sudo tee -a /etc/fstab
/local.swap swap swap sw 0 0