Linux expansion and deletion of running memory (swapfile swap file)

1 Add swapfile swap file

Swap (namely: swap partition) in Linux is similar to the virtual memory of Windows, that is, when the memory is insufficient, a part of the hard disk space is virtualized as memory for use, thereby solving the situation of insufficient memory capacity. Android is a Linux-based operating system, so you can also use the Swap partition to improve system operating efficiency.

Under normal circumstances, the Swap space should be greater than or equal to the size of the physical memory, and the minimum should not be less than 64M. Usually, the size of the Swap space should be 2-2.5 times the physical memory. The adjustment of Swap is crucial to the performance of Linux servers, especially Web servers. It is very important, by adjusting Swap, you can sometimes overcome system performance bottlenecks and save system upgrade costs.

The specific use is as follows:

# 查看当前的swap大小
free -m

# dd命令创建一个分区
dd if=/dev/zero of=/home/swap bs=1024 count=10485761
  1. if means input file, which means the input file. The input file here is /dev/zero, that is to say, when expanding, the content of /dev/zero is used for expansion. The content of /dev/zero here is generally binary data
  2. of means output file, which means the output file, which is the path of the file we want to expand, here is /home/swap
  3. bs means that 1 block = 1024 bytes is the expansion unit, that is, 1K is the basic unit expansion
  4. count indicates how many blocks to open, here is 1048576 and block, the size is: 1048576/1024/1024 = 1G, that is to say, the current expanded file is 1GB in size
# 进行格式化交换,将swap文件格式化成s文件系统
mkswap /home/swap

# 使扩容的空间有效
swapon /home/swap

# 将扩容的文件信息写入到 /etc/fstab 中
vi /etc/fstab
/home/swap swap swap defaults 0 0

使用 free -m 检查是否生效

2 Delete the swapfile swap file

2.1 Close the swapfile first:

$ sudo swapoff -v /swapfile

Check that the swapfile file is closed

$ swapon -s
Filename				Type		Size		Used		Priority
/dev/nvme0n1p6                          partition	8000508		0		-2

2.2 Delete the swapfile file

sudo rm /swapfile

2.3 Delete swapfile from /etc/fstab

The /etc/fstab file is set to automatically mount /swapfile as the swap space after booting, and the relevant entries need to be deleted. Enter the vim editor to delete swapfile from / etc/fstab

$ sudo vim /etc/fstab

 After entering, use the cursor to move the position, move to the line where the swapfile is located, and delete this line.

Related commands:

dd delete line; x delete backward; X delete forward;
:wq save and exit; :wq! force save and exit;
:q exit without saving; :q! force exit without saving; :
w save without exit; :e ! Discard all modifications
Add/delete/adjust swap partition or swap file in ubuntu22.04

Guess you like

Origin blog.csdn.net/qq_43445867/article/details/131546188