Linux的swap空间需要设置多大?怎么释放SWAP

本文探讨了Linux系统的swap空间设置原则,根据Redhat和Ubuntu的官方建议,给出了不同内存大小下swap的合理配置。同时,介绍了如何在系统内存被缓存占用导致使用swap时,通过调整vm.swappiness参数和执行特定命令来释放swap空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

无论是windows系统还是linux系统,除了物理内存外,都还有一个虚拟内存。在linux上,虚拟内存被称为swap space。过去以来,虚拟内存的大小应该是物理内存的两倍,但是最近几年来,物理内存的大小至少都有了好几个GB,如果16G内存用32G的swap岂不是太占用硬盘空间?

下图是虚拟内存和交换空间的映射关系,虚拟内存的存在,可以提高电脑的运行速度,所以其存在很有意义。

这里写图片描述

我们看看redhat的官方答复是怎么说的:

Amount of RAM in the system 物理内存Recommended swap space建议的交换空间大小Recommended swap space if allowing for hibernation如果开启休眠功能建议的交换空间大小
⩽ 2GB2 times the amount of RAM3 times the amount of RAM
> 2GB – 8GBEqual to the amount of RAM2 times the amount of RAM
> 8GB – 64GBAt least 4 GB1.5 times the amount of RAM
> 64GBAt least 4 GBHibernation not recommended

总结起来就是,如果不打算开启休眠功能,物理内存在8G以下,则swap设置为与物理内存一样大。如果物理内存在8G以上,swap空间设置为8G即可。当物理内存大于64G时,不建议开启休眠功能。

以下是Ubuntu的指南,更加细致:(从左至右依次是RAM大小,不开启休眠,开启休眠,最大值)

    RAM(MB) No hibernation With Hibernation Maximum
     256     256              512               512
     512     512             1024              1024
    1024    1024             2048              2048

    RAM(GB) No hibernation  With Hibernation  Maximum
      1      1                2                   2
      2      1                3                   4
      3      2                5                   6
      4      2                6                   8
      5      2                7                  10
      6      2                8                  12
      8      3               11                  16
     12      3               15                  24
     16      4               20                  32
     24      5               29                  48
     32      6               38                  64
     64      8               72                 128
    128     11              139                 256

 

 

 

释放linux的swap内存


如上图,一般情况下不会用到swap的,一般物理内存使用在90%以上(默认是这个数,可以自行在/etc/sysctl.conf里设置vm.swappiness参数),但有的时候,内存会被缓存占用,导致系统开始使用swap空间,此时就需要清理下swap了 
这里先说下vm.swappiness参数,设置成vm.swappiness=10:就是说当内存使用90%以上才会使用swap空间

第一步:先执行sync命令

#sync
1
sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息,以防止释放,sync命令则可用来强制将内存缓冲区中的数据立即写入磁盘中。 
第二步:(如果仅仅是清理swap的话,这一步可以不执行)

#echo 3 > /proc/sys/vm/drop_caches
1
此指令输入立即生效,意在释放所有缓存。 
关于drop_caches: 
drop_caches的详细文档如下: 
Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free. 
To free pagecache: 
* echo 1 > /proc/sys/vm/drop_caches 
To free dentries and inodes: 
* echo 2 > /proc/sys/vm/drop_caches 
To free pagecache, dentries and inodes: 
* echo 3 > /proc/sys/vm/drop_caches 
As this is a non-destructive operation, and dirty objects are notfreeable, the user should run “sync” first in order to make sure allcached objects are freed. 
This tunable was added in 2.6.16. 
**echo 1:释放页面缓存 
echo 2:释放目录文件和inodes 
echo 3:释放所有缓存(页面缓存,目录文件和inodes)** 
如下图是执行完echo3 的cache的对比: 


第三步:关闭swap,再开户swap

#swapoff -a
#swapon -a
1
2
 
现在看swap的used的那一项为零了,说明已经清空
 

参考链接如下: 

https://blog.youkuaiyun.com/chenghuikai/article/details/77476830 
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/installation_guide/s2-diskpartrecommend-ppc#id4394007

https://help.ubuntu.com/community/SwapFaq

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值