开发环境的虚拟机只分配了物理内存大小,物理内存有限且没有创建交换分区,系统总是杀进程导致服务挂掉。
Linux 可以创建 swap 交换分区,当内存不足时,把一部分磁盘空间虚拟成内存使用,在有限资源下提升系统效率。
Swap space
【译】Red Hat Enterprise Linux 7 Storage Administration Guide:CHAPTER 15. SWAP SPACE
Linux 中的交换空间在物理内存(RAM)已满时使用。如果系统需要更多内存资源且 RAM 已满,则会将内存中的非活动页移动到交换空间。
虽然交换空间可以帮助具有少量 RAM 的机器,但不应将其视为更多 RAM 的替代品。交换空间位于硬盘驱动器上,其访问时间比物理内存慢。
交换空间可以是专用交换分区(推荐)、交换文件或交换分区和交换文件的组合。请注意,Btrfs不支持交换空间。
在过去的几年里,推荐的交换空间量随着系统中的 RAM 量线性增加。但是,现代系统通常包含数百 GB 的 RAM。因此,推荐的交换空间被视为系统内存工作负载的函数,而不是系统内存。
然而,现代系统通常包括数百GB的RAM。因此,建议的交换空间被认为是系统内存工作负载的函数,而不是系统内存的函数。因此,推荐的交换空间被视为系统内存工作负载的函数,而不是系统内存。
表15.1“建议的系统交换空间”说明了交换分区的建议大小,具体取决于系统中的RAM数量以及是否需要足够的内存使系统休眠。建议的交换分区大小在安装过程中自动建立。但是,为了允许休眠,您需要在自定义分区阶段编辑交换空间。
表15.1“建议的系统交换空间”中的建议对于内存不足(1GB及以下)的系统尤其重要。未能在这些系统上分配足够的交换空间可能会导致不稳定等问题,甚至使已安装的系统无法转动。
Swap 推荐大小
官方:Recommended System Swap Space
Table 15.1. Recommended System Swap Space
Amount of RAM in the system 系统物理内存大小 |
Recommended swap space 推荐交换分区大小 |
Recommended swap space if allowing for hibernation 允许休眠时推荐交换分区大小 |
---|---|---|
⩽ 2 GB | 2 times the amount of RAM | 3 times the amount of RAM |
> 2 GB – 8 GB | Equal to the amount of RAM | 2 times the amount of RAM |
> 8 GB – 64 GB | At least 4 GB | 1.5 times the amount of RAM |
> 64 GB | At least 4 GB | Hibernation not recommended |
表 15.1,【推荐的系统交换空间】说明了交换分区的推荐大小,具体取决于系统中的 RAM 量以及您是否需要足够的内存供系统休眠。推荐的交换分区大小是在安装过程中自动建立的。但是,要允许休眠,您需要在自定义分区阶段编辑交换空间。
表 15.1,【推荐的系统交换空间】中的建议对于低内存(1 GB 或更少)的系统尤其重要。未能在这些系统上分配足够的交换空间可能会导致不稳定甚至导致已安装的系统无法启动等问题。
表15.1,【推荐的系统交换空间】中列出的每个范围之间的边界处,例如,具有2 GB、8 GB或64 GB系统RAM的系统,可以根据选择的交换空间和休眠支持进行判断。如果系统资源允许,增加交换空间可能会提高性能。
请注意,在多个存储设备上分配交换空间还可以提高交换空间性能,特别是在具有快速驱动器、控制器和接口的系统上。
创建Swap space
有时需要在安装后添加更多的交换空间。
例如,您可以将系统中的 RAM 量从 1 GB 升级到 2 GB,但只有 2 GB 的交换空间。 如果您执行内存密集型操作或运行需要大量内存的应用程序,将交换空间量增加到 4 GB 可能会更有利。
创建交换空间有三个选项:创建新的交换分区、创建新的交换文件或扩展现有 LVM2 逻辑卷上的交换空间。 建议您扩展现有的逻辑卷。
Extending Swap on an LVM2 Logical Volume
在现有的 LVM2 逻辑卷上扩展交换空间
默认情况下,红帽企业 Linux 7 在安装过程中使用所有可用空间。 如果您的系统是这种情况,那么您必须首先向交换空间使用的卷组添加一个新的物理卷。
向交换空间的卷组添加额外存储后,现在可以扩展它。 为此,请执行以下过程(假设 /dev/VolGroup00/LogVol01
是要扩展 2 GB 的卷):
Disable swapping for the associated logical volume:
# swapoff -v /dev/VolGroup00/LogVol01
Resize the LVM2 logical volume by 2 GB:
# lvresize /dev/VolGroup00/LogVol01 -L +2G
Format the new swap space:
# mkswap /dev/VolGroup00/LogVol01
Enable the extended logical volume:
# swapon -v /dev/VolGroup00/LogVol01
To test if the swap logical volume was successfully extended and activated, inspect active swap space:
$ cat /proc/swaps $ free -h
Creating an LVM2 Logical Volume for Swap
为交换空间创建 LVM2 逻辑卷。
要添加大小为 2 GB 的交换卷组,假设 /dev/VolGroup00/LogVol02
是要添加的交换卷:
Create the LVM2 logical volume of size 2 GB:
# lvcreate VolGroup00 -n LogVol02 -L 2G
Format the new swap space:
# mkswap /dev/VolGroup00/LogVol02
Add the following entry to the
/etc/fstab
file:/dev/VolGroup00/LogVol02 swap swap defaults 0 0
Regenerate mount units so that your system registers the new configuration:
# systemctl daemon-reload
Activate swap on the logical volume:
# swapon -v /dev/VolGroup00/LogVol02
To test if the swap logical volume was successfully created and activated, inspect active swap space:
$ cat /proc/swaps $ free -h
Creating a Swap File
创建交换文件。
Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
确定新交换文件的大小(以兆字节为单位)并乘以 1024 以确定块数。 例如,64 MB 交换文件的块大小为 65536。
Create an empty file:
# dd if=/dev/zero of=/swapfile bs=1024 count=65536
Replace count with the value equal to the desired block size.
Set up the swap file with the command:
# mkswap /swapfile
Change the security of the swap file so it is not world readable.
# chmod 0600 /swapfile
To enable the swap file at boot time, edit
/etc/fstab
as root to include the following entry:/swapfile swap swap defaults 0 0
The next time the system boots, it activates the new swap file.
Regenerate mount units so that your system registers the new
/etc/fstab
configuration:# systemctl daemon-reload
To activate the swap file immediately:
# swapon /swapfile
To test if the new swap file was successfully created and activated, inspect active swap space:
$ cat /proc/swaps $ free -h
Swap 使用权重
内核参数 vm.swappiness
控制换出运行时内存的相对权重,参数值大小对如何使用 swap 分区有很大联系。
值越大,表示越积极使用 swap 分区,越小表示越积极使用物理内存。
一般系统的默认值 swappiness=60,表示内存使用率超过 100-60=40% 时开始使用交换分区。
# 查看系统默认优先级
$ cat /proc/sys/vm/swappiness
30
swappiness = 0 表示最大限度使用物理内存,然后才是 swap 空间
对于 3.5 以后的内核和 RedHat 2.6.32 之后的内核,设置为 0 会禁止使用 swap,从而引发 out of memory,这种情况可以设置为 1。
swappiness=100 表示积极的使用swap分区,并且把内存上的数据及时的搬运到 swap。
临时设置优先级,重启系统后会恢复默认值
$ sudo sysctl vm.swappiness=10
永久设置生效
$ sudo vi /etc/sysctl.conf
#在文档的最后加上一行(永久修改 swappiness)
vm.swappiness = 10
$ sudo sysctl -p #使修改立即生效
Swap 系统自启动
将交换分区信息写入到
/etc/fstab
配置文件中/root/swap swap swap defaults 0 0
使用
swapon -a
命令更新状态
Swap 禁用
# 禁用所有 /proc/swaps
sudo swapoff -a
# 指定空间
sudo swapoff -v /swapfile
# 删除交换文件
sudo rm /swapfile
Swap 信息
free 查看
查看内存和交换空间
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 11G 7.2G 309M 25M 4.1G 4.0G
Swap: 8.0G 0B 8.0G
swapon 查看
[root@localhost ~]# swapon -s
文件名 类型 大小 已用 权限
/root/swap file 8388604 0 -1
swaps 文件
可以通过查看 swaps 文件来查看 swap 信息
[root@localhost ~]# cat /proc/swaps
Filename Type Size Used Priority
/root/swap file 8388604 0 -1
top 查看
top 命令输出的标题部分显示交换空间信息,以千字节为单位
[root@localhost ~]# top -H
top - 15:52:37 up 42 days, 39 min, 9 users, load average: 0.15, 0.12, 0.10
Threads: 1344 total, 1 running, 1343 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.7 us, 0.3 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 12138748 total, 180128 free, 7546512 used, 4412108 buff/cache
KiB Swap: 8388604 total, 8388604 free, 0 used. 4177832 avail Mem
vmstat 统计
[root@localhost ~]# vmstat -w
procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu--------
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 314088 0 4303824 0 0 1624 105 1 0 5 1 86 8 0
相关参考
注意:本文归作者所有,未经作者允许,不得转载