{{short description|Linux memory compression feature}} {{redirect-distinguish|Zcache|Zcash}} {{Distinguish|zram}} {{lowercase title}} {{Use mdy dates|date=August 2014}}
{{Infobox software | name = zswap | title = zswap | logo = <!-- Image name is enough --> | logo caption = | logo_size = | logo_alt = | screenshot = Example of zswap on Linux screenshot.webp | caption = Screenshot of zswap running on openSUSE Tumbleweed | screenshot_size = | screenshot_alt = | collapsible = | author = | developer = Seth Jennings and others | released = {{Start date and age|2013|09|02}} (Linux 3.11) | discontinued = | latest release version = | latest release date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | latest preview version = | latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | status = | programming language = C | operating system = Linux | platform = | size = | language = | language count = <!-- DO NOT include this parameter unless you know what it does --> | language footnote = | genre = Linux kernel features | license = GNU GPL | website = [https://www.kernel.org/doc/html/latest/admin-guide/mm/zswap.html kernel.org] }}
'''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.<ref name="lwn-537422">{{cite web | url = https://lwn.net/Articles/537422/ | title = The zswap compressed swap cache | date = February 12, 2013 | access-date = January 22, 2014 | author = Seth Jennings | publisher = LWN.net}}</ref><ref name="ibm-44a4f27eba32">{{cite web | url = https://www.ibm.com/developerworks/community/blogs/fe313521-2e95-46f2-817d-44a4f27eba32/entry/new_linux_zswap_compression_functionality7?lang=en | title = New Linux zswap compression functionality | date = December 11, 2012 | access-date = January 31, 2014 | author = Jenifer Hopper | publisher = IBM}}</ref><ref name="phoronix-MTQwODI">{{cite web | url = https://www.phoronix.com/scan.php?page=news_item&px=MTQwODI | title = Zswap Merged Into The Linux 3.11 Kernel | date = July 11, 2013 | access-date = February 5, 2014 | author = Michael Larabel | publisher = Phoronix}}</ref>
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.<ref name="kernel-docs">{{cite web | url = https://www.kernel.org/doc/Documentation/vm/zswap.txt | title = Linux kernel documentation: Documentation/vm/zswap.txt | date = November 22, 2013 | access-date = January 22, 2014 | publisher = kernel.org}}</ref>
=={{Anchor|FRONTSWAP|ZBUD|ZSMALLOC}}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.<ref>{{cite web | url = http://article.gmane.org/gmane.linux.kernel.mm/47394 | title = Frontswap [PATCH 0/4] (was Transcendent Memory): Overview | date = April 22, 2010 | access-date = December 23, 2014 | author = Dan Magenheimer | website = gmane.org}}</ref> 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.<ref name="lwn-537422" /><ref>{{cite web | url = https://lwn.net/Articles/386090/ | title = Cleancache and Frontswap | date = May 4, 2010 | access-date = March 26, 2014 | author = Jonathan Corbet | publisher = LWN.net}}</ref>
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 {{Mono|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 {{Mono|zswap.enabled}};<ref name="lwn-537422" /><ref name="ibm-44a4f27eba32" /><ref name="kernel-docs" /> however, it can be enabled by default when configuring the kernel with {{Mono|CONFIG_ZSWAP_DEFAULT_ON=y}}, or it can be enabled at any point via the sysfs interface:<ref name="docs_kernel_org">{{cite web | url = https://docs.kernel.org/admin-guide/mm/zswap.html | title = zswap | access-date = August 25, 2025 | publisher = kernel.org}}</ref> <blockquote> <code>echo 1 > /sys/module/zswap/parameters/enabled</code> </blockquote>
The maximum size of the memory pool used by zswap is configurable through the {{Mono|sysfs}} parameter {{Mono|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.<ref name="lwn-537422" /><ref name="kernel-docs" /><ref>{{cite web | url = https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=2b2811178e85553405b86e3fe78357b9b95889ce | title = Linux kernel source tree: kernel/git/torvalds/linux.git: zswap: add to mm/ | date = July 11, 2013 | access-date = February 5, 2014 | publisher = kernel.org}}</ref>
'''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,<ref>{{cite web | url = https://oss.oracle.com/projects/tmem/dist/documentation/presentations/LSFMM12-zcache-final.pdf#page=12 | title = Zcache and RAMster (oh, and frontswap too): Overview and some benchmarking | date = March 29, 2012 | access-date = August 19, 2015 | author = Dan Magenheimer | website = oss.oracle.com | format = PDF | page = 12}}</ref> 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.<ref name="phoronix-MTQwODI" /><ref>{{cite web | url = https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=4e2e2770b1529edc5849c86b29a6febe27e2f083 | title = Linux kernel source tree: kernel/git/torvalds/linux.git: zbud: add to mm/ | date = July 11, 2013 | access-date = February 5, 2014 | publisher = kernel.org}}</ref>
'''zsmalloc''' is another memory allocator used by zswap, designed for hgher memory density than zbud.<ref>{{Cite news |last=Magenheimer |first=Dan |date=2013-04-03 |title=In-kernel memory compression |url=https://lwn.net/Articles/545244/ |access-date=2026-01-22 |work=LWN.net |language=en-US}}</ref> zsmalloc replaced zbud directly as memory allocator since version 6.18 of the Linux kernel, which was released on December 1, 2025.<ref name=":0">{{Cite news |last=Alden |first=Daroc |date=2025-10-06 |title=6.18 merge window, part 1 |url=https://lwn.net/Articles/1040203/ |access-date=2026-01-22 |work=LWN.net |language=en-US}}</ref><ref>{{Cite news |last=Corbet |first=Jonathan |date=2025-12-01 |title=The 6.18 kernel has been released |url=https://lwn.net/Articles/1048703/ |access-date=2026-01-22 |work=LWN.net |language=en-US}}</ref>
==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.<ref>{{cite web | url = http://article.gmane.org/gmane.linux.kernel/1408711 | title = <nowiki>[PATCH 0/8]</nowiki> zswap: compressed swap caching | date = December 11, 2012 | access-date = January 5, 2014 | website = gmane.org}}</ref><ref>{{cite web | url = http://article.gmane.org/gmane.linux.kernel/1487788 | title = <nowiki>[PATCHv10 0/4]</nowiki> zswap: compressed swap caching | date = May 8, 2013 | access-date = January 5, 2014 | website = gmane.org}}</ref>
zswap (together with zbud) was merged into the Linux kernel mainline in kernel version 3.11, which was released on September 2, 2013.<ref name="kernel-docs" /><ref>{{cite web |date=September 2, 2013 |title=Linux kernel 3.11, Section 9. Zswap: A compressed swap cache |url=https://kernelnewbies.org/Linux_3.11#Zswap:_A_compressed_swap_cache |access-date=January 22, 2014 |website=kernelnewbies.org}}</ref>
Since version 3.15 of the Linux kernel, which was released on June 8, 2014, zswap properly supports multiple swap devices.<ref>{{cite web |date=June 8, 2014 |title=Linux kernel 3.15, Section 4. Memory management |url=https://kernelnewbies.org/Linux_3.15#Memory_management |access-date=June 15, 2014 |website=kernelnewbies.org}}</ref><ref>{{cite web | url = https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=60105e1248f571aa3b895cd63bef072ed9d90c77 | title = Linux kernel source tree: kernel/git/torvalds/linux.git: mm/zswap: support multiple swap devices | date = April 7, 2014 | access-date = June 15, 2014 | publisher = kernel.org}}</ref>
Since version 6.8 of the Linux kernel, which was released on March 11, 2024, zswap supports disabling writeback for specific cgroups.<ref>{{Cite web|url=https://github.com/torvalds/linux/commit/501a06fe8e4c185bbda371b8cedbdf1b23a633d8|title=zswap: memcontrol: implement zswap writeback disabling · torvalds/linux@501a06f|website=GitHub}}</ref>
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.<ref name=":0" />
=={{Anchor|VS-OTHERS}}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 {{Mono|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.<ref name="lwn-545244">{{cite web | url = https://lwn.net/Articles/545244/ | title = In-kernel memory compression | date = April 3, 2013 | access-date = March 8, 2014 | author = Dan Magenheimer | publisher = LWN.net}}</ref>
==See also== {{Portal|Free and open-source software|Linux}} * Cache (computing) * Linux swap * Swap partitions on SSDs
==References== {{Reflist|30em}}
{{Linux kernel}} {{Memory management navbox}} {{Operating system}}
Category:Free software programmed in C Category:Linux kernel features Category:Memory management Category:Virtual memory Category:Software using the GNU General Public License