当前位置: 首页>>技术教程>>正文


如何决定交换大小

问题描述

我只是想确认交换分区的大小应该是多少。很多地方都说交换大小应该是 RAM 大小的两倍。有人还告诉我,它不应该超过8 GB。如果RAM大小的两倍超过8 GB,那么Swap分区应该有多大?

最佳思路

以下文本来自 nixCraft 页面 Linux: Should You Use Twice the Amount of Ram as Swap Space? 。 (请点击链接并阅读来源以获取更多信息)

请注意,nixCraft 页面引用了据称来自 OpenBSD FAQ 的文本,但当前文档中没有匹配的文本;然而,这可能是 2006 年曾经存在的东西(因此 2 x RAM 规则在 10 多年前就已经过时了):

\\n

Nonsense rule: Twice the size of your main system RAM for Servers

\\n

According to OpenBSD FAQ:

\\n

\\n

Many people follow an old rule of thumb that your swap partition\\n should be twice the size of your main system RAM. This rule is\\n nonsense. On a modern system, that’s a LOT of swap, most people\\n prefer\\n that their systems never swap. You don’t want your system to ever\\n run\\n out of RAM+swap, but you usually would rather have enough RAM in the\\n system so it doesn’t need to swap.

\\n

\\n

更多来自 nixCraft 页面:

\\n

Red hat recommends setting as follows for RHEL 5:

\\n

\\n

The reality is the amount of swap space a system needs is not really\\n a\\n function of the amount of RAM it has but rather the memory workload\\n that is running on that system. A Red Hat Enterprise Linux 5 system\\n will run just fine with no swap space at all as long as the sum of\\n anonymous memory [memory mapping without file or device backing it: memory obtained from the OS stack and heap] and system V shared memory is less than about 3/4 the\\n amount of RAM. In this case the system will simply lock the anonymous\\n and system V shared memory into RAM and use the remaining RAM for\\n caching file system data so when memory is exhausted the kernel only\\n reclaims pagecache memory.

\\n

Considering that:\\n 1. At installation time when configuring the swap space there is no easy way to predetermine the memory a workload will require, and \\n 2. The more RAM a system has the less swap space it typically needs, a better swap space [might be along this recommendation for CentOS Linux 7.x and RHEL 7]:

\\n

Recommendation for Swap Space in RHEL/CentOS 7

\\n

\\n

nixCraft 页面的结论是:

\\n

Conclusion

\\n

If Linux kernel is going to use more than 2GiB swap space at a time,\\n all users will feel the heat. Either, you get more RAM (recommend) and\\n move to faster storage to improve disk I/O. There are no rules, each\\n setup and configuration is unique. Adjust values as per your\\n requirements. Select amount of swap that is right for you.

\\n

此外,这篇红帽文章:Do we really need swap on modern systems? (2017) 说:

\\n

Our size recommendation for most modern systems is \\u2018a part of the\\n physical RAM\\u2019, for example, 20%. With this, the painfully slow phase\\n of operation in our example will not last as long, and the OOM kicks\\n in earlier.

\\n

Of course, there are scenarios when different behaviour is desired.\\n When aware of the behaviour, such swap configurations are ok, as well\\n as running the system without any swap. Such a system is supported by\\n us as well – but the customer should know the behaviour in the above\\n situations.

\\n

次佳思路

我所知道的交换空间的唯一硬性规则是 1x RAM 到 Hibernate。系统监视器或 CLI free 命令将告诉您根据特定的硬件配置和使用模式正在使用多少交换空间。对于 2GB RAM,我的系统很少使用任何交换空间,而且我见过的从未超过 0.4GB。

第三种思路

交换不是您想要使用的东西。这是您想要作为最后手段的东西,因为交换速度并不快,它是 dead-slow。如果您使用大约 1 GB 的交换空间,那么您的系统响应速度将会非常慢。

SWAP 内存的全部意义在于,当您用完可用 RAM 时,系统不会崩溃。它不是您可以用作额外内存的东西,也不是您真正想要使用的东西,除非您确实必须这样做。

因此,对于大小,我会说 5 GB,但如果您确实正在做需要大量 RAM 的繁重工作,那么您可以选择最高 8 GB。但实际上,如果您开始使用超过 1 GB 的交换空间,请开始终止进程​​。所以你不会发生车祸。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14295.html,未经允许,请勿转载。