# Step 1: 一次行分配8G的文件
fallocate -l 8G swapfile
# Step 2: 设置权限,可读可写
sudo chmod 600 swapfile
# Step 3: 设置交换区
sudo mkswap swapfile
# Step 4: 启用交换区
sudo swapon swapfile
# 不用时,关闭交换区
# sudo swapoff swapfile
# 关闭后就可以进行删除
# sudo rm swap file
使用前
(base) cmf@cmf-pc:~$ free -h
total used free shared buff/cache available
Mem: 7.7G 6.2G 295M 497M 1.2G 718M
Swap: 2.0G 128M 1.9G
使用后
(base) cmf@cmf-pc:~$ free -h
total used free shared buff/cache available
Mem: 7.7G 6.2G 295M 495M 1.2G 716M
Swap: 9G 128M 9.9G