Linux - Swap 篇

什么是 Swap

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space

即 Swap 是当物理内存是满的时候,会被使用。当系统需要更多的内存资源,但是内存却满了,这时候不活跃的页会被移动到 Swap 空间。

建议 Swap 使用单独的分区:

  • a swap file
  • a combination of swap partitions and swap files.

Swap 大小的计算公式:

M 等于物理内存大小(GB),S 等于 Swap 大小(GB)

当物理内存小于 2 GB 的时候,Swap 等于物理内存的 2 倍。当物理内存大于 2 GB 的时候,Swap 等于物理内存 + 2 GB

If M < 2
    S = M *2
Else
    S = M + 2

查看 Swap 的方式

查看总体的使用 Swap 的情况:

free -m

查看是哪个进程使用了 Swap:

使用 top 命令,然后再按 f,再按 p ,即可显示哪个进程使用了多少 Swap 空间。然后要释放的话,把那个进程重起下即可。

查看 Swap 使用的哪个分区使用 /sbin/swapon -s 命令。

参考资料

    原文作者:yexiaobai
    原文地址: https://segmentfault.com/a/1190000000702921
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞