In some instances swap is disabled by default – usually on lightweight VPS virtual machines. If you find processes being killed and are unable to add physical memory to the machine, the short term remedy is to increase or enable swap space. The below commands will create a swap file of four gigabytes.
sudo dd if=/dev/zero of=/myswap count=4096 bs=1MiB
sudo chmod 600 /myswap
sudo mkswap /myswap
sudo swapon /myswap
Make the Swap File Permanent:
The swap file is enabled at the moment, but when we reboot, the server will not automatically enable the file for use. We can change that by modifying the fstab file, which is a table that manages file-systems and partitions.
Edit the file with sudo privileges in your text editor:
At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:
/swapfile swap swap sw 0 0
Flushing the Swap File:
swapoff -a && swapon -a
See more about freeing up virtual and physical memory
Disabling the Swap File:
In some environments in can be advantageous to disable swap…ie: high memory machines or SSDs.
sudo swapoff -a
Permanently Disabling the Swap File / Virtual Memory:
And # hash out the line indicated as swap then reboot:
# Created by anaconda on Thu Jan 31 19:49:31 2019
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root / xfs defaults 0 0
UUID=5cd945c2-5ca7-48c5-b67b-0f3610534e8f /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
# /dev/mapper/centos-swap swap swap defaults 0 0