不要买内存,压缩
Do not buy RAM, compress

原始链接: https://curiousbits.nl/blog/do-not-buy-ram-compress/

随着AI工作负载和并行任务增加内存需求,升级RAM可能成本高昂或不可行。作者推荐使用zram/RAM压缩,许多平台已经使用,但并非所有Linux发行版默认启用。压缩会稍微增加CPU使用并带来延迟,但能大幅扩展有效内存,常可达1:4比例。作者指出这在早期SaaS扩展时本可将云成本减半。Firefox压力测试显示5.7 GB数据被压缩到1.7 GB RAM,约1:3.35。总体而言,RAM压缩帮助用户和云环境以更少资源做更多事;设置指南可在Arch Linux wiki找到。

一篇标题为“不要买内存,压缩”的 Hacker News 帖子主张使用 zram 内存压缩而非升级硬件。作者在运行多个 Claude 会话时内存耗尽后实现了 zram,从而在同一设备上完成更多工作,并引发了关于云环境中潜在收益的问题。一位评论者指出,macOS 已使用类似压缩,Pop!_OS 多年来已启用 zram,且在 1GB 服务器上运行良好。
相关文章

原文

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 not come with downsides. It takes a little bit more from your CPU and adds latency to some processes. However, 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!

How it looks in practice

I opened a lot of websites in Firefox. So many, it would almost fully take all system memory. (I have 32GB with some reserved for the graphics)

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

If we look at the running apps, then we see it should not fit.

List of apps with more memory demand than available memory

In the memory usage we see that some demand was placed into the zram swap space.

Overview of memory usage. Some applications are in zram swap space

In the terminal we can check how much memory is used, and we can calculate the compression ratio.

zramctl overview

5.7G of data in 1.7G of RAM. That’s a compression ratio of 1:3.35.

Getting started

Compressing RAM is something which helps to get you more out of your own hardware, but also out of your cloud environment. Do more with less.

The Arch Linux wiki has a good article on how to get started.

Looking forward to hear about your experiences.

联系我们 contact @ memedata.com