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.
| Stage | Kernel action | Location of data |
|---|---|---|
| Active use | Page mapped and accessed normally | Uncompressed page frame |
| Selected for eviction | Reclaim marks page as inactive | Uncompressed page frame |
| Compressed | Contents encoded, frame freed | Compressed pool in RAM |
| Written back | Pool full, oldest entries flushed | Swap file or partition |
| Faulted in | Decompressed into a new frame | Uncompressed page frame |
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.
| Algorithm | Relative speed | Relative ratio | Typical use |
|---|---|---|---|
| LZ4 | Very fast | Moderate | Default in many zram setups |
| LZO / LZO-RLE | Very fast | Moderate | Older Linux defaults |
| Zstandard | Fast | High | Systems short on RAM |
| WKdm family | Very fast | Moderate | macOS and iOS compressor |
| Xpress variants | Fast | Moderate to high | Windows compression store |
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.
| Platform | Mechanism | Relation to swap |
|---|---|---|
| Windows | Compression store in the Memory Compression process | Sits in front of the page file |
| macOS | Compressor integrated with the virtual memory system | Sits in front of swap files |
| Linux zram | Compressed block device in RAM | Used as a swap device itself |
| Linux zswap | Compressed cache for swap pages | Sits in front of a disk swap device |
| Android | zram-based swap | Often no disk swap at all |
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.
| Property | Uncompressed RAM | Compressed memory | Disk swap |
|---|---|---|---|
| Access latency | Nanoseconds | Microseconds | Tens of microseconds to milliseconds |
| CPU cost | None | Moderate | Low, but I/O waits |
| Effective capacity | 1x | About 2-4x per page | Limited by storage size |
| Storage wear | None | None | Writes wear SSD cells |
| Risk of thrashing | Low | Delayed | High under heavy pressure |
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.