Newshot

别买内存,压缩就好

Translation(model translation, not the source's own text)

不要买内存,压缩吧!

2026年11月19日

AI 已经发生了很大变化。我们并行做更多的工作。这增加了工作站在内存上的需求。此外,内存价格昂贵,而且往往无法扩展。因此,购买更多内存并不是一个容易的选择。最近我的内存不够用了,我在 zram 中找到了解决方案,这是一种压缩内存的方案。

内存压缩并不新鲜。如果你使用的是 Windows 或 Mac 设备,你已经在使用它了。此外,许多 Android 智能手机也利用压缩技术将更多数据放入内存。不幸的是,并非所有 Linux 环境都默认启用了内存压缩。Fedora、Pop!_OS 启用了,但 Ubuntu 没有。Amazon Linux 只在较小的虚拟机上启用 zram。大多数其他发行版都没有启用它。

压缩内存并非没有缺点。它会多占用一点 CPU,并给某些进程增加延迟。然而,你获得的是巨大的额外容量。压缩比高达 1:4 并不罕见。

我记得在编写 SaaS 软件的早期,当我们为单体应用添加更多功能时,往往需要将机器规模翻倍。通常 CPU 压力很小,所以如果我们启用了内存压缩,本可以避免升级。这本可以将我们的云支出削减一半!

实际效果

我在 Firefox 中打开了很多网站。多到几乎占满了全部系统内存。(我有 32GB,其中一些预留给显卡)


        for u in nos.nl nu.nl telegraaf.nl ad.nl volkskrant.nl nrc.nl rtl.nl tweakers.net buienradar.nl ns.nl npo.nl \
  bbc.com/news edition.cnn.com theguardian.com nytimes.com reuters.com apnews.com aljazeera.com dw.com spiegel.de lemonde.fr washingtonpost.com bloomberg.com \
  theverge.com arstechnica.com techcrunch.com wired.com engadget.com phoronix.com lwn.net news.ycombinator.com tomshardware.com \
  github.com/torvalds/linux github.com/microsoft/vscode github.com/rust-lang/rust stackoverflow.com/questions gitlab.com developer.mozilla.org docs.python.org kernel.org ubuntu.com huggingface.co \
  youtube.com "youtube.com/results?search_query=linux" "youtube.com/results?search_query=music" "youtube.com/results?search_query=documentary" twitch.tv vimeo.com dailymotion.com soundcloud.com open.spotify.com \
  reddit.com reddit.com/r/linux reddit.com/r/worldnews reddit.com/r/pics reddit.com/r/technology x.com instagram.com linkedin.com pinterest.com tumblr.com imgur.com 9gag.com medium.com quora.com \
  bol.com coolblue.nl marktplaats.nl amazon.nl amazon.com ebay.com aliexpress.com ikea.com/nl/nl zalando.nl etsy.com mediamarkt.nl ah.nl \
  google.com/maps openstreetmap.org booking.com airbnb.com tripadvisor.com klm.nl schiphol.nl \
  en.wikipedia.org/wiki/Linux en.wikipedia.org/wiki/Netherlands en.wikipedia.org/wiki/Amsterdam en.wikipedia.org/wiki/World_War_II nl.wikipedia.org/wiki/Nederland \
  imdb.com rottentomatoes.com archive.org nationalgeographic.com nasa.gov flickr.com/explore espn.com weather.com \
  canva.com figma.com notion.so trello.com office.com dropbox.com wikihow.com; do
  firefox --new-tab "https://$u" >/dev/null 2>&1; sleep 1
done
      

如果我们查看正在运行的应用,就会发现它本不该放得下。

应用列表,内存需求超过可用内存
应用列表,内存需求超过可用内存

在内存使用情况中,我们看到部分需求被放入了 zram 交换空间。

内存使用概览。部分应用位于 zram 交换空间中
内存使用概览。部分应用位于 zram 交换空间中

在终端中我们可以查看使用了多少内存,并计算压缩比。

zramctl 概览
zramctl 概览

5.7G 的数据放在 1.7G 的内存中。压缩比为 1:3.35。

入门

文章标题:别买内存,压缩吧

正文: 压缩内存不仅能帮你更充分地利用自己的硬件,也能让你从云环境中获得更多收益。以更少的资源做更多的事。

Arch Linux wiki 上有一篇很好的文章,介绍了如何入门。

期待听到你的经验分享。

联系我

正在为你的项目招聘软件工程师或技术负责人?

更多联系方式 →

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Hacker News
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗