Linux Swap故障之 swapoff failed: Cannot allocate memory


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接受的參數是123,分別清空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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM