{"id":47110,"date":"2024-08-26T16:18:48","date_gmt":"2024-08-26T16:18:48","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47110"},"modified":"2024-09-06T08:17:01","modified_gmt":"2024-09-06T08:17:01","slug":"enable-zswap-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/08\/enable-zswap-ubuntu\/","title":{"rendered":"How to Enable Zswap for Ubuntu 24.04 PC with Limited RAM"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-47111\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon-700x700.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/ram-memory-icon.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This tutorial shows how to enable zswap kernel module to increase usable RAM in your Ubuntu computer or laptop.<\/p>\n<p>As you know, Ubuntu Linux usually includes a swap space (either disk partition or swap file) on the disk. It can be used for hibernation, and as extended memory space. When the system needs more memory resources and the RAM is going full, then the inactive pages in memory are moved to the swap space.<\/p>\n<p>Swap space can be helpful for computers with limited RAM. But, it&#8217;s slow! Too much swap in use will slow down the overall system performance.<\/p>\n<p>As workarounds, Linux includes 2 kernel modules: <b>zram<\/b> and <b>zswap<\/b>. Instead of moving inactive pages from memory to disk (swap space), they do similar things by compressing the data into dynamically allocated RAM space.<\/p>\n<p>As the data is compressed, it takes less physical memory space which makes your RAM even more usable. While, keeping the system running smoothly because reading from compressed data in RAM is usually faster than reading from swap on disk.<\/p>\n<p><!--more--><\/p>\n<h3>zram and zswap<\/h3>\n<p><b>zram<\/b> creates a block device in RAM, that acts as swap space (or a general-purpose RAM disk). When system needs to swap out inactive pages, zram compress them into the block device. No swap on disk is required.<\/p>\n<p><b>zswap<\/b> is a lightweight compressed RAM cache for swap pages. It attempts to compress the pages, that are needed to swap out, and store them into a memory pool in RAM. Poorly compressible data is directly written to disk (swap space). And, when the memory pool is full or the RAM is exhausted, the least recently used page is decompressed and written to disk (swap). A swap space on disk is required!<\/p>\n<p>Both zram and zswap do NOT use any RAM initially. They dynamically grow or shrink RAM usage as required. The difference is that zram works as swap space with higher priority, so hibernation won&#8217;t work normally. While zswap needs a swap space on disk to work along with.<\/p>\n<p><b>Use zram or zswap is a good choice for computer with small amount of RAM, or for large apps\/games that need more memory than physical RAM capacity.<\/b><\/p>\n<p><b>However, if you have a plenty of RAM that rarely uses swap space, or you&#8217;re CPU-bound (bottlenecked by the CPU), then you may stay away from them, because they consume more CPU for compressing\/decompressing.<\/b><\/p>\n<h3>Option 1: Enable Zswap in Run-time<\/h3>\n<p>If you need swap for hibernation feature, then <a href=\"https:\/\/docs.kernel.org\/admin-guide\/mm\/zswap.html\" target=\"_blank\" rel=\"noopener\">zswap<\/a> is the better choice. All current Ubuntu releases have the kernel module loaded by default, all you need to do is just enable the function and do few parameter changes.<\/p>\n<p><b>1.<\/b> First, press <code>Ctrl+Alt+T<\/code> on keyboard to open up a terminal window. Then, run command to check current zswap module parameters:<\/p>\n<pre>systool -v -m zswap<\/pre>\n<p><i>Run <code>sudo apt install sysfsutils<\/code> if command not found.<\/i> In the terminal output, it should print that &#8220;enabled = &#8220;N&#8221;&#8221; meaning the feature is not enabled.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/check-zswap.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47112\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/check-zswap-700x395.webp\" alt=\"\" width=\"610\" height=\"344\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/check-zswap-700x395.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/check-zswap-300x169.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/check-zswap.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>All the module parameters can be modified by the config files under <code>\/sys\/module\/zswap\/parameters\/<\/code> directory. They include:<\/p>\n<ul>\n<li><code>accept_threshold_percent<\/code> &#8211; The percentage threshold at which zswap would start accepting pages again after it became full. Default in 90% in Ubuntu.<\/li>\n<li><code>compressor<\/code> &#8211; Set the compression algorithm. Possible values include <code>zstd<\/code>, <code>deflate<\/code>, <code>lzo<\/code>, <code>842<\/code>, <code>lz4<\/code> and <code>lz4hc<\/code>. See the <a href=\"https:\/\/linuxreviews.org\/Comparison_of_Compression_Algorithms\" target=\"_blank\" rel=\"noopener\">comparision<\/a>.<\/li>\n<li><code>enabled<\/code> &#8211; enable \/ disable the function by setting value 0 or 1.<\/li>\n<li><code>exclusive_loads<\/code> &#8211; invalidate zswap entries when pages are loaded. Values include 0 or 1.<\/li>\n<li><code>max_pool_percent<\/code> &#8211; the maximum percentage of physical memory that zswap can take.<\/li>\n<li><code>zpool<\/code> &#8211; control the management of the compressed memory pool. Values include <code>zbud<\/code> (2:1 or less compression ratio), <code>z3fold<\/code> (3:1 or less), <code>zsmalloc<\/code>(more complex method since Kernel 6.3).<\/li>\n<\/ul>\n<p><b>2. To enable zswap<\/b>, simply write &#8220;1&#8221; to the <code>enabled<\/code> config file by running command in terminal:<\/p>\n<pre>echo 1 | sudo tee \/sys\/module\/zswap\/parameters\/enabled<\/pre>\n<p>And, you may change the zpool value to <code>zsmalloc<\/code> via command:<\/p>\n<pre>echo zsmalloc | sudo tee \/sys\/module\/zswap\/parameters\/zpool<\/pre>\n<p>You can as well run similar commands above to configure more zswap parameters. To verify, simply run the <code>systool<\/code> command in step 1 again:<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zswap.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47117\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zswap-700x504.webp\" alt=\"\" width=\"610\" height=\"439\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zswap-700x504.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zswap-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zswap.webp 730w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>3.<\/b> To see zswap statistics, simply run the command below in terminal:<\/p>\n<pre>sudo grep -r . \/sys\/kernel\/debug\/zswap<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zswap-static.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47114\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zswap-static-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zswap-static-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zswap-static-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zswap-static.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Option 2: Enable zswap on startup<\/h3>\n<p>The previous commands enable zswap on the run-time, but it won&#8217;t persistent after system reboot.<\/p>\n<p>To make it permanent, you may configure the Grub boot-loader to load the Kernel parameters on startup.<\/p>\n<p><b>1.<\/b> First, open terminal (Ctrl+Alt+T) and run command to edit the config file for Grub:<\/p>\n<pre>sudo nano \/etc\/default\/grub<\/pre>\n<p><i>Here I use <code>nano<\/code> command line text editor that works in most desktop environments. You may replace it with <code>gnome-text-editor<\/code> for 24.04 with default GNOME, <code>gedit<\/code> for 22.04 and earlier, or other editors depends on your desktop.<\/i><\/p>\n<p><b>2.<\/b> When file opens, add following kernel parameters as value of &#8220;GRUB_CMDLINE_LINUX_DEFAULT&#8221;:<\/p>\n<pre>zswap.enabled=1 zswap.zpool=zsmalloc<\/pre>\n<p>You can add\/remove more zswap parameters as you want (e.g., <code>zswap.compressor=lzo<\/code>), and separate them with blank space. Finally, press Ctrl+S to save and Ctrl+X to exit nano text editor.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-zswap.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47115\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-zswap-700x453.webp\" alt=\"\" width=\"610\" height=\"395\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-zswap-700x453.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-zswap-300x194.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-zswap-768x497.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-zswap.webp 866w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>3.<\/b> After saving the changes, run the command below to re-generate Grub boot menu entries:<\/p>\n<pre>sudo update-grub<\/pre>\n<p><b>Tips:<\/b> For those who have <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2022\/04\/install-grub-customizer-ubuntu-2204\/\" target=\"_blank\" rel=\"noopener\">Grub-customizer<\/a> installed, just launch the app and navigate to &#8220;General Settings&#8221; page for adding the Kernel parameters, and finally click Save to apply changes.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-customizer-zswap.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47116\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-customizer-zswap-700x502.webp\" alt=\"\" width=\"610\" height=\"437\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-customizer-zswap-700x502.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-customizer-zswap-300x215.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-customizer-zswap-768x551.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/grub-customizer-zswap.webp 928w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to enable zswap kernel module to increase usable RAM in your Ubuntu computer or laptop. As you know, Ubuntu Linux usually includes a swap space (either disk partition or swap file) on the disk. It can be used for hibernation, and as extended memory space. When the system needs more memory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":47111,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[1195,2204],"class_list":["post-47110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-ram","tag-zswap"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=47110"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47110\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/47111"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=47110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}