Enable swap on my Gen 4 VPS & Dedicated Server
Swap on a server provides additional virtual memory when physical RAM is exhausted. It allows the system to temporarily store data on disk, preventing crashes or slowdowns due to memory limitations and ensuring smooth operation of applications. Swap can be enabled on your server through SSH.
Warning: This article covers an advanced technical topic. If the steps are completed incorrectly, it's possible to make unwanted changes to your site. Therefore, we strongly recommend creating a backup before you proceed. Please review the full Statement of Support to learn more about available support for additional products and services.
Warning: Relying heavily on swap can lead to performance degradation, as accessing data from disk is slower than retrieving it from RAM, potentially causing noticeable delays in application responsiveness. Therefore, we strongly recommend only using swap as a temporary solution while you work on optimizing your sites and server processes.
- Connect to your server using SSH.
- Switch to the root user.
- Create a file that will be used for swap:
sudo fallocate -l 1G /swapfile
- Set the permissions for the file:
sudo chmod 600 /swapfile
- Set up a Linux swap area on the file:
sudo mkswap /swapfile
- Enable swap:
sudo swapon /swapfile
- Using your preferred text editor command, open your
/etc/fstab
file, append the following line of code, and then save your changes:
/swapfile swap swap defaults 0 0
- Verify that swap is active:
sudo swapon --show
An output similar to the following will verify that swap is enabled:
NAME TYPE SIZE USED PRIO /swapfile file 1024M 0B -2
More Info
- 檢視Linux伺服器上的CPU和記憶體
- Remember to disable swap after you’ve optimized your sites and server processes.