From 47911055a04ef0125effb8dc693a14d9cfcca054 Mon Sep 17 00:00:00 2001 From: Xingyu Wang Date: Sat, 28 Jan 2023 11:41:19 +0800 Subject: [PATCH] ATRP @wxy https://linux.cn/article-15487-1.html --- ...1111.4 ⭐️⭐️ Drop swap for zram on Linux.md | 194 ++++++++++++++++++ ...1111.4 ⭐️⭐️ Drop swap for zram on Linux.md | 191 ----------------- 2 files changed, 194 insertions(+), 191 deletions(-) create mode 100644 published/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md delete mode 100644 sources/tech/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md diff --git a/published/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md b/published/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md new file mode 100644 index 0000000000..16f7e92bcb --- /dev/null +++ b/published/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md @@ -0,0 +1,194 @@ +[#]: subject: "Drop swap for zram on Linux" +[#]: via: "https://opensource.com/article/22/11/zram-swap-linux" +[#]: author: "David Both https://opensource.com/users/dboth" +[#]: collector: "lkxed" +[#]: translator: "wxy" +[#]: reviewer: "wxy" +[#]: publisher: "wxy" +[#]: url: "https://linux.cn/article-15487-1.html" + +在 Linux 上用 zram 替代传统交换空间 +====== + +![][0] + +> zram 是一个用于创建内存压缩缓存的工具,特别是可以用作交换空间。 + +我在我的电脑上花了很多时间(我是说工作),我发现了很多有趣的东西。其中最近引起我注意的是 `zram0` 设备。我是在几个月前写一篇文章时第一次注意到它,它显示在 `lsblk` 命令的输出中: + +``` +# lsblk +NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS +sda 8:0 0 931.5G 0 disk +├─sda1 8:1 0 600M 0 part +[...] +zram0 252:0 0 8G 0 disk [SWAP] +``` + +它被识别为交换空间,这就是首先引起我的好奇心的原因,所以我做了一些研究。zram 最初被称为 “压缩缓存compcache”,即 “压缩的高速缓存”。事实证明,zram 是一个用于创建内存内压缩缓存的工具,特别是作为交换空间使用。 + +但为什么呢? + +当我开始研究 zram 时,我只发现了几篇关于将 zram 用于交换空间的基础文章。起初,这对我来说似乎有点违反直觉。毕竟,如果你的内存快用完了,你把页面交换到内存中的虚拟驱动器中,有什么好处呢? + +然后我找到了 Fedora 项目的维基页面,它提议使用 [zram 交换空间][1]swap-on-zram。该建议说:“交换是有用的,除了它的速度很慢。zram 是一个使用了压缩的内存驱动器。在启动时创建一个 zram 交换空间,并且不再使用默认的交换分区。” + +该页面的其余部分是关于它的细节、好处、副作用和反馈。 + +### Linux 上用于交换空间的 zram + +使用 zram 作为交换空间,与常规的基于分区或基于文件的交换空间做的事情相同。当内存压力过大时,一些最近使用最少的数据会被移到交换空间。平均来说,它会被压缩到其原始大小的 50% 左右,并被放置在内存的 zram 空间中。这比将这些内存页存储在硬盘上要快得多,并可以释放出它所使用的内存用于其他用途。 + +### 节省交换空间 + +我试图找到关于配置多少交换空间或 zram 交换空间的总结建议。这使我重新回顾了交换空间的设置,以及我之前的文章《[现代 Linux 系统的正确交换空间是多少?][2]》。就我所知,从 RHEL 和 Fedora 的最新文档来看,推荐的交换空间数量并没有改变。不过,该文档忽略了 zram 的使用。 + +然而,在不使用 zram 的旧版 Linux 或 zram 被禁用的情况下,之前文章中的表格仍然为交换空间的分配提供了一个好的起点。 + +我找到的关于 zram 功能的文档在 zram 如何根据内存大小分配空间,以及分配给 zram 交换空间的数量方面是不一致的。 + +由于缺乏权威性的文档,我进行了一些实验来凭经验确定用于分配 zram 交换空间的算法。我为此使用了我自己的物理和虚拟系统。结果很有趣,与我迄今为止发现的任何文档都不一致。 + +在所有足够大的系统上,zram 的默认大小是 8GB,但在内存较小的主机上通常会大大减少。在我用于测试的一台虚拟机(VM)上,可以访问 4GB 的内存,zram 的虚拟交换空间被分配为 3.8GB。我的一台旧戴尔电脑拥有 8GB 的内存,zram 被设置为 7.6GB。当内存减少到 2GB 时,zram 就减少到 1.9GB。 + +我拥有的所有内存超过 8GB 的物理和虚拟主机都显示正好是 8GB 的 zram。这包括我拥有 64GB 内存的主工作站和其他拥有 16GB 或 32GB 内存的主机。 + +基于这几个数据点,我可以得出这样的结论:目前的默认设置是最多 8GB 的 zram,而在 8GB 或以下的主机上,zram 占内存的 95%。 + +我读过一些文章,其中提到了 zram 交换空间的其他大小,甚至高达 100% 的内存,但这些似乎都是理论上的,而不是现实。 + +你的发行版可能不同,但这里是 Fedora 和类似发行版的实际 zram 交换空间的分配情况: + +- 内存 ⇐ 8 GB:0.95 × 内存 +- 内存 > 8 GB:8 GB + +请注意,zram 交换空间大小的算法并没有基于对任何给定的现实世界的系统或应用程序的 “最佳” 交换大小的建议。这种 zram 交换空间的分配是一种相当概率性的方法,它应该在广泛的 Linux 主机上运行良好。然而,最大的 zram 交换空间大小被配置为 8GB,而且我一直推荐 8GB 作为传统交换空间的最大容量,我想我可以说它反映了 zram 交换空间的最佳大小。 + +### 管理 zram 交换空间 + +zram 的默认值保存在 `/usr/lib/systemd/zram-generator.conf` 配置文件中。以下是我的一个测试虚拟机,分配了 5097GB 的内存。 + +``` +# cat /usr/lib/systemd/zram-generator.conf +# This config file enables a /dev/zram0 device with the default settings: +# - size - same as available RAM or 8GB, whichever is less +# - compression - most likely lzo-rle +# +# To disable, uninstall zram-generator-defaults or create empty +# /etc/systemd/zram-generator.conf file. +[zram0]zram-size= min(ram, 8192) +``` + +你可以在 `zram-generator.conf` 配置文件的最后一行改变默认的 zram 交换空间大小。但我建议不要这样做,除非你能明确说明这样做的原因,并在你做任何改变后测试你的结果。像 Linux 中的许多其他配置默认值一样,zram 的默认值已经被很好地测试过了,适合大多数使用情况。 + +### 监控 zram + +可以使用 `zramctl` 工具来查看 zram 的当前状态。 + +``` +# zramctl +NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT +/dev/zram0 lzo-rle       4.8G   4K   80B   12K       4[SWAP] +``` + +传统的 `swapon` 命令也可以用来查看交换,包括作为交换使用的 zram: + +``` +# swapon --show +NAME       TYPE      SIZE USED PRIO +/dev/zram0 partition 4.8G   0B  100 +``` + +需要注意的是,`zramctl` 在不包含数据时不报告 zram,所以结果会包含空输出。而像 `lsblk`、`swapon`、 `top`、`free`、`htop` 等工具,即使不包含数据,也会显示 zram。 + +### 停用 zram + +`swapoff -a` 命令会关闭 zram 交换空间以及用作交换的传统 HDD 或 SSD 存储。`swapon -a` 命令在 zram 为空时不显示它,可以使用 `zramctl /dev/zram0` 代替。 + +``` +# swapon --show# lsblk +NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS +sda             8:00  120G  0 disk +├─sda1          8:10    1G  0 part /boot/efi +├─sda2          8:20    1G  0 part /boot +└─sda3          8:30  118G  0 part +  ├─vg01-root 253:00   10G  0 lvm  / +  ├─vg01-swap 253:10    3G  0 lvm  [SWAP] +  ├─vg01-usr  253:10   30G  0 lvm  /usr +  ├─vg01-home 253:20   10G  0 lvm  /home +  ├─vg01-var  253:30   30G  0 lvm  /var +  └─vg01-tmp  253:40   10G  0 lvm  /tmp +sr0            11:01 1024M  0 rom +zram0         252:00    0B  0 disk +# zramctl## zramctl /dev/zram0 +NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT +/dev/zram0 lzo-rle         0B   0B    0B    0B       4 +``` + +注意,`/dev/zram0` 在这些命令中并没有显示为交换空间,直到它被用于该目的。这给我造成了一些困惑,直到我的实验表明这是事实。 + +### 创建 zram 交换空间 + +zram 本身已经存在了大约 20 年,但只是在过去的一两年里才在一些发行版上作为交换空间使用。你的一些或所有主机上当前的 Linux 环境可能没有用 zram 创建交换空间。如果是这种情况,它可以很容易地被补救。 + +对于 Fedora 32,它是默认使用 zram 交换空间之前的最后一个版本,它只需要三个简单的命令。 + +首先,验证是否存在 `zram-swap.service` 文件,它作为 `zram` RPM 包的一部分安装: + +``` +# systemctl status zram-swap +● zram-swap.service - Enable compressed swap in memory using zram +     Loaded: loaded (/usr/lib/systemd/system/zram-swap.service; disabled; vendor preset: disabled) +     Active: inactive (dead) +``` + +接下来,安装 `zram-generator-defaults` 和 `zram-generator` 软件包: + +``` +# dnf install zram-generator-defaults zram-generator +``` + +启用并启动 `zram-swap` 服务: + +``` +# systemctl enable zram-swap.service# systemctl start zram-swap.service +``` + +然后验证 `zram0` 是否存在并被用作交换空间: + +``` +# lsblk +NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT +sda             8:00  120G  0 disk +├─sda1          8:10    2G  0 part /boot +└─sda2          8:20  118G  0 part +  ├─vg01-root 253:00   10G  0 lvm  / +  ├─vg01-swap 253:10    3G  0 lvm  [SWAP] +  ├─vg01-usr  253:20   35G  0 lvm  /usr +  ├─vg01-tmp  253:30   15G  0 lvm  /tmp +  ├─vg01-var  253:40   35G  0 lvm  /var +  └─vg01-home 253:50   20G  0 lvm  /home +sr0            11:01 1024M  0 rom +zram0         252:00  7.5G  0 disk [SWAP] +``` + +### 用 zram 改进交换空间 + +这就是全部内容了。在 Fedora 上这很容易。不同的发行版可能也一样简单,只是软件包名称和命令的细节可能不同。在你的电脑上试试 zram 交换空间吧。在我的下一篇文章中,我将进一步演示一些 zram 选项。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/22/11/zram-swap-linux + +作者:[David Both][a] +选题:[lkxed][b] +译者:[wxy](https://github.com/wxy) +校对:[wxy](https://github.com/wxy) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/dboth +[b]: https://github.com/lkxed +[1]: https://fedoraproject.org/wiki/Changes/SwapOnZRAM +[2]: https://opensource.com/article/19/2/swap-space-poll +[0]: https://img.linux.net.cn/data/attachment/album/202301/28/113826twvkkbrso9ws2kss.jpg \ No newline at end of file diff --git a/sources/tech/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md b/sources/tech/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md deleted file mode 100644 index df8dd3e3fa..0000000000 --- a/sources/tech/20221111.4 ⭐️⭐️ Drop swap for zram on Linux.md +++ /dev/null @@ -1,191 +0,0 @@ -[#]: subject: "Drop swap for zram on Linux" -[#]: via: "https://opensource.com/article/22/11/zram-swap-linux" -[#]: author: "David Both https://opensource.com/users/dboth" -[#]: collector: "lkxed" -[#]: translator: " " -[#]: reviewer: " " -[#]: publisher: " " -[#]: url: " " - -Drop swap for zram on Linux -====== - -Zram is a tool for creating an in-RAM compressed cache, specifically for use as swap space. - -I spend a lot of time playing (I mean working) on my computers, and I've found a lot of interesting things. One that has most recently come to my attention is the `zram0` device. I first noticed it when working on one of my Opensource.com articles several months ago. It showed up in the output from the `lsblk` command: - -``` -# lsblk -NAME          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS -sda             8:00 931.5G  0 disk -├─sda1          8:10   600M  0 part -[...] -zram0         252:00     8G  0 disk [SWAP] -``` - -It's identified as swap space, which is what first piqued my curiosity, so I did some exploration. Zram was originally called "compcache," which stands for "compressed cache." It turns out that zram is a tool for creating an in-RAM compressed cache, specifically for use as swap space. - -But Why? - -When I began researching zram, all I found were a couple of basic articles about using zram for swap space. At first, this seemed a bit counterintuitive to me. After all, if you're running out of RAM and you swap pages into a virtual drive in RAM, what's gained? - -I then found the Fedora Project wiki page that proposed the use of [Swap on zram][1]. The proposal says: "Swap is useful, except when it's slow. zram is a RAM drive that uses compression. Create a swap-on-zram during start-up. And no longer use swap partitions by default." - -The rest of the page is about details, benefits, side effects, and feedback. - -### Zram for swap space on Linux - -Using zram for swap space is intended to do the same thing as regular partition-based or file-based swap space. When memory pressure becomes too great, some of the least recently used data is moved to swap space. On average, it's compressed to about 50% of its original size, and placed in zram space in RAM. This is much faster than storing those memory pages on a hard drive and frees up the RAM it was using for other use. - -### Saving on swap - -I tried to find revised recommendations for how much swap or zram swap to configure. This led me back to a reassessment of swap, and my previous article, [What's the right amount of swap space for a modern Linux system?][2] As far as I can tell from the most current documentation for RHEL and Fedora, the recommended amount of swap space has not changed. That documentation, however, ignores the use of zram. - -However, the tables in that previous article still provide a good starting point for swap space allocation when using older releases of Linux that don't use zram or in cases where zram has been disabled. - -The documents I found for the Zram feature are inconsistent in terms of how zram is allocated with respect to RAM size, and the amount of space allocated to zram swap. - -Due to the lack of authoritative documentation, I performed some experiments to empirically determine the algorithm used to allocate zram swap. I used my own physical and virtual systems for this. The results are interesting and do not match any documentation I've so far found. - -The default size of zram is 8 GB on all systems large enough to support that, but it's typically reduced significantly on hosts with small amounts of RAM. On one virtual machine (VM) I use for testing, with access to 4 GB of RAM, the zram virtual swap space is allocated to 3.8 GB. One old Dell I have contains 8 GB of RAM, and the zram is set to 7.6 GB. When RAM is reduced to 2 GB, Zram is reduced to 1.9 GB. - -All physical and virtual hosts I have with more than 8 GB of RAM show exactly 8 GB of zram. This includes my primary workstation with 64 GB of RAM and other hosts with 16 GB or 32 GB of RAM. - -Based on these few data points, I can draw the conclusion that the current default settings are for 8 GB of zram at most, and for zram to be 95% of RAM on hosts with 8 GB or less. - -I have read a number of articles that mention other sizes for zram swap, even up to 100% of RAM, but those all seem to be theoretical rather than reality. - -Your distribution may be different, but here are the actual zram swap allocations for Fedora and similar distributions: - -- **RAM ⇐ 8 GB:** 0.95 × RAM -- **RAM > 8 GB:** 8 GB - -Be aware that the zram swap size algorithm is not based on any recommendations for the "best" swap size for any given real-world system or application. This zram swap allocation is a rather probabilistic approach to what should work well on a wide range of Linux hosts. However, the fact that the maximum zram swap size is configured for 8 GB and the fact that I have always recommended 8 GB as the maximum amount of traditional swap, I think I can say it's reflective of the optimum sizes for zram swap. - -### Managing zram swap - -Zram defaults are stored in the `/usr/lib/systemd/zram-generator.conf` configuration file. The following is from one of my test VMs with 5097 GB of RAM allocated. - -``` -# cat /usr/lib/systemd/zram-generator.conf -# This config file enables a /dev/zram0 device with the default settings: -# - size - same as available RAM or 8GB, whichever is less -# - compression - most likely lzo-rle -# -# To disable, uninstall zram-generator-defaults or create empty -# /etc/systemd/zram-generator.conf file. -[zram0]zram-size= min(ram, 8192) -``` - -You can change the default Zram swap size in the last line of the `zram-generator.conf` configuration file. I recommend against doing that, unless you can definitively show a reason for doing so, and test your results once you make any changes. Like many other configuration defaults in Linux, the zram ones have been well-tested and are appropriate for most use cases. - -### Monitor zram - -The zramctl utility can be used to view the current state of zram. - -``` -# zramctl -NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT -/dev/zram0 lzo-rle       4.8G   4K   80B   12K       4[SWAP] -``` - -The traditional `swapon` command can also be used to view swap including zram used as swap: - -``` -# swapon --show -NAME       TYPE      SIZE USED PRIO -/dev/zram0 partition 4.8G   0B  100 -``` - -One thing to be aware of is that `zramctl` does not report on zram when it contains no data, so the results contain null output. Tools like `lsblk`, `swapon`, `top`, `free`, `htop`, and so on, do show zram even when it contains no data. - -### Deactivate zram - -The `swapoff -a` command turns off `zram` swap as well as traditional HDD or SSD storage used as swap. The `swapon -a` command does not show zram when it is empty. Use `zramctl /dev/zram0` instead. - -``` -# swapon --show# lsblk -NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS -sda             8:00  120G  0 disk -├─sda1          8:10    1G  0 part /boot/efi -├─sda2          8:20    1G  0 part /boot -└─sda3          8:30  118G  0 part -  ├─vg01-root 253:00   10G  0 lvm  / -  ├─vg01-swap 253:10    3G  0 lvm  [SWAP] -  ├─vg01-usr  253:10   30G  0 lvm  /usr -  ├─vg01-home 253:20   10G  0 lvm  /home -  ├─vg01-var  253:30   30G  0 lvm  /var -  └─vg01-tmp  253:40   10G  0 lvm  /tmp -sr0            11:01 1024M  0 rom -zram0         252:00    0B  0 disk -# zramctl## zramctl /dev/zram0 -NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT -/dev/zram0 lzo-rle         0B   0B    0B    0B       4 -``` - -Note that `/dev/zram0` doesn't show up in these commands as swap space until it's being used for that purpose. This caused me some confusion until my experiments showed it to be the case. - -### Creating Zram Swap - -Zram itself has been around for about 20 years, but has only been in use as swap space on some distributions for the last year or two. The current Linux installation on some or all of your hosts may not have been created with zram for swap. If that's the case, it can be easily remedied. - -For Fedora 32, the last release prior to the default use of zram for swap, it only takes three easy commands. - -First, verify the presence of the `zram-swap.service` file, installed as part of the `zram` RPM package. - -``` -# systemctl status zram-swap -● zram-swap.service - Enable compressed swap in memory using zram -     Loaded: loaded (/usr/lib/systemd/system/zram-swap.service; disabled; vendor preset: disabled) -     Active: inactive (dead) -``` - -Next, install the `zram-generator-defaults` and `zram-generator` packages. - -``` -# dnf install zram-generator-defaults zram-generator -``` - -Enable and start the zram-swap service: - -``` -# systemctl enable zram-swap.service# systemctl start zram-swap.service -``` - -And then verify that `zram0` exists, and is being used as swap space: - -``` -# lsblk -NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT -sda             8:00  120G  0 disk -├─sda1          8:10    2G  0 part /boot -└─sda2          8:20  118G  0 part -  ├─vg01-root 253:00   10G  0 lvm  / -  ├─vg01-swap 253:10    3G  0 lvm  [SWAP] -  ├─vg01-usr  253:20   35G  0 lvm  /usr -  ├─vg01-tmp  253:30   15G  0 lvm  /tmp -  ├─vg01-var  253:40   35G  0 lvm  /var -  └─vg01-home 253:50   20G  0 lvm  /home -sr0            11:01 1024M  0 rom -zram0         252:00  7.5G  0 disk [SWAP] -``` - -### Improve swap with zram - -That's all there is to it. It was easy with Fedora. Different distributions will likely be just as easy, with some possible different details in the package names and commands. Give zram swap a try on your computer. In my next article, I'll demonstrate some further zram options. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/22/11/zram-swap-linux - -作者:[David Both][a] -选题:[lkxed][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/dboth -[b]: https://github.com/lkxed -[1]: https://fedoraproject.org/wiki/Changes/SwapOnZRAM -[2]: https://opensource.com/article/19/2/swap-space-poll