Memory compression

Also known as: compressed memory

On this page 7

Memory compression, also called compressed memory, is an operating systems technique in which the kernel compresses rarely used pages and keeps them in a reserved area of RAM instead of writing them to disk. In memory management, it acts as an intermediate tier between active RAM and swap: decompressing a page takes microseconds, while reading it back from storage takes far longer.

Major desktop and mobile platforms enable it by default, including Windows 10 and later, macOS since OS X Mavericks and Linux through zram and zswap. Its value rests on one trade-off: it spends CPU cycles to save memory and avoid costly paging to disk.

How memory compression works

Memory compression works by intercepting pages that the page replacement algorithm has chosen for eviction. Instead of writing such a page to the swap device immediately, the kernel runs a fast compressor over its 4 KiB of contents and stores the result in a compressed pool held in physical memory.

When a process later touches the evicted page, a page fault occurs. The fault handler finds the page in the compressed pool, decompresses it into a free page frame, updates the page table entry and resumes the process. Because no disk I/O is required, the fault is resolved far faster than a major fault served from storage.

The table below traces the lifecycle of a page that passes through compression.

StageKernel actionLocation of data
Active usePage mapped and accessed normallyUncompressed page frame
Selected for evictionReclaim marks page as inactiveUncompressed page frame
CompressedContents encoded, frame freedCompressed pool in RAM
Written backPool full, oldest entries flushedSwap file or partition
Faulted inDecompressed into a new frameUncompressed page frame
Lifecycle of a compressed page

Compression algorithms and achievable ratios

Memory compression relies on fast, lightweight algorithms rather than maximum compression, because every compressed page may need to be restored within microseconds. Common choices are LZ4, LZO and Zstandard, together with specialized schemes such as WKdm on Apple platforms that exploit patterns typical of memory words.

Typical ratios fall between about 2:1 and 4:1, depending heavily on the data. Pages full of zeros, text or pointers compress well; encrypted data, media and already compressed files barely shrink. The table compares common algorithms in general terms.

AlgorithmRelative speedRelative ratioTypical use
LZ4Very fastModerateDefault in many zram setups
LZO / LZO-RLEVery fastModerateOlder Linux defaults
ZstandardFastHighSystems short on RAM
WKdm familyVery fastModeratemacOS and iOS compressor
Xpress variantsFastModerate to highWindows compression store
Common algorithms used for memory compression

Kernels usually reject pages that compress poorly, for example pages that would still occupy more than roughly three quarters of their original size, because storing them saves too little memory to justify the CPU cost.

Implementations in major operating systems

Each major operating system implements memory compression differently, although the principle is the same. The differences lie in where the compressed pool lives, how it is sized and whether it sits in front of a swap device or replaces one.

PlatformMechanismRelation to swap
WindowsCompression store in the Memory Compression processSits in front of the page file
macOSCompressor integrated with the virtual memory systemSits in front of swap files
Linux zramCompressed block device in RAMUsed as a swap device itself
Linux zswapCompressed cache for swap pagesSits in front of a disk swap device
Androidzram-based swapOften no disk swap at all
Memory compression in common platforms

On Windows, the compressed pages belong to a system process whose working set appears in Task Manager, which often leads users to suspect a leak. On Linux, zram suits devices without disk swap, while zswap fits systems that already have swap and want to reduce write traffic to it.

Benefits compared with conventional swapping

The main benefit of memory compression is that it turns many slow disk accesses into fast in-memory decompression. Decompressing a 4 KiB page typically takes a few microseconds, whereas reading from an SSD takes tens to hundreds of microseconds and from a hard disk several milliseconds.

The comparison below places compression between ordinary RAM and disk-based paging.

PropertyUncompressed RAMCompressed memoryDisk swap
Access latencyNanosecondsMicrosecondsTens of microseconds to milliseconds
CPU costNoneModerateLow, but I/O waits
Effective capacity1xAbout 2-4x per pageLimited by storage size
Storage wearNoneNoneWrites wear SSD cells
Risk of thrashingLowDelayedHigh under heavy pressure
Memory compression compared with RAM and swap

Compression also reduces SSD write wear and keeps systems usable on devices with small amounts of RAM, which is why it is standard on phones and budget laptops.

Limitations and tuning considerations

Memory compression is not free, and it cannot fix a system whose working set simply exceeds available memory. When most pages are hot, the kernel keeps compressing and decompressing the same data, which consumes CPU time and can resemble thrashing without any disk activity.

  • Incompressible data, such as encrypted volumes or video buffers, gains little and wastes compression effort.
  • An oversized compressed pool shrinks the memory left for active pages and can raise fault rates.
  • On battery-powered devices, the extra CPU work carries a small energy cost.
  • Compressed memory still counts toward pressure, so an out of memory condition can occur when the pool cannot grow.
  • Monitoring tools may report memory in confusing ways, so compressed size and original size should be read separately.

Administrators usually tune the pool size limit, the algorithm and the swappiness setting together, then validate the result against real workloads rather than synthetic tests.

A non-technical view

A computer keeps the programs it is using in its fast working memory. When that memory fills up, the old approach is to move some data to the much slower disk. Memory compression does something smarter first. It squeezes data the computer is not using right now and keeps it in memory.

It is like vacuum-packing winter clothes in a small closet. The clothes take less space but stay close at hand. Unpacking them takes a moment, yet it is much faster than fetching them from a storage unit across town. The computer fits more in the same memory and slows down less.

A concrete example

A student uses a laptop with 8 GB of RAM for a browser with 40 tabs, a messaging app, a code editor and a video call. Together, these programs request about 11 GB of memory. Without compression, the system would push several gigabytes to the SSD, and switching between tabs would stall noticeably.

With memory compression enabled, the operating system compresses background tabs and idle application data at an average ratio of about 3:1. Roughly 3 GB of inactive pages shrink to about 1 GB inside RAM, so most of the workload fits in physical memory and only a small amount reaches the page file.

When the student returns to an old tab, its pages are restored in milliseconds rather than read from disk. The video call stays smooth, and the laptop remains responsive despite running well beyond its nominal RAM capacity.