以2核1.5G,60G系统盘+40G数据盘为例。发现虚拟内存只有1G

root@hlvps:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.3Gi       298Mi       1.1Gi       3.5Mi        92Mi       1.0Gi
Swap:          974Mi          0B       974Mi
root@hlvps:~# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/sdb5 partition 975M   0B   -2
root@hlvps:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
fd0      2:0    1    4K  0 disk 
sda      8:0    0   40G  0 disk 
`-sda1   8:1    0   40G  0 part 
sdb      8:16   0   60G  0 disk 
|-sdb1   8:17   0   59G  0 part /
|-sdb2   8:18   0    1K  0 part 
`-sdb5   8:21   0  975M  0 part [SWAP]
sr0     11:0    1 1024M  0 rom  

1、增加虚拟内存到2G: 这里使用的是增加,而不是删除旧的 Swap再创建新的。因为根据lsblk 信息,可见Swap 目前是一个物理磁盘分区 (/dev/sdb5),而不是文件。 

如果采取【删除旧的 Swap再创建新的】这种方案,请再次请教AI。

1️⃣ 创建 1GB swap 文件

fallocate -l 1G /swapfile

2️⃣ 设置权限

chmod 600 /swapfile

3️⃣ 格式化为 swap

mkswap /swapfile

4️⃣ 启用 swap

swapon /swapfile

5️⃣ 验证

swapon --show
free -h

6️⃣ 开机自动挂载

echo '/swapfile none swap sw 0 0' >> /etc/fstab

⚙️ 优化(非常关键)

echo "vm.swappiness=10" >> /etc/sysctl.conf
sysctl -p

Linux 内核的 swappiness 参数(取值 0-100)决定了系统“有多积极”地把物理内存里的数据搬到 Swap 中:

  • 默认值 60: 系统在物理内存还有较多剩余时,就会开始把一些不常用的数据往 Swap 搬。这在内存大的机器上没问题,但在 1.5G 的机器上,频繁的硬盘 I/O 会让系统产生瞬时卡顿。

  • 设置 10: 告诉系统“尽量先用物理内存,直到物理内存真的非常紧张了(通常剩余 10% 左右),再开始动用 Swap”。

root@VM-0-2-debian:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       372Mi       1.4Gi       2.0Mi       335Mi       1.6Gi
Swap:             0B          0B          0B
root@VM-0-2-debian:~# swapon --show
root@VM-0-2-debian:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1  117M  0 rom  
vda    254:0    0   40G  0 disk 
└─vda1 254:1    0   40G  0 part /
root@VM-0-2-debian:~# fallocate -l 4G /swapfile
root@VM-0-2-debian:~# chmod 600 /swapfile
root@VM-0-2-debian:~# mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=08f1e983-f3a4-4c93-80fc-51127a260dfb
root@VM-0-2-debian:~# swapon /swapfile
root@VM-0-2-debian:~# swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   4G   0B   -2
root@VM-0-2-debian:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       402Mi       1.3Gi       2.0Mi       382Mi       1.5Gi
Swap:          4.0Gi          0B       4.0Gi
root@VM-0-2-debian:~# echo '/swapfile none swap sw 0 0' >> /etc/fstab
root@VM-0-2-debian:~# sysctl vm.swappiness=10
vm.swappiness = 10
root@VM-0-2-debian:~# echo 'vm.swappiness=10' >> /etc/sysctl.conf
root@VM-0-2-debian:~# cat /proc/sys/vm/swappiness
10

4G虚拟内存案例:

连接主机...
连接主机成功
Linux VM-4-15-debian 6.1.0-47-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.170-3 (2026-05-08) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jun 21 01:45:56 2026 from 120.235.9.71
root@VM-4-15-debian:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       382Mi       1.4Gi       2.0Mi       341Mi       1.5Gi
Swap:             0B          0B          0B
root@VM-4-15-debian:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1  117M  0 rom  
vda    254:0    0   40G  0 disk 
└─vda1 254:1    0   40G  0 part /
root@VM-4-15-debian:~# swapon --show
root@VM-4-15-debian:~# fallocate -l 4G /swapfile
root@VM-4-15-debian:~# chmod 600 /swapfile
root@VM-4-15-debian:~# mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=02ca540d-6b67-4cec-9ee5-8dc6fa6f602a
root@VM-4-15-debian:~# swapon /swapfile
root@VM-4-15-debian:~# swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   4G   0B   -2
root@VM-4-15-debian:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       389Mi       1.4Gi       2.0Mi       344Mi       1.5Gi
Swap:          4.0Gi          0B       4.0Gi
root@VM-4-15-debian:~# echo '/swapfile none swap sw 0 0' >> /etc/fstab
root@VM-4-15-debian:~# sysctl vm.swappiness=10
vm.swappiness = 10
root@VM-4-15-debian:~# echo "vm.swappiness=10" >> /etc/sysctl.conf
root@VM-4-15-debian:~# sysctl -p

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐