Here's how to enable swap space:
-
Create a swap file (swap file) or swap partition (swap partition), and format it as a swap file system type supported by Linux. For example, a swap file of size 4GB can be created with the following command:
sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile
-
Enable swap space, and configure swap files or swap partitions into the system. For example, a previously created swap file can be added to the swap space with the following command:
sudo swapon /swapfile
-
Configure swap space to be automatically enabled. Edit
/etc/fstab
the file and add the following lines to it:/swapfile none swap sw 0 0
In this way, the system will automatically add the swap file or swap partition to the swap space at each boot.
Please note that enabling swap space can relieve memory pressure, but it will also bring some performance overhead. Therefore, before enabling swap space, make sure you have tried other ways to reduce memory usage and have identified the need for enabling swap space.