linux swap交换空间


linux内存通过 virtual memory 虚拟内存来管理整个内存, physical RAM物理内存和swap交换空间即为virtual memory总量。
 
swap的使用场景
 
swap主要有两个用处
 
1、当系统需要比物理内存更多的内存空间的时候,内核会把内存里边用得比较少的内存页面swap out到交换分区,以空出物理内存给当前应用来快速运行。
 
2、某些应用启动的时候初始化但是随后的应用运行期间不再使用的内存页面,系统会把这部分页面也swap out到交换空间,以留出物理内存页面给其他应用或者磁盘缓存。
 
linux的这种内存管理策略主要用来节约物理内存,提升当前应用的执行速度。但是swap不能当做扩充内存的一个手段,因为swap读写属于磁盘io,要比物理内存的io慢得多。
 
如果系统频繁的swap out内存页面到交换分区,随后又swap in交换分区到内存页面,这说明系统在寻找空闲内存来是多个应用同时运行,也就是说当前系统任务比较繁忙,但是可用内存又不足了,这时候唯一的办法只能通过提高物理内存来解决。
 
因此衡量一个系统内存用量是否到了瓶颈,就可以通过观察swap用量和si so的频率来评估。
 
生成swap
 
swap空间有两种形式:一是交换分区,二是交换文件。总之对它的读写都是磁盘操作。
 
1、交换分区
 
交换分区可以在安装操作系统的时候分配,也可以进入系统后用fdisk来划分一个交换分区
 
比如在磁盘上划分一个/dev/sda5分区,然后标记为交换分区。
 
然后使用mkswap命令在该分区上面建立交换分区文件系统:
 
mkswap /dev/sda5
 
最后激活交换分区:
 
swapon /dev/sda5
 
2、交换文件
 
交换空间也可以是文件,只需要用dd命令在磁盘上建立分配了大小的文件/home/swapfile
 
建立交换分区文件系统
 
mkswap /home/swapfile
 
最后激活它
 
swapon /home/swapfile
 
swap空间大小的分配策略
 
swap空间大小的分配众说纷纭,有说物理内存两倍的,有说物理内存一样大小的,也有说物理内存一半的。在不同场景下,这些说法应该都是对的。(因此拿一个绝对说法当面试题的,考官可能也不真正的懂swap)
 
下面是红帽系的分配策略:
 
Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.
 
So, if:
 
M = Amount of RAM in GB, and S = Amount of swap in GB, then
 
If M < 2
S = M *2
Else
S = M + 2
 
居然有个if else的伪代码来计算swap,最后还有一句:
 
For systems with really large amounts of RAM (more than 32 GB) you can likely get away with a smaller swap partition (around 1x, or less, of physical RAM).
 
所以怎么分,还得看具体应用场景,只要不离谱,都是正确的。
 
红帽系还有个注意项:
 
Important
 
File systems and LVM2 volumes assigned as swap space cannot be in use when being modified. For example, no system processes can be assigned the swap space, as well as no amount of swap should be allocated and used by the kernel. Use the free and cat /proc/swaps commands to verify how much and where swap is in use.
 
The best way to achieve swap space modifications is to boot your system in rescue mode, and then follow the instructions (for each scenario) in the remainder of this chapter. Refer to the Red Hat Enterprise Linux Installation Guide for instructions on booting into rescue mode. When prompted to mount the file system, select Skip.
 
下面是另一个swap分配策略,适合大部分系统和场景:
 
A rule of thumb is as follows:
 
1) for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications;
 
2) for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary;
 
3) for older desktop machines (with say only 128MB), use as much swap space as you can spare, even up to 1GB.
 
swap优化
 
linux内2.6增加一个新参数来管理swap,叫做swappiness ,Swappiness 可以有 0 到 100 的值。设置这个参数为较低的值会减少内存的交换,从而提升一些系统上的响应度。
 
值越高,内存页面越多的swap out到交换空间,值越低,越多的应用使用物理内存空间。因此要想最大限度的使用物理内存,应该尽量减小swappiness的值甚至设置为0。
 
linux默认设置为60,临时修改它的值:
 
echo 10 > /proc/sys/vm/swappiness
 
要永久修改可以在/etc/sysctl.conf文件修改vm.swappiness参数

相关内容