Linux zswap vs zram Memory-Compression Playbook

2026-04-07 · software

Linux zswap vs zram Memory-Compression Playbook

Date: 2026-04-07
Category: knowledge
Domain: software / linux / memory management / platform operations

Why this matters

A lot of Linux memory advice still collapses two different tools into one vague sentence:

“Turn on compressed swap.”

That is usually not enough.

zswap and zram are related, but they solve different problems with different trade-offs:

If you pick the wrong one, or stack them carelessly, you can get:

The practical goal is not “use the coolest kernel feature.” The goal is:

stretch usable memory under pressure without creating worse latency, worse write amplification, or harder-to-debug reclaim paths.


1) Fast mental model

zswap in one sentence

zswap intercepts pages on their way to swap, compresses them into an in-RAM pool, and only writes them to the real swap device when that pool needs to evict pages.

Implications:

zram in one sentence

zram creates a compressed RAM-backed block device, which you can format as swap or a filesystem.

Implications:

The simplest distinction


2) The architecture difference that actually changes operations

zswap path

Pressure path, simplified:

  1. kernel decides a page should be swapped,
  2. zswap tries to compress and keep it in its in-memory pool,
  3. if the pool is full or the page is rejected/evicted, it goes to the backing swap device.

Operational meaning:

This is why zswap often makes sense on:

zram path

Pressure path, simplified:

  1. kernel swaps to /dev/zramN,
  2. that page is compressed and stored in RAM-backed zram memory,
  3. the system effectively trades CPU for storing more cold pages in memory.

Operational meaning:

This is why zram often makes sense on:


3) Do not casually stack zswap in front of zram swap

This is the most practical footgun.

If you configure zram as swap and also leave zswap enabled, zswap can act as a compressed cache in front of that zram swap device. That means you are effectively putting one RAM compression layer in front of another.

In practice, that usually means:

For that reason, a good default rule is:

If your primary swap strategy is zram-as-swap, disable zswap.

There are edge cases where people experiment with combining them, but as an operational default it is harder to justify than either:

If you cannot clearly explain why both layers are helping your workload, keep only one.


4) The decision matrix

Choose zram when

A) You want RAM extension before disk

This is the classic zram fit:

B) You care about desktop interactivity under moderate pressure

For many desktops and laptops, zram is a very good “soft landing” before the system starts thrashing on disk.

C) You do not need hibernation to that swap device

This matters because hibernating to swap on zram is not supported. If hibernation is a hard requirement, pure zram swap is usually the wrong core design.

D) You want a simple systemd-managed setup

zram-generator makes this operationally easy on many distros.


Choose zswap when

A) You already have real swap and want to cut swap I/O

This is zswap’s home turf. It is especially attractive when backing swap exists for policy reasons, hibernation reasons, or operational compatibility.

B) You want a compressed buffer before slower swap storage

zswap can significantly reduce disk activity when many swapped pages remain cold enough to stay in the compressed pool.

C) You want to preserve “real swap exists” semantics

This matters for environments where:

D) You are in overcommitted virtualized environments

The kernel docs explicitly call out overcommitted guests sharing I/O as a strong zswap use case, because compressed caching can reduce swap pressure on the shared storage layer.


Choose neither until tested when

A) You are running strict low-latency services

Compressed memory is not free:

For latency-sensitive servers, the right answer may be:

before introducing compressed swap paths.

B) Your pressure is dominated by anonymous-memory explosions that should just fail fast

If the system should shed work or OOM specific cgroups instead of “trying harder,” compressed swap can hide policy problems.


5) A practical rule of thumb by machine type

Laptop / desktop, hibernation not required

Default starting point: zram

Why:

Laptop / desktop, hibernation required

Default starting point: zswap + real disk swap

Why:

Small-RAM edge device / SBC

Default starting point: zram

Why:

General-purpose VM with disk-backed swap already present

Default starting point: zswap

Why:

Latency-sensitive server

Start conservative

If you experiment at all:


6) The two biggest tuning ideas people miss

A) For in-memory swap, swappiness can reasonably be >100

The kernel VM docs explicitly note that for in-memory swap like zram or zswap, as well as hybrids where swap random I/O is faster than filesystem I/O, values beyond 100 can make sense.

That surprises people because they still think of swappiness as “0–100 and higher is crazy.” That mental model is too old.

Why this matters:

This does not mean “set swappiness to 200 everywhere.” It means:

When swap is memory-like rather than disk-like, low swappiness is not automatically optimal.

For zram desktops, a higher swappiness is often worth testing.


B) Priority matters when zram coexists with disk swap

A common and useful pattern is:

That gives you:

  1. compressed-RAM swap first,
  2. slower disk swap second.

This can be a good compromise when you want:

If you do this, keep zswap disabled unless you have a very deliberate reason otherwise.


7) zram-specific operator guidance

Good starting profile

A pragmatic zram starting point is usually:

Important nuance:

zram disksize is the maximum uncompressed payload size, not the physical RAM it will consume.

So a large zram size is not immediately fully reserved. But gigantic values are still not free, because metadata and runaway expectations matter.

Kernel docs note that there is little point in sizing zram much beyond roughly what a plausible compression ratio can support. As a first-pass operational default, “half of RAM” is a sane start, not a universal law.

What to watch

The most useful zram checks are:

Interpret those numbers correctly

If TOTAL keeps climbing close to your comfort limit while the compression ratio is poor, zram is not saving you much.

If huge_pages / incompressible pages are substantial, your workload may not be a good fit for aggressive zram expectations.

If same_pages is high, zram is getting unusually efficient wins from same-filled or highly repetitive pages.

Useful advanced features

zram has some more serious capabilities than many operators realize:

This means zram is not just “tiny laptop trickery.” It can be a more configurable compressed-memory tier than people expect.

Important caution on writeback

The moment you enable zram writeback to backing storage, you are no longer in a pure “RAM-only compressed swap” world. Now you are operating:

That can be useful, but it changes the durability/wear/latency story. If the backing device is flash, write budget and wear become first-class concerns.


8) zswap-specific operator guidance

Good starting profile

A pragmatic zswap starting point is usually:

The kernel exposes key policy knobs such as:

Why hysteresis matters

This is an underappreciated detail. If zswap is allowed to refill too aggressively right after becoming full, pages can churn in and out of the compressed pool with little real benefit.

That is why the kernel added accept_threshold_percent:

Runtime enable/disable nuance

You can enable or disable zswap at runtime, but disabling it does not instantly flush all compressed pages out of the pool. Stored pages remain until they are invalidated or faulted back in. To force them all out, a swapoff on the backing swap device(s) is the hard reset path.

That matters during experiments:

“I turned it off” is not the same as “the system has fully returned to pre-zswap state.”

What to watch

At minimum, watch:

If zswap fills quickly and spends its life evicting to backing swap anyway, the benefit may be small.


9) Compression algorithm selection: do not over-romanticize it

People can spend too much time cargo-culting “best” algorithms. The correct framing is:

For many real systems, the better question is not:

Which algorithm wins benchmark screenshots?

It is:

Does this algorithm improve pressure behavior enough to justify its CPU cost on this machine class?

As a rough intuition:

For zram specifically, recompression and algorithm parameters can make this more nuanced than “pick one forever.”


10) Failure modes and bad interpretations

Failure mode A — treating compression as capacity instead of delay

Compressed swap is usually best thought of as a pressure absorber or delay mechanism, not true free memory. If the working set is simply too large, compression only postpones the problem.

Failure mode B — hiding a cgroup policy problem

If one workload should be constrained or killed earlier, compressed swap can make the box look healthier while masking the actual bad actor. Use it with cgroup memory policy, not instead of cgroup memory policy.

Failure mode C — CPU becomes the new bottleneck

Compression/decompression work is real. On CPU-starved machines, memory compression can shift the pain from I/O stalls to CPU reclaim stalls.

Failure mode D — bad expectations around hibernation

If you need hibernation, do not design around zram swap as though it were ordinary disk swap. It is not.

Failure mode E — combining zram and zswap because “more compression must be better”

Usually not. More layers can mean more CPU and harder-to-predict reclaim with little incremental win.


11) A sane rollout ladder

Stage 1 — Establish baseline pressure behavior

Before enabling anything, capture:

Without that, you cannot tell whether compression actually helped.

Stage 2 — Pick one strategy

Pick exactly one starting design:

Do not start by stacking both.

Stage 3 — Canary on one machine class

Test on a clear cohort:

Stage 4 — Tune only the obvious knobs first

For zram:

For zswap:

Do not jump immediately into exotic multi-algorithm experiments.

Stage 5 — Judge by outcomes, not ideology

Success looks like:

If the machine still falls apart, only with more CPU heat and more reclaim complexity, roll back.


12) Recommended defaults by intent

“I want my laptop to stay usable when RAM gets tight”

Start with:

“I need hibernation and want less swap pain”

Start with:

“I run tiny devices and hate flash wear”

Start with:

“I run low-latency services and I’m not sure this is worth it”

Start with:


13) Bottom line

The practical answer is simpler than the internet often makes it:

Memory compression is useful. But like most kernel knobs, it is most useful when the operator is clear about the actual bottleneck:

Pick the tool that matches that bottleneck.


Sources and further reading