Ubuntu adds methods for swap partitioning

  • 2020-09-16 07:56:05
  • OfStack

Some time ago, I got on the bus of Tencent Cloud 360 and bought a student computer for more than 3 years. I gained 6 years in total with the matching reduction. However, after the memory was reduced to 1G, I ran mysql for a long time without enough memory. What's more, Tencent Cloud's ubuntu has only 800+M memory in 1G for unknown reasons, which makes things worse.

Finally, two days ago, the server crashed due to a memory burst without opening swap, almost crashed, ssh could not even be connected, the console forced restart to restore normal. To prevent this from happening again, I added swap to the server.

Reference: https: / / askubuntu com/questions / 33697 / how - do - i add - a - swap - partition - after - system - installation / 796997 # 796997

First, what is Swap

An Swap partition (also known as a swap partition) is an area on a hard disk that is designated as a place where the operating system can temporarily store data that can no longer be stored in RAM. Basically, this allows you to increase the amount of information the server holds in working "memory," but there are a few considerations, mainly that swap space on the hard drive will be used when there is not enough space in RAM to hold the application data being used.

Information written to disk will be much slower than information stored in RAM, but the operating system prefers to keep application data in memory and swap old data. In general, using swap space as fallback space can be a good safety net for non-ES39en storage systems that run out of memory when the system runs out of RAM.

The specific steps and commands are as follows:


#  create 1 The size of an empty file is recommended to be twice the size of a small memory machine  ( In the example  1K * 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  swap  file .
sudo mkswap swapfile

#  open  swap.

sudo swapon swapfile

#  through  swapon  or  top  Command validation :
swapon -s
#  or 
top -bn1 | grep -i swap
#  Similar information is displayed : KiB Swap: 4194300 total, 4194300 free

#  disable  swap  Can be used in  sudo swapoff swapfile.

#  Set the partition to boot load .
echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

#  Test boot load :
sudo swapoff swapfile
sudo swapon -va

Related articles: