Linux SWAP 交换分区配置说明

一.SWAP 说明

1.1 SWAP 概述

当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。

这个是SWAP 交换分区的作用。实际上,我们更关注的应该是SWAP分区的大小问题。 设置多大才是最优的。

一般来说可以按照如下规则设置swap大小:

4G以内的物理内存,SWAP 设置为内存的2倍。

4-8G的物理内存,SWAP 等于内存大小。

8-64G 的物理内存,SWAP 设置为8G。

64-256G物理内存,SWAP 设置为16G。

实际上,系统中交换分区的大小并不取决于物理内存的量,而是取决于系统中内存的负荷,所以在安装系统时要根据具体的业务来设置SWAP的值。

1.2 系统在什么情况下才会使用SWAP?

实际上,并不是等所有的物理内存都消耗完毕之后,才去使用swap的空间,什么时候使用是由swappiness 参数值控制。

[root@rhce ~]# cat /proc/sys/vm/swappiness

60

该值默认值是60.

swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,

swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。

现在服务器的内存动不动就是上百G,所以我们可以把这个参数值设置的低一些,让操作系统尽可能的使用物理内存,降低系统对swap的使用,从而提高系统的性能。

1.3 如何修改swappiness参数?

–临时性修改:

[root@rhce ~]# sysctl vm.swappiness=10

vm.swappiness = 10

[root@rhce ~]# cat /proc/sys/vm/swappiness

10

这里我们的修改已经生效,但是如果我们重启了系统,又会变成60.

–永久修改:

在/etc/sysctl.conf 文件里添加如下参数:

vm.swappiness=10

或者:

[root@rhce ~]# echo ‘vm.swappiness=10’>> /etc/sysctl.conf

保存,重启,就生效了。

14 Responses so far.

  1. quest bars says:
    Hello, Neat post. There is an issue together with your site in web
    explorer, would check this? IE nonetheless is the market leader
    and a huge component to other folks will pass over your wonderful
    writing because of this problem.
  2. quest bars says:
    Thanks for sharing your thoughts on quest bars. Regards
  3. I pay a quick visit each day a few sites and blogs to read posts, but this
    blog provides quality based content.
  4. I love what you guys are up too. This kind of clever work and reporting!

    Keep up the good works guys I’ve you guys to my personal blogroll.

  5. It’s appropriate time to make some plans for the long run and it’s time to be happy.
    I’ve learn this submit and if I may just I want to recommend
    you few interesting things or suggestions. Maybe you could write
    subsequent articles relating to this article. I want to learn more issues about it!
  6. I have fun with, result in I found just what I used to be
    taking a look for. You have ended my four day long hunt!
    God Bless you man. Have a great day. Bye
  7. What’s up i am kavin, its my first time to commenting anyplace, when i read this piece of writing
    i thought i could also make comment due to this brilliant post.
  8. Hey! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?
  9. Thanks to my father who told me regarding this webpage, this
    webpage is actually remarkable.
  10. I am regular visitor, how are you everybody? This article posted at this web page is in fact pleasant.
  11. Thank you for the auspicious writeup. It in fact was a amusement account it.

    Look advanced to more added agreeable from you! By the way, how could
    we communicate?

  12. It’s awesome for me to have a website, which is valuable in support
    of my know-how. thanks admin
  13. I must thank you for the efforts you have put in penning this blog.

    I’m hoping to see the same high-grade blog posts from you in the future
    as well. In truth, your creative writing abilities has encouraged me to get my very own site now 😉

  14. Howdy this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get advice from someone with experience. Any help would be greatly appreciated!

LEAVE A COMMENT