你是否须要SWAP分区呢?

1、查看Linux系统当前的分区状况:浏览器

>free -mcookie

简单来讲,SWAP 交换分区是用来存放内存溢出来的数据。咱们能够将内存想像成一个盆子,运行程序时的数据都被做为水放在内存这个盆子当中。若是你运行太多的应用程序,致使水过多而(内存这个)盆子已经装不下时,就会将多于部分的水装到 SWAP 交换分区当中。app


从理论上讲,SWAP 交换分区增大了可使用的内存空间,但事实并不是如此。由于内存的数据交换速度是很是快的,而 SWAP 交换分区是把物理磁盘当成内存的载体,也就是说:虽然使用 SWAP 可让咱们运行更多或更大的应用程序了,但物理磁盘的数据交换速度远比不上物理内存,所以也就可能形成运行性能的降低。而对硬盘特别是 SSD 磁盘的频繁读写,也会形成磁盘寿命的缩短和性能降低。tcp


其实,咱们能够把 SWAP 交换分区比喻成 Windows 中的 pagefile 页面文件,但二者在技术实现上仍有许多差别。性能


SWAP优先级spa

SWAP 交换分区会将内存中较少使用的数据移到磁盘当中,从而将更多的内存空间腾出来给其它的重要应用,也就是说:内存中较少使用的数据会被移动 SWAP 分区。debug


“较少使用”其实取决于“swappiness”阈值,在 Linux 中实际上是一个可配置项。较高在 swappiness 值意味着数据更有可能被移到 SWAP 分区,较低的 swappiness 表示数据不太可能被移到 SWAP 分区。ip


SWAP与Linux休眠内存

最后还有一点须要注意:Linux 系统休眠的数据是存储到 SWAP 交换分区中的,也就是说若是系统没有配置 SWAP 交换分区,在 Linux 上是没法进行休眠的。it


你是否须要SWAP分区呢?


你有必要为本身的 Linux 划一个 SWAP 分区吗?SWAP 交换分区对你来讲是必要的吗?

绝对不是! Linux 系统不配置 SWAP 也能够运行得很好,是否须要划分 SWAP 分区也有以下几个优缺点:


优势:内存用完时,提供更多运行空间

能够休眠


缺点:SWAP对磁盘的占用设定好以后是固定的,没法动态调整

增长了磁盘的读写次数和损耗概率

不必定提升性能(详细解释见下文)


SWAP起副作用的状况

开启 SWAP 交换分区也并不是问题能提高 Linux 性能,在某些状况下使用 SWAP 可能比不使用更糟。

若是你在一台1GB内存和5400转硬盘的上网本上安装了 Linux,只须要多打开几个浏览器标签并多开几个应用就会形成内存占完而开始使用 SWAP 交换分区。因为硬盘只有5400转,随后便开始出现性能瓶颈。这是由于硬盘转速太慢,而又有大量的数据须要访问到 SWAP 交换分区,因此会形成机器愈来愈慢。在此时,咱们一般须要重启系统来彻底清除 SWAP 的内容。


关于SWAP的建议

  • 若是你须要 Linux 休眠功能,那么固然就要划分 SWAP 分区。该分区通常为你内存大小外加 10-25%

  • 若是你的内存小于4GB,而且但愿获得小的性能提高(至少7200转磁盘),能够划分一个交换分区

  • 若是你的磁盘转速小于7200转,建议不要划分 SWAP 交换分区


更改Swappiness

咱们能够直接在 /etc/sysctl.conf 文件中对 swappiness 值进行编辑,在该文件中找到 vm.swappiness 并对其值进行更改(最好是10),若是你没找到这个参数,能够直接在文件末尾添加:

# vm.swappiness=10

该值主要表示咱们但愿 Linux 从内存与 SWAP 分区进行数据交换的强度,值为 10 则表示当内存占用到 90% 时开始将数据写入 SWAP。Ubuntu 的默认 swappiness 值为 60,这表示当内存占用到 40% 时开始将数据写入 SWAP。


[root@AY140716161543837722Z etc]# cat sysctl.conf

# Kernel sysctl configuration file for Red Hat Linux

#

# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and

# sysctl.conf(5) for more details.


# Controls IP packet forwarding

net.ipv4.ip_forward = 0


# Controls source route verification

net.ipv4.conf.all.rp_filter=0

net.ipv4.conf.default.rp_filter=0


# Do not accept source routing

net.ipv4.conf.default.accept_source_route = 0


# Controls the System Request debugging functionality of the kernel

kernel.sysrq = 0


# Controls whether core dumps will append the PID to the core filename.

# Useful for debugging multi-threaded applications.

kernel.core_uses_pid = 1


# Controls the use of TCP syncookies

net.ipv4.tcp_syncookies = 1


# Disable netfilter on bridges.

net.bridge.bridge-nf-call-ip6tables = 0

net.bridge.bridge-nf-call-iptables = 0

net.bridge.bridge-nf-call-arptables = 0


# Controls the default maxmimum size of a mesage queue

kernel.msgmnb = 65536


# Controls the maximum size of a message, in bytes

kernel.msgmax = 65536


# Controls the maximum shared segment size, in bytes

kernel.shmmax = 68719476736


# Controls the maximum number of shared memory segments, in pages

kernel.shmall = 4294967296


# Disable IPv6

net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1


# Determines how often to check for stale neighbor entries.

net.ipv4.neigh.default.gc_stale_time=120


# Using arp_announce/arp_ignore to solve the ARP Problem

net.ipv4.conf.default.arp_announce = 2

net.ipv4.conf.all.arp_announce=2

vm.swappiness = 0

net.ipv4.tcp_max_tw_buckets = 5000

net.ipv4.tcp_max_syn_backlog = 1024

net.ipv4.tcp_synack_retries = 2

net.ipv4.conf.lo.arp_announce=2

[root@AY140716161543837722Z etc]#

相关文章
相关标签/搜索