Linux关闭swap失败,swapoff failed: cannot allocate memory

栏目: 数据库 · 发布时间: 6年前

内容简介:有几台服务器内存充足,可用内存还有几十个G,swap分区被少量使用,执行Google了一番,网上主要给出了两种解决方法,分别是1)释放内存缓存,2)允许内存overcommit。

现象

有几台服务器内存充足,可用内存还有几十个G,swap分区被少量使用,执行 swapoff -a 的时候,报错:

$ swapoff -a -v
swapoff /swapfile
swapoff: /swapfile: swapoff failed: Cannot allocate memory

调查

Google了一番,网上主要给出了两种解决方法,分别是1)释放内存缓存,2)允许内存overcommit。

方法1:释放内存缓存后,重新执行swapoff -a

Unable to swapoff but enough physical memory is available 中提出了这种解决方法:

sync ; echo 3 > /proc/sys/vm/drop_caches

How to Clear RAM Memory Cache, Buffer and Swap Space on Linux 对此作做了更详细的说明, drop_caches 接受的参数是 123 ,分别清空pagecache、slab对象、pagecahce和slab对象。在 Documentation/sysctl/vm.txt 中可以找到说明:

To free pagecache:
    echo 1 > /proc/sys/vm/drop_caches

To free reclaimable slab objects (includes dentries and inodes):
    echo 2 > /proc/sys/vm/drop_caches

To free slab objects and pagecache:
    echo 3 > /proc/sys/vm/drop_caches

dirty 状态的内存缓存不会被释放,如果要释放尽可能多的内存缓存,先执行命令 sync ,减少dirty状态的内存缓存。如果要disable,输入参数 4 ,注意 0 不被接受:

echo 4 > /proc/sys/vm/drop_caches

释放了部分内存缓存之后,部分机器 等待一段时间 ,swapoff -a执行成功,有的机器还是不能成功,关停了几个服务之后,swapoff -a执行成功。

方法2:允许内存overcommit

这是后来看到的方法,没有试验,仅做记录。

当overcommit_memory == 2时,swapoff失败swap无法卸载 中使用的是这种方法:在 /etc/sysctl.conf 中设置 vm.overcommit_memory=0 ,执行sysctl -p更新内核参数。

Documentation/sysctl/vm.txt 中介绍了内核参数 overcommit_memory 的用途,它用来控制“用户空间申请内存时,是否进行内存容量判断(overcommit判断)以及是否批准:

When this flag is 0, the kernel attempts to estimate the amount
of free memory left when userspace requests more memory.

When this flag is 1, the kernel pretends there is always enough
memory until it actually runs out.

When this flag is 2, the kernel uses a "never overcommit"
policy that attempts to prevent any overcommit of memory.
Note that user_reserve_kbytes affects this policy.

设置的值为 2 表示不允许overcommit,这时候如果停掉swap,那么可用内存减少,用户空间的内存申请就可能触发overcommit被拒绝。但是事后查了一下我们集群中的机器,overcommit_memory的参数值本来就是0或1了,似乎和swapoff失败没啥关系。不过掌握了一组内核参数,也值了。

参考

  1. Unable to swapoff but enough physical memory is available
  2. How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
  3. Documentation/sysctl/vm.txt
  4. 当overcommit_memory == 2时,swapoff失败
  5. swap无法卸载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据库系统概念

数据库系统概念

Abraham Silberschatz、Henry F. Korth、S. Sudarshan / 杨冬青、马秀莉、唐世渭 / 机械工业 / 2006-10-01 / 69.50元

本书是数据库系统方面的经典教材之一。国际上许多著名大学包括斯坦福大学、耶鲁大学、得克萨斯大学、康奈尔大学、伊利诺伊大学、印度理工学院等都采用本书作为教科书。我国也有许多所大学采用本书以前版本的中文版作为本科生和研究生的数据库课程的教材和主要教学参考书,收到了良好的效果。 本书调整和新增内容:调整了第4版的讲授顺序。首先介绍SQL及其高级特性,使学生容易接受数据库设计的概念。新增数据库设计的专......一起来看看 《数据库系统概念》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具