swap (virtual memory) setting method in ubuntu

  • 2020-11-25 07:44:20
  • OfStack

ubuntu (Virtual machine) version 16.04 LTS

Add swap

View the swap partition size with free


free -m

Create 1 swap folder


mkdir swap
cd swap
sudo dd if=/dev/zero of=swapfile bs=1024 count=100000

Where the size of count corresponds to the size of swap, as shown in the above code, count= 100,000 is about 100M

Note: When setting the value of count, please note that setting the size of swap beyond the size of the hard disk will cause the GRAPHICAL interface of ubuntu to be inaccessible after setting the restart

Convert the generated file to an swap file


mkswap swapfile

Activate the swap file


swapon swapfile

Modify /etc/fstab auto mount if you want it to start automatically every time you enter:

Add to file


/disk2/swap swap defaults 0 0 

(Since I created the swap file in disk2, the path is /disk2/swap)

If it does not start automatically, every time you reboot, go to the swap folder and activate it with the swapon swapfile command

Check the swap partition size again to make sure that the swap setting was successful


free -m

Delete swap

After use, delete the swap area

Go to the swap folder and close swap


cd swap
swapoff swapfile

Turn off auto mount (skip this step if you haven't modified /etc/fstab)


vi /etc/fstab

Add a # or delete the previous statement

Note: If you do not have permission to modify /etc/fstab, you will be prompted to save this file as read-only and cannot be modified. Solution: Save using the following statement


 : w  ! sudo tee%

delete


rm -r swapfile

Problems I encountered during configuration:

When swap is set to be larger than the hard disk size of the virtual machine, the graphical interface of THE virtual machine ubuntu cannot be entered after the restart. The following methods are adopted to solve this problem:


mkdir swap
cd swap
sudo dd if=/dev/zero of=swapfile bs=1024 count=100000
0

Check disk space to see if there is a full disk problem that prevents you from accessing the graphical interface


mkdir swap
cd swap
sudo dd if=/dev/zero of=swapfile bs=1024 count=100000
1

Found disk space full, need to delete 1 some files

You can use the rm-r statement to delete 1 file, or you can use the above mentioned methods to delete swap and reconfigure


Related articles: