Some time ago, I got on the car of Tencent Cloud 360 to buy the [student machine] (https://cloud.tencent.com/act/campus?from=10680) activity for more than three years. In addition, I got 6 years in total. However, after the memory dropped to 1G, it took a long time to run mysql and the memory was not enough. And Tencent Cloud's ubuntu doesn't know why the 1G memory is actually only 800+M, which is even worse.
Finally, two days ago, the server crashed due to a memory burst and no swap was opened. It almost crashed, and ssh was not connected. The console was forced to restart before it returned to normal. In order to prevent this from happening again, I added a swap to the server.
Reference document: https://askubuntu.com/questions/33697/how-do-i-add-a-swap-partition-after-system-installation/796997#796997
First, let's understand what is Swap
Swap partition (also called swap partition) is an area on the hard disk that is designated as a place where the operating system can temporarily store data, and these data can no longer be stored in RAM. Basically, this allows you to increase the amount of information the server retains in the working "memory", but there are some considerations, mainly when there is not enough space in the RAM to accommodate the application data being used, the data on the hard drive will be used Swap space.
Information written to disk will be much slower than information stored in RAM, but the operating system is more willing to store application data in memory and use it to exchange old data. In general, when the system's RAM is exhausted, using swap space as a fallback space may be a good safety net to prevent non-SSD storage systems from running out of memory.
The specific steps and commands are as follows:
# Create an empty file, the specific size is recommended to be twice the memory for small memory machines(1K in the example* 4M =4 GiB).
sudo mkdir -v /var/cache/swap
cd /var/cache/swap
sudo dd if=/dev/zero of=swapfile bs=1K count=4M
sudo chmod 600 swapfile
# Convert the newly created file to a swap file.
sudo mkswap swapfile
# Open swap.
sudo swapon swapfile
# Verification by swapon or top command:
swapon -s
# or
top -bn1 | grep -i swap
# Will show similar information: KiB Swap:4194300 total,4194300 free
# You can use sudo swapoff swapfile when swap is disabled.
# Set the partition to load at boot.
echo "/var/cache/swap/swapfile none swap sw 0 0"| sudo tee -a /etc/fstab
# Test boot loading:
sudo swapoff swapfile
sudo swapon -va
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts