当有足够的内存时,为什么我不能“交换”?


10

我最近注意到,即使我的系统上有很多内存:

# free -m
             total       used       free     shared    buffers     cached
Mem:         15965       9680       6284         35       1754       2560
-/+ buffers/cache:       5365      10599
Swap:         2047        258       1789

我无法swapoff -a使用2G交换文件

-rw-r----- 1 root root 2.0G Feb  9 21:34 /2GB.swap

我已经通过更改了以下内核参数,sysctl但这应该是原因:

vm.swappiness = 5
vm.vfs_cache_pressure = 200

任何原因:

# swapoff -a
swapoff: /2GB.swap: swapoff failed: Cannot allocate memory

?失败dmesg时不记录任何内容swapoff。我正在使用linux内核4.19.20-041920-generic


另一个例子包括 /proc/meminfo

# cat /proc/meminfo
MemTotal:       16348296 kB
MemFree:         6673788 kB
MemAvailable:   11233052 kB
Buffers:          525048 kB
Cached:          2837788 kB
SwapCached:       362556 kB
Active:          4728244 kB
Inactive:        2758260 kB
Active(anon):    3132940 kB
Inactive(anon):  1043676 kB
Active(file):    1595304 kB
Inactive(file):  1714584 kB
Unevictable:        2396 kB
Mlocked:            2396 kB
SwapTotal:       2097148 kB
SwapFree:        1124272 kB
Dirty:               336 kB
Writeback:             0 kB
AnonPages:       3786868 kB
Mapped:           699944 kB
Shmem:             53116 kB
Slab:            1770268 kB
SReclaimable:    1578564 kB
SUnreclaim:       191704 kB
KernelStack:       47216 kB
PageTables:        82968 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    10271296 kB
Committed_AS:   24712604 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
Percpu:             3552 kB
HardwareCorrupted:     0 kB
AnonHugePages:     26624 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      714480 kB
DirectMap2M:    13891584 kB
DirectMap1G:     3145728 kB

free -m

# free -m
             total       used       free     shared    buffers     cached
Mem:         15965       9447       6517         51        512       2771
-/+ buffers/cache:       6163       9801
Swap:         2047        950       1097

编辑

strace swapoff -a

root@MACHINE:~# strace swapoff -a
execve("/sbin/swapoff", ["swapoff", "-a"], [/* 22 vars */]) = 0
...
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=9469488, ...}) = 0
mmap(NULL, 9469488, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd262737000
close(3)                                = 0
open("/proc/swaps", O_RDONLY)           = 3
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd263a54000
read(3, "Filename\t\t\t\tType\t\tSize\tUsed\tPrio"..., 1024) = 102
readlink("/2GB.swap", 0x7ffcfbb3bea0, 4096) = -1 EINVAL (Invalid argument)
read(3, "", 1024)                       = 0
close(3)                                = 0
munmap(0x7fd263a54000, 4096)            = 0
swapoff("/2GB.swap")                    = -1 ENOMEM (Cannot allocate memory)
open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2570, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd263a54000
read(3, "# Locale name alias data base.\n#"..., 4096) = 2570
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7fd263a54000, 4096)            = 0
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
...
open("/usr/share/locale-langpack/en/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "swapoff: ", 9swapoff: )                = 9
write(2, "/2GB.swap: swapoff failed", 25/2GB.swap: swapoff failed) = 25
write(2, ": ", 2: )                       = 2
open("/usr/share/locale/en_US.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
...
open("/usr/share/locale-langpack/en/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "Cannot allocate memory\n", 23Cannot allocate memory
) = 23
open("/etc/fstab", O_RDONLY|O_CLOEXEC)  = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=838, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd263a54000
read(3, "# /etc/fstab: static file system"..., 4096) = 838
readlink("/2GB.swap", 0x7ffcfbb3c2b0, 4096) = -1 EINVAL (Invalid argument)
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7fd263a54000, 4096)            = 0
exit_group(-1)                          = ?
+++ exited with 255 +++

如果您的系统正在交换,则删除交换分区将无法解决任何问题。您只会造成其他问题(顺便说一句,最好先使用分区,再使用文件)。您可能需要查找程序没有将数据交换到该文件中的瞬间。也许您应该停止此类程序。注意:内存碎片可能是原因。尝试也许也花一些时间sync来减少脏页。
Giacomo Catenazzi

1
@GiacomoCatenazzi 1)为什么使用分区比使用文件更好?2)sync不会改变内存利用率一位
Patryk

1
sync会将脏页写入磁盘(不是全部,但是当这些页应该在磁盘上时)。这样可以使此类页面整洁,因此可以轻松丢弃它们(无需交换或写入磁盘),因此内核可以快速将部分内存从缓存转换为空闲。这是一个非常短期的技巧,可以缩短关键阶段(交换/卸载)。
Giacomo Catenazzi

2
分区直接控制内核(单个连续块,对齐)。文件:存在没有足够连续空间或不受其他系统实用程序干扰的风险。[并且理想的根文件系统应该是只读的]。[并且通常有一个临时分区,恢复系统或进行某种“侵入式”系统处理非常有用,尤其是在远程计算机或没有易启动设备的计算机上)。这不是一个硬性要求,但是我发现通常有一个专用分区会比较容易[对于RAID机器,您可以取消RAID交换,以提高速度]
Giacomo Catenazzi

1
问题仍然存在。还有很多可用RAM,超过交换文件的TOTAL大小。为什么不能禁用交换?
Paul Stelian

Answers:



0

如果您的交换文件是在/etc/fstab(而不是在systemd-swap中)定义的,则只需删除或注释掉其中的行,/etc/fstab然后重新启动即可。

如果您正在使用systemd-swap配置交换,请swapfc_enabled=0在一Swap File Chunked节中进行设置/etc/systemd/swap.conf并重新引导。


我认为没有交换文件就可以启动系统的事实并不能真正回答为什么交换文件(如果使用)无法关闭的问题。
ilkkachu

如果可以忍受,重新启动是一个简单的解决方案
Daniel Farrell
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.