zswap is a Linux kernel feature that provides a compressed write-back cache for swapped pages, as a form of virtual memory compression. Instead of moving memory pages to a swap device when they are to be swapped out, zswap compresses them and then stores them in a memory pool dynamically allocated in the system RAM. This way, writeback to the actual swap device is deferred or even completely avoided, resulting in a significantly reduced I/O load for systems that require swapping; the tradeoff is the need for additional CPU cycles to perform the compression.[1][2][3]
As a result of reduced I/O, zswap offers advantages to various devices that use flash-based storage, including embedded devices, netbooks and similar low-end hardware devices, as well as to other devices that use solid-state drives (SSDs) for storage. Flash memory has a limited lifespan due to its nature, so avoiding using it as swap space extends its lifespan.[4]
Internals
zswap is integrated into the Linux kernel’s virtual memory subsystem using the API provided by frontswap, which is a kernel mechanism that abstracts various types of storage that can be used as swap space.[5] As a result, zswap operates as a backend driver for frontswap by providing a pseudo-RAM device that is exposed internally. In other words, the frontswap API allows zswap to intercept memory pages while they are being swapped out, and to intercept page faults for the already swapped pages; the access to those two paths allows zswap to act as a compressed write-back cache for swapped pages.[1][6]
Internally, zswap uses compression modules provided by the Linux kernel’s crypto API, which makes it possible, for example, to offload the compression tasks from the main CPU to any hardware compression accelerators supported by the Linux kernel. The selection of the desired compression module can be performed dynamically at boot time through the kernel boot parameter zswap.compressor; if not specified, it selects the Lempel–Ziv–Oberhumer (LZO) compression. As of version 3.13 of the Linux kernel, zswap also needs to be explicitly enabled by setting the kernel boot parameter zswap.enabled;[1][2][4] however, it can be enabled by default when configuring the kernel with CONFIG_ZSWAP_DEFAULT_ON=y, or it can be enabled at any point via the sysfs interface:[7]
echo 1 > /sys/module/zswap/parameters/enabled
The maximum size of the memory pool used by zswap is configurable through the sysfs parameter max_pool_percent, which specifies the maximum percentage of total system RAM that can be occupied by the pool. The memory pool is not preallocated to its configured maximum size, and instead grows and shrinks as required. When the configured maximum pool size is reached, or when growing the pool is impossible due to an out-of-memory condition, swapped pages are evicted from the memory pool to a swap device on the least recently used (LRU) basis. This approach makes zswap a true swap cache, as the oldest cached pages are evicted to a swap device once the cache is full, making room for newer swapped pages to be compressed and cached.[1][4][8]
zbud is a special-purpose memory allocator used internally by zswap for storing compressed pages, implemented as a rewrite of the zbud allocator used by the Oracle‘s zcache,[9] which is another virtual memory compression implementation for the Linux kernel. Internally, zbud works by storing up to two compressed pages (“buddies“, hence the allocator name) per physical memory page, which brings advantages due to easy coalescing and reusing of freed space, and disadvantages due to possible lower memory utilization. However, as a result of its design, zbud cannot allocate more memory space than would be originally occupied by the uncompressed pages.[3][10]
zsmalloc is another memory allocator used by zswap, designed for hgher memory density than zbud.[11] zsmalloc replaced zbud directly as memory allocator since version 6.18 of the Linux kernel, which was released on December 1, 2025.[12][13]
History
Both zswap and zbud were created by Seth Jennings. The first public announcement was in December 2012, and development continued until May 2013 at which point the codebase reached maturity, although it still had the status of an experimental kernel feature at this point.[14][15]
zswap (together with zbud) was merged into the Linux kernel mainline in kernel version 3.11, which was released on September 2, 2013.[4][16]
Since version 3.15 of the Linux kernel, which was released on June 8, 2014, zswap properly supports multiple swap devices.[17][18]
Since version 6.8 of the Linux kernel, which was released on March 11, 2024, zswap supports disabling writeback for specific cgroups.[19]
Since version 6.18 of the Linux kernel, which was released on December 1, 2025, zswap uses zsmalloc directly and disables zpool compression sysfs configuration.[12]
Alternatives
One of the alternatives to zswap is zram, which provides a similar but still different “swap compressed pages to RAM” mechanism to the Linux kernel.
The main difference is that zram provides a compressed block device using RAM for storing data, which acts as a regular and separate swap device.
In comparison, zswap acts as a RAM-based cache for swap devices. This provides zswap with an eviction mechanism for less used swapped pages, which zram lacked until the introduction of CONFIG_ZRAM_WRITEBACK in kernel version 4.14. Though, as a result of its design, at least one already existing swap device is required for zswap to be used.[20]
See also
References
- ^ a b c d Seth Jennings (February 12, 2013). “The zswap compressed swap cache”. LWN.net. Retrieved January 22, 2014.
- ^ a b Jenifer Hopper (December 11, 2012). “New Linux zswap compression functionality”. IBM. Retrieved January 31, 2014.
- ^ a b Michael Larabel (July 11, 2013). “Zswap Merged Into The Linux 3.11 Kernel”. Phoronix. Retrieved February 5, 2014.
- ^ a b c d “Linux kernel documentation: Documentation/vm/zswap.txt”. kernel.org. November 22, 2013. Retrieved January 22, 2014.
- ^ Dan Magenheimer (April 22, 2010). “Frontswap [PATCH 0/4] (was Transcendent Memory): Overview”. gmane.org. Retrieved December 23, 2014.
- ^ Jonathan Corbet (May 4, 2010). “Cleancache and Frontswap”. LWN.net. Retrieved March 26, 2014.
- ^ “zswap”. kernel.org. Retrieved August 25, 2025.
- ^ “Linux kernel source tree: kernel/git/torvalds/linux.git: zswap: add to mm/”. kernel.org. July 11, 2013. Retrieved February 5, 2014.
- ^ Dan Magenheimer (March 29, 2012). “Zcache and RAMster (oh, and frontswap too): Overview and some benchmarking” (PDF). oss.oracle.com. p. 12. Retrieved August 19, 2015.
- ^ “Linux kernel source tree: kernel/git/torvalds/linux.git: zbud: add to mm/”. kernel.org. July 11, 2013. Retrieved February 5, 2014.
- ^ Magenheimer, Dan (April 3, 2013). “In-kernel memory compression”. LWN.net. Retrieved January 22, 2026.
- ^ a b Alden, Daroc (October 6, 2025). “6.18 merge window, part 1”. LWN.net. Retrieved January 22, 2026.
- ^ Corbet, Jonathan (December 1, 2025). “The 6.18 kernel has been released”. LWN.net. Retrieved January 22, 2026.
- ^ “[PATCH 0/8] zswap: compressed swap caching”. gmane.org. December 11, 2012. Retrieved January 5, 2014.
- ^ “[PATCHv10 0/4] zswap: compressed swap caching”. gmane.org. May 8, 2013. Retrieved January 5, 2014.
- ^ “Linux kernel 3.11, Section 9. Zswap: A compressed swap cache”. kernelnewbies.org. September 2, 2013. Retrieved January 22, 2014.
- ^ “Linux kernel 3.15, Section 4. Memory management”. kernelnewbies.org. June 8, 2014. Retrieved June 15, 2014.
- ^ “Linux kernel source tree: kernel/git/torvalds/linux.git: mm/zswap: support multiple swap devices”. kernel.org. April 7, 2014. Retrieved June 15, 2014.
- ^ “zswap: memcontrol: implement zswap writeback disabling · torvalds/linux@501a06f”. GitHub.
- ^ Dan Magenheimer (April 3, 2013). “In-kernel memory compression”. LWN.net. Retrieved March 8, 2014.