Swap交换分区概念php
Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.html
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. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.linux
Linux内核为了提升读写效率与速度,会将文件在内存中进行缓存,这部份内存就是Cache Memory(缓存内存)。即便你的程序运行结束后,Cache Memory也不会自动释放。这就会致使你在Linux系统中程序频繁读写文件后,你会发现可用物理内存变少。当系统的物理内存不够用的时候,就须要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操做的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap分区中恢复保存的数据到内存中。这样,系统老是在物理内存不够时,才进行Swap交换。算法
查看Swap分区大小缓存
查看Swap分区的大小以及使用状况,通常使用free命令(经常使用free -m或free -g)便可服务器
以下:Swap总大小为2015M,已使用0,未使用2015Mapp
[root@DB-Server ~]# free -m total used free shared buffers cached Mem: 1000 855 145 0 28 296 -/+ buffers/cache: 530 470 Swap: 2015 0 2015
还可使用swapon命令查看当前swap相关信息:dom
例如swap空间是swap partition,Swap size,使用状况等详细信息ide
[root@DB-Server ~]# swapon -s Filename Type Size Used Priority /dev/sda3 partition 2064344 0 -1 [root@DB-Server ~]# cat /proc/swaps Filename Type Size Used Priority /dev/sda3 partition 2064344 0 -1
Swap分区大小设置性能
系统的Swap分区大小设置多大才是最优呢?
有个参考标准,具体还应该根据系统实际状况和内存的负荷综合考虑,像ORACLE的官方文档就推荐以下设置,这个是根据物理内存来作参考的。
RAM Swap Space Up to 512 MB 2 times the size of RAM Between 1024 MB and 2048 MB 1.5 times the size of RAM Between 2049 MB and 8192 MB Equal to the size of RAM More than 8192 MB 0.75 times the size of RAM
另外,野史记载(无从考证),能够做为一个参考。
4G之内的物理内存,SWAP 设置为内存的2倍。
4-8G的物理内存,SWAP 等于内存大小。
8-64G 的物理内存,SWAP 设置为8G。
64-256G物理内存,SWAP 设置为16G。
注意:若是内存不足,应该及时升级内存,不要尝试经过增长swap来解决内存不足的问题,问题根源是因为物理内存不足,再多的swap也是于事无补的。
就好像你的电脑,你用512M内存,你占用内存却要1G,就算你硬盘分配再多的虚拟内存又能如何。你只能是听到嘎吱嘎吱的硬盘读写声,系统依旧慢的要死。
就算是SSD硬盘来充当内存,速度也是要慢不少。SSD当内存的话,这块SSD则会变得至关短命。
这个时候你要作的就是升级内存,而不是想着用硬盘空间划分虚拟内存来解决。
释放Swap分区空间
[root@testlnx ~]# free -m total used free shared buffers cached Mem: 64556 55368 9188 0 926 51405 -/+ buffers/cache: 3036 61520 Swap: 65535 13 65522 [root@testlnx ~]# swapon -s Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol01 partition 67108856 14204 -1
使用swapoff关闭交换分区
[root@testlnx ~]# swapoff /dev/mapper/VolGroup00-LogVol01
使用swapon启用交换分区,此时查看交换分区的使用状况,你会发现used为0了
[root@testlnx ~]# swapon /dev/mapper/VolGroup00-LogVol01 [root@testlnx ~]# free -m total used free shared buffers cached Mem: 64556 55385 9171 0 926 51406 -/+ buffers/cache: 3052 61504 Swap: 65535 0 65535
Swap分区空间何时使用
系统在什么状况或条件下才会使用Swap分区的空间呢?
实际上是Linux经过一个参数swappiness来控制的。固然还涉及到复杂的算法。
这个参数值可为 0-100,控制系统 swap 的使用程度。
高数值可优先系统性能,在进程不活跃时主动将其转换出物理内存。低数值可优先互动性并尽可能避免将进程转换处物理内存,并下降反应延迟。默认值为 60。
注意:这个只是一个权值,不是一个百分比值,涉及到系统内核复杂的算法。
关于该参数请参考这篇文章[转载]调整虚拟内存,在此不作过多赘述。下面是关于swappiness的相关资料
The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."
Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.
有两种临时修改swappiness参数的方法,系统重启后失效
方法1:
[root@DB-Server ~]# more /proc/sys/vm/swappiness 60 [root@DB-Server ~]# echo 10 > /proc/sys/vm/swappiness [root@DB-Server ~]# more /proc/sys/vm/swappiness 10
方法2
[root@DB-Server ~]#sysctl vm.swappiness=10
永久修改swappiness参数的方法就是在配置文件/etc/sysctl.conf里面修改vm.swappiness的值,而后重启系统
echo 'vm.swappiness=10' >>/etc/sysctl.conf
若是有人会问是否物理内存使用到某个百分比后才会使用Swap交换空间,能够明确的告诉你不是这样一个算法.
有时即便物理内存只剩下8M了,可是依然没有使用Swap交换空间,而另一个例子,物理内存还剩下19G,竟然用了一点点Swap交换空间。
另外调整/proc/sys/vm/swappiness这个参数,若是你没有绝对把握,就不要随便调整这个内核参数,这个参数符合大多数状况下的一个最优值。
Swap交换分区对性能的影响
咱们知道Linux可使用文件系统中的一个常规文件或独立分区做为Swap交换空间,相对而言,交换分区要快一些。
可是和RAM比较而言,Swap交换分区的性能依然比不上物理内存,目前的服务器上RAM基本上都至关充足,那么是否能够考虑抛弃Swap交换分区,是否不须要保留Swap交换分区呢?
这个实际上是个人疑问之一。在这篇What Is a Linux SWAP Partition, And What Does It Do?博客中,做者给出了swap交换空间的优劣
Advantages:
Disadvantages:
其实保留swap分区归纳起来能够从下面来看:
首先,当物理内存不足以支撑系统和应用程序(进程)的运做时,这个Swap交换分区能够用做临时存放使用率不高的内存分页,把腾出的内存交给急需的应用程序(进程)使用。
有点相似机房的UPS系统,虽然正常状况下不须要使用,可是异常状况下, Swap交换分区仍是会发挥其关键做用。
其次,即便你的服务器拥有足够多的物理内存,也有一些程序会在它们初始化时残留的极少再用到的内存分页内容转移到 swap 空间,以此让出物理内存空间。
对于有发生内存泄漏概率的应用程序(进程),Swap交换分区更是重要,由于谁也不想看到因为物理内存不足致使系统崩溃。
最后,如今不少我的用户在使用Linux,有些甚至是PC的虚拟机上跑Linux系统,此时可能经常使用到休眠(Hibernate),这种状况下也是推荐划分Swap交换分区的。
其实少许使用Swap交换空间是不会影响性能,只有当RAM资源出现瓶颈或者内存泄露,进程异常时致使频繁、大量使用交换分区才会致使严重性能问题。
另外使用Swap交换分区频繁,还会引发kswapd0进程(虚拟内存管理中, 负责换页的)耗用大量CPU资源,致使CPU飙升。
调整Swap分区的大小
以下测试案例所示,Swap分区大小为65535M,我如今想将Swap分区调整为8G,那么咱们来看看具体操做吧
1:查看Swap的使用状况以及相关信息
[root@getlnx14uat ~]# swapon -s Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol01 partition 67108856 878880 -1 [root@getlnx14uat ~]# free -m total used free shared buffers cached Mem: 3957 3920 36 0 39 3055 -/+ buffers/cache: 825 3132 Swap: 65535 858 64677
2: 关闭Swap交换分区
[root@getlnx14uat ~]# swapoff /dev/mapper/VolGroup00-LogVol01 [root@getlnx14uat ~]# swapon -s Filename Type Size Used Priority
3: 这里是缩小Swap分区大小,若是是增大Swap分区大小,那么就须要扩展正在使用的swap分区的逻辑卷,此处使用lvreduce命令收缩逻辑卷。
[root@getlnx14uat ~]# lvreduce -L 8G /dev/mapper/VolGroup00-LogVol01 WARNING: Reducing active logical volume to 8.00 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce LogVol01? [y/n]: y Reducing logical volume LogVol01 to 8.00 GB Logical volume LogVol01 successfully resized
4:格式化swap分区
[root@getlnx14uat ~]# mkswap /dev/mapper/VolGroup00-LogVol01 Setting up swapspace version 1, size = 8589930 kB
5:启动swap分区,并增长到/etc/fstab自动挂载
[root@getlnx14uat ~]# swapon -s Filename Type Size Used Priority [root@getlnx14uat ~]# swapon /dev/mapper/VolGroup00-LogVol01 [root@getlnx14uat ~]# swapon -s Filename Type Size Used Priority /dev/mapper/VolGroup00-LogVol01 partition 8388600 0 -1
参考资料:
https://wiki.archlinux.org/index.php/swap
http://blog.csdn.net/tianlesoftware/article/details/8741873
http://www.makeuseof.com/tag/swap-partition/
https://www.cnblogs.com/kerrycode/p/5246383.html