Ubuntu changes the Swap swap space size

Foreword:

When installing the Ubuntu system, the default space allocation scheme was selected. The Swap space was only 1G, and the actual physical memory was 32G. The Swap space allocated was at least 1 times the memory, and preferably 2 times the memory value. The system was quite laggy. After redoing the system, why does the hard-to-deploy environment need to be redeployed? What a headache!

1. Check the memory size of the local Swap swap space

The command is as follows
free -m
The execution results are as follows:

longzhiye@longzhiye-laptop:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:          31995         374       30603           9        1016       31142
Swap:           976          65         911

You can see that the Swap space of this machine is about 976M, which is 1G space.

Next we will expand it to 64G

2. Create Swap file

sudo -i
After importing secretly, change root.

mkdir /swap
Create swap folder

cd /swap/
Enter the swap folder

sudo dd if=/dev/zero of=swapfile bs=64M count=1k
Create swapfile with size bs*count = 64M * 1k = 64G

sudo mkswap -f swapfile
Convert the generated file into a Swap file
The execution results are as follows:

longzhiye@longzhiye-laptop:~$ sudo -i
[sudo] longzhiye 的密码: 
root@longzhiye-laptop:~# cd /
root@longzhiye-laptop:/# mkdir /swap
root@longzhiye-laptop:/# cd swap/
root@longzhiye-laptop:/swap# sudo dd if=/dev/zero of=swapfile bs=64M count=1k
记录了1024+0 的读入
记录了1024+0 的写出
68719476736 bytes (69 GB, 64 GiB) copied, 276.311 s, 249 MB/s
root@longzhiye-laptop:/swap# sudo mkswap -f swapfile
Setting up swapspace version 1, size = 64 GiB (68719472640 bytes)
无标签, UUID=c7feaf13-7f02-4941-a07f-86a43bdf3ef5

3. Activate Swap file

sudo swapon swapfile
free -m
Activate the Swap file and re-check the Swap space memory
Execution results As follows:

root@longzhiye-laptop:/swap# sudo swapon swapfile
swapon: /swap/swapfile:不安全的权限 0644,建议使用 0600。
root@longzhiye-laptop:/swap# free -m
              total        used        free      shared  buff/cache   available
Mem:          31995         381         374           9       31238       31065
Swap:         66512          64       66448

You can see that the Swap space memory changes from 1G to 64G = our expanded 64G + original 1G.

4. Set as permanent Swap

If this step is not done, the settings before restarting will be invalid! ! ! !
sudo gedit /etc/fstab
Change the swap path.

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=b726da21-83a0-497b-b3eb-a09f16403d60 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=C0BE-3630  /boot/efi       vfat    umask=0077      0       1
#UUID=c7bb6d12-b247-4119-9c27-797f9995884e none            swap    sw              0       0
/swap/swapfile            swap            swap    sw              0       0

5. Expansion

If you need to uninstall this swap file, you can enter the created swap file directory. Execute the following commands.
sudo swapoff swapfile
If you need to keep this swap, you can write it to the /etc/fstab file.
/swap/swapfile /swap swap defaults 0 0

Guess you like

Origin blog.csdn.net/u010345983/article/details/134877147
Recommended