一文说尽 Linux 系统的 swap 交换空间

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你好,我是看山。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用 Ubuntu 已经将近 1 年了,最近重装了 16.04 之后,每天到下午 5 点左右,都会发现 Swap 交换空间有几百兆的写入,系统内存 8G,硬盘是 SSD,i5 处理器,配置中档,也没有启动什么大型软件,就是用 IDEA 做开发,虽然没有影响,但本着一颗求知的心,google 一下,第一篇是 ","attrs":{}},{"type":"link","attrs":{"href":"https://www.linux.com/news/all-about-linux-swap-space","title":"","type":null},"content":[{"type":"text","text":"《All about Linux swap space》","attrs":{}}]},{"type":"text","text":",口气很大,直接翻译了。(译文是在 2016 年 5 月写的,虽然已经是 5 年前,但是 Linux 架构并没有发生变化,所以本文所述还是正确的。)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3e/3ee95997d271adfa959fe4dc238e536a.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 将随机存储 RAM 称为内存页。交换技术就是将一页内存复制到预先设定的硬盘上的交换空间,来释放该页占用内存。物理内存和交换空间的和就是可提供的虚拟内存的总量。有两个原因证明交换技术是很重要的。首先,系统需要的内存量比物理内存更大时,系统内核可以把较少使用的内存页写到交换空间,把空闲出来的内存给当前的应用程序(进程)使用。其次,一个应用启动时使用的内存页,可能只是在初始化时使用,之后不会再用,操作系统就可以把这部分内存页写入交换空间,把空闲出来的内存给其他应用使用或作为磁盘高速缓存。但是,交换技术也有负面作用。相对于内存,硬盘读写速度慢。内存的读写速度可以使用纳秒衡量,但是硬盘的速度只能达到毫秒级,访问硬盘的速度比访问内存的速度慢成千上万倍。发生的交换越多,系统运行越慢。有时候会有过度的交换或内存页频繁的写入写出的抖动发生,因为系统既要保证应用正常运行,又要寻找空闲的内存。这种情况下,只能通过增加 RAM 来解决。Linux 有两种形式的交换空间:交换分区和交换文件。交换分区就是一个独立的硬盘,没有文件或内容。交换文件是文件系统中的一个特殊文件,独立于系统和数据文件之外。可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -s","attrs":{}}],"attrs":{}},{"type":"text","text":"命令查看 swap 空间,输出如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"Filename Type Size Used Priority\n/dev/sda5 partition 859436 0 -1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每一行列出的都是系统正在使用的交换空间。这里的'Type'字段表明该交换空间是一个分区而不是文件,通过'Filename'可以知道交换分区是磁盘 sda5。'Size'字段磁盘大小,单位是 KB,'Used'字段是表示有多少交换空间被使用。'Priority'字段表示 Linux 系统的交换空间使用优先级。有一个重要的特性,如果在 Linux 系统中挂载两个(或更多)具有相同优先级的交换空间(最好是两个不同的设备),Linux 将交替使用,可以提升交换性能。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"交换分区","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要为系统添加一个额外的交换分区,首先你需要准备一个。第一步是确保分区标记为交换分区,第二步是将格式设置为 swap 文件系统。将分区标记为 swap 分区,以 root 权限运行:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"fdisk -l /dev/hdb\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"将'/dev/hdb'替换为你的交换分区的磁盘。输出类似于:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"Device Boot Start End Blocks Id System\n/dev/hdb1 2328 2434 859446 82 Linux swap / Solaris\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果分区没有标记为 swap 分区,你需要使用命令","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"fdisk","attrs":{}}],"attrs":{}},{"type":"text","text":"及参数 t 来声明。操作分区时要小心,你绝对不想删除重要的分区或把系统分区的标识改错。交换分区上的数据会丢失,所以每次改动都需要多次确认。还需要注意的是,Solaris 使用相同的 ID 作为 Linux 交换空间,所以需要小心不要杀掉 Solaris 分区。如果分区已经标记为 swap 分区,就需要通过 root 权限运行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mkswap","attrs":{}}],"attrs":{}},{"type":"text","text":"命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"mkswap /dev/hdb1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果运行没有错误,你的交换空间就开始使用。立即激活:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"swapon /dev/hdb1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以通过","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -s","attrs":{}}],"attrs":{}},{"type":"text","text":"来确认是否运行。为了在系统启动时自动挂载 swap 空间,需要在'/etc/fstab'文件中添加一些列的配置,swap 空间是特殊的文件系统,许多参数不可用。比如:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"/dev/hdb1 none swap sw 0 0\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"检查你的交换空间是无需重新启动,你可以运行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapoff -a","attrs":{}}],"attrs":{}},{"type":"text","text":"命令,然后运行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -a","attrs":{}}],"attrs":{}},{"type":"text","text":",再通过","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon -s","attrs":{}}],"attrs":{}},{"type":"text","text":"检查。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"交换文件","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"和交换分区类似,Linux 也支持使用交换文件,你可以创建、准备,以交换分区的方式挂载。交换文件的好处是,你不需要找一个空的分区或添加额外的交换分区磁盘。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dd","attrs":{}}],"attrs":{}},{"type":"text","text":"命令创建一个空文件。创建一个 1G 的文件,比如:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"dd if=/dev/zero of=/swapfile bs=1024 count=1048576\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"'/swapfile'是交换文件的名字,'count'的 1048576 是文件大小,单位 KB。准备交换文件使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mkswap","attrs":{}}],"attrs":{}},{"type":"text","text":"命令,类似于准备分区,不过这次是使用同一个交换文件:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"mkswap /swapfile\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同样的,挂载交换文件使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"swapon","attrs":{}}],"attrs":{}},{"type":"text","text":"命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"swapon /swapfile\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在'/etc/fstab'中输入下面的内容:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"/swapfile none swap sw 0 0\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"交换空间的大小","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你有很大的内存,有可能没有交换空间,系统也能运行良好。但是如果物理内存耗光,系统就会崩溃,因为它没有其他缓解方式,所以最好还是提供一个交换空间,更何况磁盘比内存便宜很多。关键的问题是内存空间多大?老版的类 UNIX 操作系统要求交换空间是物理内存的两到三倍。现在的扩展版(比如 Linux)不需要这么多,但是如果你配置这些,他们也会使用。重要的原则如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"对于桌面系统,使用系统内存的两倍的交换空间,将可以运行大量的应用程序(其中可能有很多闲置的),使更多的 RAM 用于主要的应用;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"对于服务器,使用小量的交换空间(通常是物理内存的一半),这样你就可以通过监控交换空间的大小来预警是否需要增加 RAM;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"对于老式台式机,使用尽可能大的交换空间","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 2.6 内核中增加一个新的内核参数'swappiness',管理员可以通过该参数修改 Linux 交换方式。参数值从 0 到 100. 从本质上说,值越大,将引起越多内存页发生交换;值越小,就有越多的应用驻留在内存中,而交换空间是空闲的。内核维护者 Andrew Morton 说过,他在他的台式机中设置 swappiness 值是 100,说:“我的观点是,通过内核参数降低交换是错误的。你不需要几百兆的无用应用占用内存。把它放在磁盘上,把内存留给有用的东西。”Morton 的想法有一个漏洞,如果内存交换太快,应用响应就会下降,因为当应用窗口被点击时,应用正在从交换空间读入内存,就会感觉运行很慢。默认的'swappiness'值是 60。你可以使用 root 命令调整参数(作用到重启):","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sh"},"content":[{"type":"text","text":"echo 50 > /proc/sys/vm/swappiness\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你需要使参数永久有效,就需要修改'/etc/sysctl.conf'中的'vm.swappiness'参数。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"结论","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"管理交换空间是系统管理的一个重要方面。有了良好的规划和合理的使用交换技术可以有很多好处。不要害怕实验,并且经常监控你的系统,以确保你得到你需要的结果。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"写在最后","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就目前来说,内存和 SSD 都开始降价,基本上很轻松就能把机器攒到 8G(RAM)+120G(SSD),这样的话,就个人用户的桌面系统而言,交换空间的作用被大大削弱,但是正如上面说的,如果没有交换空间,内存耗光的时候,机器就挂了。因为 SSD 不建议分多个分区,所以使用 swap file 的方式比较好,而且还可以多建几个 swap file 文件,提升交换性能。","attrs":{}}]},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你好,我是看山,10 年老猿,开源贡献者。游于码界,戏享人生。关注公众号:","attrs":{}},{"type":"link","attrs":{"href":"http://static.howardliu.cn/about/kanshanshuo.png","title":"","type":null},"content":[{"type":"text","text":"看山的小屋","attrs":{}}]},{"type":"text","text":",领取资料。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章