Adding virtual memory (also known as swap space or swap files) to a cloud server can help you cope with low memory conditions, thereby improving system stability and performance. Virtual memory allows the operating system to move some data from physical memory to disk to free up physical memory for use by other applications.
Here are the general steps to add virtual memory to an ECS cloud server in the United States:
Log in to the server: Use a remote connection tool such as SSH to log in to your cloud server.
Check memory usage: In the terminal, use the following command to view the current memory usage:
free -h
This displays the physical memory and swap space usage.
Create a swap file: If you find that you are running out of physical memory, you can create a swap file. First, determine how much swap space you want to allocate. In general, the swap space size can be set to 1 to 2 times the physical memory. Here is an example of creating a swap file with a size of 2GB:
sudo fallocate -l 2G /swapfile
Set permissions and format: Set permissions on a swap file and format it as a swap file system:
sudo chmod 600 /swapfile sudo mkswap /swapfile
Enable swap file: Enable swap file for it to take effect:
sudo swapon /swapfile
To make sure the swap file still works after the server restarts, you need to add it to the /etc/fstab file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify: Run the free -h command again to verify that the swap space has been added and taken effect.
Note that the use of virtual memory can alleviate the problem of running out of memory, but it can also have some impact on performance because disk access is relatively slow. It is recommended to use virtual memory when you really need it, rather than relying on it for a long time. If the server continues to run out of memory, upgrade the physical memory of the server.