How to add swap partition in Ubuntu

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

How to add swap partition in Ubuntu
Explain how to add swap partition on Ubuntu 16.04
How to add users to Sudoers in Ubuntu
How to install Helm in Ubuntu
How to use hanlp in ubuntu
How to install mysql in Ubuntu 14.04
How to install mysql in Ubuntu 14.04
How to add swap on CentOS 7
How to easily compile openJDK in Ubuntu
How to install cuda10.1 driver in Ubuntu
How to delete redundant kernels in Ubuntu
How to delete redundant kernels in Ubuntu
How to install ROS Noetic in Ubuntu20.04
How to set static IP in ubuntu14.04
How to add custom modules in Python
How to modify software source in Ubuntu 7.10
How to open root account in Ubuntu20.04
How to add background music in python
How to hide applications in Ubuntu Dash?
How to upgrade to Ubuntu 20.04
How to upgrade to Ubuntu 20.04
How to add Apt software source on Ubuntu
How to modify time zone and time in ubuntu
How to configure TensorFlow use environment in Ubuntu
How to install python in ubuntu server environment
How to use dpkg command in Ubuntu system
How to open the ubuntu system in win10
How to configure /var/log/messages in Ubuntu system log
How to add logging module to Nginx on Ubuntu 16.04
How to install Hadoop in standalone mode on Ubuntu 18.04
How to add the gzip module to Nginx on Ubuntu 14.04
How to view detailed network routing table in Ubuntu
How to use Putty to log in to ubuntu installed in VirtualBox
How to modify time zone and time in ubuntu system
Example of how to modify ip address in Ubuntu20.04
How to create a Python virtual environment in Ubuntu 14.04
How to upgrade to Ubuntu 16.04 LTS
How to control the ubuntu system in win10 through ssh
How to install memcache and start it in ubuntu environment
How to compile and install PHP and Nginx in Ubuntu environment
How to map shared disk to window in linux under Ubuntu
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to wrap in python code
How to install PHP7.4 in CentOS
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to compile ijkplayer-android under ubuntu
How to install Anaconda3 on Ubuntu 18.04