In the resource-constrained environment of a mobile device, effective memory management is critical for a smooth user experience. When a device runs out of physical RAM, the operating system is forced to take drastic measures, such as killing background applications, to free up memory. This leads to a frustrating experience where apps you were just using have to reload from scratch when you switch back to them. To mitigate this problem, Android and other Linux-based systems use a clever kernel feature called ZRAM (previously known as “compram”). ZRAM is a form of virtual memory that creates a compressed block device in RAM, which acts as a fast swap space.
The Problem: Running Out of Physical RAM
Smartphones, unlike desktop computers, have a limited amount of physical RAM and typically do not have a dedicated, slow disk-based swap partition. The Android kernel’s memory manager, the Low Memory Killer (LMK), constantly monitors the amount of free memory. When memory pressure becomes high, the LMK starts terminating processes to reclaim memory, following a priority order.
This creates several problems:
- Slow Multitasking: When you switch away from an app (e.g., a game) to quickly check a message, the LMK might kill the game’s process if the system is low on memory. When you switch back, the game has to reload completely, which is slow and frustrating.
- Increased Battery Drain: Constantly reloading applications from scratch consumes more CPU cycles and power than simply resuming them from memory. –System Sluggishness: When memory pressure is high, the system can become unresponsive as the kernel spends significant time trying to free up pages and kill processes.
Android needed a way to “create” more available memory without physically adding more RAM and without killing apps so aggressively.
Introducing ZRAM: A Compressed Swap Space in RAM
ZRAM solves this problem by using a portion of the physical RAM as a compressed swap device. Instead of killing a background app’s process entirely, the kernel can take the memory pages belonging to that app that are least recently used (LRU), compress them, and store them in this dedicated ZRAM area.
This has two immediate benefits:
- The original memory occupied by those pages is now free and can be used by the foreground app, relieving memory pressure.
- The background app’s process is not killed. Its state is preserved in the compressed ZRAM block.
Because the compression ratio is typically 2:1 or even higher, a 1GB ZRAM partition can effectively hold 2GB or more of compressed application memory. This effectively increases the total amount of memory available to the system, allowing more applications to be kept in a “standby” state instead of being killed.
The trade-off is a small amount of CPU overhead required to perform the compression and decompression. However, modern smartphone CPUs are extremely fast at this, and the performance penalty of a quick decompression is vastly smaller than the penalty of a full app reload.
How ZRAM Works Internally: The Kernel-Level Process
ZRAM is implemented as a Linux kernel module. When enabled, it creates a new block device, typically `/dev/block/zram0`.
Here’s a step-by-step look at the process:
- Initialization: During boot, the system allocates a certain portion of physical RAM for the ZRAM device (e.g., 2GB on a device with 8GB of RAM). This block device is then formatted as a swap area and activated.
- High Memory Pressure: The system is running, and the user launches a new, memory-intensive app. The kernel detects that free memory is running low.
- Identifying Swap Candidates: Instead of immediately invoking the Low Memory Killer, the kernel’s memory management subsystem looks for “clean” pages (those that haven’t been modified) or “idle” pages from background processes that can be swapped out.
- Compression and Swap-Out: The kernel takes a 4KB memory page, passes it to a fast compression algorithm (like LZ4 or ZSTD), which might compress it down to 2KB. This compressed block is then written to the `/dev/block/zram0` device (which is, of course, just another area in RAM).
- Memory Reclaimed: The original 4KB page is now freed, and the system’s free memory count increases, relieving the pressure.
- Swap-In (Page Fault): Later, the user switches back to the background app. When the app tries to access a memory address that is now in ZRAM, it triggers a “page fault.”
- Decompression: The kernel traps this fault. It finds the corresponding compressed block in the ZRAM device, fetches it, decompresses it back to its original 4KB size, and places it back into the application’s memory space. The application then continues running, completely unaware that its memory was ever swapped out.
// To check ZRAM status on a rooted device or via ADB shell $ cat /proc/swaps Filename Type Size Used Priority /dev/block/zram0 partition 4194300 1250000 -2 $ zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lz4 4G 1.2G 550M 580M 8 [SWAP] The output above shows a 4GB ZRAM device (`DISKSIZE`) is holding 1.2GB of swapped-out data (`DATA`), which has been compressed down to just 550MB (`COMPR`), saving significant space.
ZRAM vs. Traditional Disk Swap
| Feature | Traditional Disk Swap | ZRAM (RAM Swap) |
|---|---|---|
| Storage Medium | Slow physical storage (HDD or Flash). | Fast physical RAM. |
| Latency | Very high (milliseconds). Limited by I/O speed. | Extremely low (nanoseconds to microseconds). Limited by CPU compression speed. |
| Wear and Tear | Causes significant wear on flash storage (SSD/eMMC) due to frequent writes. | No wear and tear, as RAM has virtually unlimited endurance. |
| Primary Cost | I/O wait time. | CPU cycles for compression/decompression. |
For more technical details on ZRAM, the official Linux Kernel Documentation is the authoritative source.
Frequently Asked Questions
Is ZRAM enabled on all Android phones?
Today, yes, virtually all modern Android smartphones use ZRAM. It has become a standard and essential feature for memory management, especially for devices in the mid-range and budget categories which may have less physical RAM. The size of the ZRAM partition is configured by the device manufacturer and is typically between 25% and 50% of the physical RAM size.
Does ZRAM use a lot of battery?
No, the opposite is often true. While the compression/decompression does use some CPU cycles, this cost is minimal compared to the energy saved by not having to kill and then completely reload an application from flash storage. By improving multitasking and reducing the need for the CPU to do heavy lifting during an app launch, ZRAM generally contributes to better battery life.
What is the difference between ZRAM, ZSWAP, and ZCACHE?
These are all related Linux kernel features that use compression to improve memory management.
- ZRAM: Creates a compressed RAM-based block device to be used as a swap device. It’s self-contained in RAM.
- ZSWAP: Acts as a compressed cache for a traditional, disk-based swap device. When a page is swapped out, ZSWAP tries to compress it and store it in a RAM-based pool. Only if the pool is full does it write the page out to the slower disk. It’s a middle-ground for systems that have both RAM and a slow swap disk.
- ZCACHE: A more complex system that provides a compressed page cache, primarily for file system data, not for swapping anonymous memory.
For mobile devices like Android phones that do not have a disk swap, ZRAM is the ideal and most commonly used solution.
Can I disable or configure ZRAM on my phone?
On a standard, non-rooted Android device, you cannot disable or configure ZRAM. These settings are determined by the manufacturer in the device’s kernel configuration and are considered a core part of the system’s memory management strategy. On a rooted device, it is technically possible to change ZRAM parameters, but it is highly discouraged unless you are an expert, as it can easily lead to system instability and poor performance.