目录
swap分区关闭
方法1:释放内存缓存
方法2:允许内存overcommit

swap分区关闭
准备调整Linux下的swap分区的使用率。
在Linux下执行 swapoff -a -v报如下错误:
swapoff: /dev/mapper/cryptswap1: swapoff failed: Cannot allocate memory

上述错误原因分析:
  从上述的信息可以看出,当前Linux系统把/dev/mapper/cryptswap1这个设备当做了交换分区,如果当前改交换分区使用的容量大于系统当前剩余的内存,就会报这个错误,因为在关闭交换分区的时候,需要把分区的数据全部写入到内存,如果内存容量不足,就会导致这个错误。

解决:
方法1:释放内存缓存
# sync ; echo 3 > /proc/sys/vm/drop_caches #先把内存数据回写到磁盘,然后释放内存缓存
drop_caches接受的参数是1、2、3,分别清空pagecache、slab对象、pagecahce和slab对象

从 https://github.com/torvalds/linux/blob/master/Documentation/sysctl/vm.txt 可以找到对该文件参数的解释:
drop_caches

Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes. Once dropped, their
memory becomes free.

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不被接受:
上述方法可能不会成功,当你的swap分区使用太多的时候。

方法2:允许内存overcommit
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被拒绝。

参考
https://www.lijiaocn.com/%E9%97%AE%E9%A2%98/2019/02/27/linux-swap-off-fail.html

Linux Swap故障之 swapoff failed: Cannot allocate memory的更多相关文章

  1. 关于阿里云ESC上go语言项目编译6l: running gcc failed: Cannot allocate memory

    (1)前段时间将自己的阿里云服务器上的系统由centos 6.5换为了ubuntu 14,其他的硬件配置都没有发生改变,将服务器上的数据恢复并且重新安装了golang的编译环境后,发现使用go bui ...

  2. mprotect() failed: Cannot allocate memory

    遇到这个问题是在測试项目的性能时发现的,每一个对象分配一页大小的空间然后mprotect() 保护起来,当系统分配3W多个页的时候会出现这个问题. google到在一份邮件列表中也曾提到该问题.htt ...

  3. 解决Failed to allocate memory: 8转

    解决Failed to allocate memory: 8 昨天换了x64的Win7,发现在Eclipse上启动模拟器的时候存在问题,当设置的模拟器分辨率大于400×800的时候会出现 Failed ...

  4. Android 启动模拟器是出现“Failed to allocate memory: 8”错误提示的原因及解决办法

    某天,Android 启动模拟器是出现“Failed to allocate memory: 8”错误,模拟器无法启动,如下图: 原因:设置了不正确AVD显示屏模式,4.0版默认的模式为WVGA800 ...

  5. Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 (转)

    Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8http://www.crifan.com/android_emulator_ ...

  6. Failed to allocate memory: 8

    Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual ...

  7. 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8

    [问题] 折腾: [已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 过程中,增大对应AVD的内存为2G后,结果无法启 ...

  8. fork failed.: Cannot allocate memory

    在做压力测试时候: [root@666 ok]# webbench -c 5000 -t30 http://10.100.0.61/ Webbench - Simple Web Benchmark 1 ...

  9. 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory

    [已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 结论是: 当前有个bug: 默认是通过 hw.ramSize=1024 ...

随机推荐

  1. db服务器参数优化

    1.swap分区 swap作用是在系统内存不够的情况下,当做临时的内存使用. swap是在硬盘上,性能肯定没有再内存好,当系统内存使用超过40%的时候, swap会可能被使用,而系统一旦使用swap会 ...

  2. Centos查看虚拟机IP地址及使用XShell连接

    1.在VMware中安装Centos7系统[1] 2.查看虚拟机里的Centos7的IP[2] 1)查看IP 输入ip查询命名 ip addr 发现 ens33 没有 inet 这个属性,那么就没法通 ...

  3. 记一次Pr中视频蜜汁卡顿往复和解决方法

    目录 问题 换素材的起因 灵异素材 无端联想 解决 问题 换素材的起因 本来视频剪了一晚剪完了,导出一看,好家伙,糊到上世纪.原来素材的像素大小都没法看,这视频素材我是从别人U盘拷过来的,可他竟然是用 ...

  4. Elasticsearch详解

    Elasticsearch详解 Chandler_珏瑜 关注  5.8 2019.05.05 17:19* 字数 10971 阅读 1147评论 5喜欢 36 5.1 Lucene简介  Lucene ...

  5. Linux搭建Nexus3.X构建maven私服

    备注:linux版本: ubuntu 同时已经部署好JDK8环境 一.linux安装nexus 1.创建文件夹并进入该目录 cd /usr/local && mkdir nexus & ...

  6. 命名法:骆驼(Camel)、帕斯卡(pascal)、匈牙利(Hungarian)、下划线(_)

    首先欢迎大家到来! 常用的命名法:骆驼(Camel).帕斯卡(pascal).匈牙利(Hungarian).下划线(_) 骆驼:是指混合使用大小写字母来构成变量和函数的名字 帕斯卡:与骆驼命名法类似只 ...

  7. PHP微信发红包简明教程

    PHP微信发红包简明教程1首先进入公众号申请微信支付 申请成功账号密码会发到你指定的邮箱 是登陆商户平台的2 进入后申请发红包借口3 调用发红包接口 https://api.mch.weixin.qq ...

  8. C++之救济金发放问题

    n(n<20)个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上).接 ...

  9. Java | Spring Boot Swagger2 集成REST ful API 生成接口文档

      Spring Boot Swagger2 集成REST ful API 生成接口文档 原文 简介 由于Spring Boot 的特性,用来开发 REST ful 变得非常容易,并且结合 Swagg ...

  10. docker系列之三:docker实际应用

    以Docker为基础完成持续集成.自动交付.自动部署: 原理: RD推送代码到git 仓库或者svn等代码服务器上面,git服务器就会通过hook通知jenkins. jenkine 克隆git代码到 ...