Creating a Swap File 4GB
sudo dd if=/dev/zero of=/swap count=4096 bs=1MiB
Now set the permissions:
sudo chmod 600 /swap
and format the swap file:
sudo mkswap /swap
To enable the swap file just use:
sudo swapon /swap
To make the swap be automatically mounted after reboot, the following command will add it to your fstab config:
sudo echo "/swap swap swap sw 0 0" >> /etc/fstab
source : https://www.netweaver.uk/create-swap-file-centos-7/