用 zram 压缩 Linux 工作站内存
一篇 Hacker News 文章建议使用 zram 压缩 Linux 内存,以替代购买昂贵或无法扩展的工作站内存。作者在一次 Firefox 测试中把 5.7 GB 数据压缩进 1.7 GB RAM,压缩比为 1:3.35,同时指出这会增加少量 CPU 消耗和进程延迟。文章称 Fedora 与 Pop!_OS 默认启用内存压缩,而 Ubuntu 和大多数其他发行版未默认启用。
对于运行并行 AI 工作负载的工作站,zram 可以在暂不购买更多物理内存的情况下增加可用容量。
AI has changed a lot. We do more work in parallel. This increases the memory demand on our workstations. Also, memory is expensive and often not extendable. So buying more RAM is not an easy option. Recently I was running out of memory, I found my solution in zram, a solution to compress memory.
Memory compression is not new. If you have a Windows or Mac device you are already using it. Also, many Android smartphones utilize the technique of using compression to put more data into the RAM. Unfortunately not all Linux environments have RAM compression enabled by default. Fedora, Pop!_OS do, but Ubuntu doesn’t. Amazon Linux only enables zram on smaller virtual machines. Most other distributions do not have it enabled.
Compressing memory does come with small, but not really noticeable, downsides. It takes a little bit more from your CPU and adds latency to some processes. What you gain is gigantic amounts of extra capacity. It is not uncommon to see compression ratios up to 1:4.
I remember that in the early days of writing SaaS software we would often need double the size of the machines when we added more features to our monolithic app. Often the CPUs were under little stress, so we could have avoided upgrading if we had enabled RAM compression. This could have cut our Cloud spend in half!