{"id":47119,"date":"2024-08-28T11:59:17","date_gmt":"2024-08-28T11:59:17","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47119"},"modified":"2024-08-28T11:59:17","modified_gmt":"2024-08-28T11:59:17","slug":"enable-zram-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/08\/enable-zram-ubuntu\/","title":{"rendered":"Enable Zram in Ubuntu for Better Performance on 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 is a step by step tutorial shows how to enable zram Kernel module and use as Swap device for better system performance in Ubuntu 24.04 PC\/laptop with limited RAM.<\/p>\n<p>As you may know, Ubuntu usually includes a swap space. It works like MS Windows&#8217; &#8220;virtual memory&#8221;, that uses the hard disk for additional memory when there is not enough physical RAM for all tasks request.<\/p>\n<p>Swap on disk however is slow, which will slow down the overall system performance. So, Linux introduced &#8220;zram&#8221; and &#8220;zswap&#8221; kernel modules to improve performance for computers with small amount of RAM.<\/p>\n<p><!--more--><\/p>\n<p>Instead of using Swap on disk, <b>zram<\/b> creates block device (use no memory initially, and grow or shrink dynamically) in RAM that can use as Swap space. It compresses the data that are going to swap out, then moves into the block device. In the case, it uses less physical memory for more data, while keeps system running faster.<\/p>\n<p>I&#8217;ve written about <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2024\/08\/enable-zswap-ubuntu\/\" target=\"_blank\" rel=\"noopener\">how to enable zswap in Ubuntu<\/a> to do the similar job, by providing a compressed RAM cache for swap pages. Here I&#8217;m going to show you how to use zram instead, which is enabled by default in Fedora and Pop!_OS.<\/p>\n<p><b>NOTE 1: If you have a plenty of RAM that rarely uses swap space, or you&#8217;re CPU-bound, then stay away from zram or zswap, because they consume more CPU for compressing\/decompressing.<\/b><\/p>\n<p><b>NOTE 2: zram will prevent hibernation for working properly! Use zswap instead, if you need sleep to disk.<\/b><\/p>\n<h3>Disable zswap (if enabled)<\/h3>\n<p>Zswap will prevent zram from being used effectively. If you have it enabled in your Ubuntu machine, you need to disable it first for trying out zram.<\/p>\n<p><b>To check if zswap enabled<\/b>, just open terminal (Ctrl+Alt+T) and run command:<\/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 output, &#8220;enabled = &#8220;N&#8221;&#8221; means 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>If it said &#8220;enabled = &#8220;Y&#8221;&#8221;, then you may run the command below to <b>disable zswap at run-time<\/b> (works until reboot):<\/p>\n<pre>echo 0 | sudo tee \/sys\/module\/zswap\/parameters\/enabled<\/pre>\n<p>NOTE: According to the <a href=\"https:\/\/www.kernel.org\/doc\/html\/latest\/admin-guide\/mm\/zswap.html\" target=\"_blank\" rel=\"noopener\">Kernel doc<\/a>, the data in zswap will NOT be freed immediately. You need to wait until they are either invalidated or faulted back into memory, or manually <code>swapoff<\/code> the swap and <code>swapon<\/code> (if need) again.<\/p>\n<p>If you want to disable zswap permanently, then try editing Grub boot-loader, and delete the kernel parameters for zswap (if any).<\/p>\n<pre>sudo nano \/etc\/default\/grub<\/pre>\n<p>Delete <code>zswap.enabled=1<\/code> from &#8220;GRUB_CMDLINE_LINUX_DEFAULT&#8221;, and, finally run <code>sudo update-grub<\/code> to apply changes.<\/p>\n<h3>Option 1: Enable Zram at Run-Time<\/h3>\n<p>To try out the kernel module at run-time, until system reboot or you manually disable it, just do steps below to manually load the module and set up for swap space.<\/p>\n<p><b>1.<\/b> First, press <code>Ctrl+Alt+T<\/code> on keyboard to open up a terminal window. Then, run command:<\/p>\n<pre>sudo modprobe zram<\/pre>\n<p>The command loads the zram module and creates a single block device in RAM. While, all the config files are\u00a0stored in <code>\/sys\/block\/zram0<\/code> directory.<\/p>\n<p><b>Tips:<\/b> If want, you may create multiple zram block devices in RAM. For example, run <code>sudo modprobe zram num_devices=4<\/code> will create 4, while the config files are located in \/sys\/block\/zram{0, 1, 2, 3}.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-module.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47121\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-module-700x341.webp\" alt=\"\" width=\"610\" height=\"297\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-module-700x341.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-module-300x146.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-module.webp 738w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>2.<\/b> As you can see in the last screenshot, the zram block device include many config files under <code>\/sys\/block\/zram0<\/code>. They include:<\/p>\n<ul>\n<li><code>disksize<\/code> &#8211; The maximum un-compressed data it can store.<\/li>\n<li><code>mem_limit<\/code> &#8211; The maximum physical memory it can use to store compressed data.<\/li>\n<li><code>comp_algorithm<\/code> &#8211; Set the compression algorithm. Supported values include <code>lzo<\/code>, <code>z4<\/code>, <code>lz4hc<\/code>, <code>842<\/code>, <code>zstd<\/code>. See the <a href=\"https:\/\/linuxreviews.org\/Comparison_of_Compression_Algorithms\" target=\"_blank\" rel=\"noopener\">comparision<\/a>.<\/li>\n<li><code>max_comp_streams<\/code> &#8211; The number of possible compress operations run at the same time. Default is number of your CPU core.<\/li>\n<li>There are as well many other configure options. See the <a href=\"https:\/\/docs.kernel.org\/admin-guide\/blockdev\/zram.html\" target=\"_blank\" rel=\"noopener\">Kernel Doc<\/a> for more.<\/li>\n<\/ul>\n<p>To set the maximum un-compressed data size it can store, 4GiB for example (replace the number accordingly), use command:<\/p>\n<pre>echo 4G | sudo tee \/sys\/block\/zram0\/disksize<\/pre>\n<p>To set the maximum memory size it can use to store compressed data, 2GiB for example (replace the number accordingly), use command:<\/p>\n<pre>echo 2G | sudo tee \/sys\/block\/zram0\/mem_limit<\/pre>\n<p>And, to set the compression algorithm, use command:<\/p>\n<pre>echo lzo | sudo tee \/sys\/block\/zram0\/comp_algorithm<\/pre>\n<p>Similarly, you may use the <code>echo<\/code> command to configure other options for the zram block device.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/echo-config-zram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47122\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/echo-config-zram-700x441.webp\" alt=\"\" width=\"610\" height=\"384\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/echo-config-zram-700x441.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/echo-config-zram-300x189.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/echo-config-zram.webp 754w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>3.<\/b> After enabled and configured the zram block device, you may then run command to set the device as swap area:<\/p>\n<pre>sudo mkswap \/dev\/zram0<\/pre>\n<p>Finally, enable the swap area with higher priority, via command:<\/p>\n<pre>sudo swapon --priority 100 \/dev\/zram0<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47123\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zram-700x318.webp\" alt=\"\" width=\"610\" height=\"277\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zram-700x318.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zram-300x136.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/enable-zram.webp 754w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>4.<\/b> Finally, you may run <code>swapon<\/code> to identify current swap devices (zram must have the highest priority). And, run the command below to check zram statistics.<\/p>\n<pre>zramctl<\/pre>\n<p>In the output:<\/p>\n<ul>\n<li>DISKSIZE &#8211; means the maximum un-compressed data size it can store.<\/li>\n<li>DATA &#8211; means current size of un-compressed data (1.1G in my case).<\/li>\n<li>COMPR &#8211; the 1.1 G data has been compressed to 209.1M.<\/li>\n<li>TOTAL &#8211; is the current size of physical memory that zram use (metadata + compressed data).<\/li>\n<\/ul>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/monitor-zram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47124\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/monitor-zram-700x394.webp\" alt=\"\" width=\"610\" height=\"343\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/monitor-zram-700x394.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/monitor-zram-300x169.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/monitor-zram.webp 762w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>5. (Optional)<\/b> To take full potential of the zram advantages, you can configure the system&#8217;s swap via 4 commands below:<\/p>\n<pre>echo 180 | sudo tee \/proc\/sys\/vm\/swappiness<\/pre>\n<pre>echo 0 | sudo tee \/proc\/sys\/vm\/watermark_boost_factor<\/pre>\n<pre>echo 125 | sudo tee \/proc\/sys\/vm\/watermark_scale_factor<\/pre>\n<pre>echo 0 | sudo tee \/proc\/sys\/vm\/page-cluster<\/pre>\n<p>They are default <a href=\"https:\/\/github.com\/pop-os\/default-settings\/pull\/163\" target=\"_blank\" rel=\"noopener\">swap settings in Pop!_OS<\/a>, but NOT tested in my case.<\/p>\n<p><b>(Optional) To disable zram at run-time<\/b>, either reboot your computer, or run 2 commands below to disable the swap space and un-load the zram module:<\/p>\n<pre>sudo swapoff \/dev\/zram0<\/pre>\n<pre>sudo modprobe --remove zram<\/pre>\n<h3>Option 2: Enable Zram on Startup (make it permanent)<\/h3>\n<p>The steps in &#8220;Option 1&#8221; do NOT persistent after reboot. If zram works great in your machine, you may do the following steps instead to make it permanent.<\/p>\n<p><b>NOTE:<\/b> In the steps below, I&#8217;ll use <code>nano<\/code> command line text editor for editing config files, which works in most desktops. For choice, you may replace it with <code>gnome-text-editor<\/code> for 24.04 with default GNOME, <code>gedit<\/code> for 22.04 and earlier with default GNOME, <code>mousepad<\/code> for XFCE, <code>xed<\/code> for Linux Mint Cinnamon, or other text editor according to your desktop environment.<\/p>\n<p><b>1.<\/b> First, run command to create (and edit) a <strong>config file to load zram module<\/strong> at boot:<\/p>\n<pre>sudo nano \/etc\/modules-load.d\/zram.conf<\/pre>\n<p>When file opens, just write <b>zram<\/b> into it. Then, press Ctrl+S to save and Ctrl+X to exit.<\/p>\n<p><b>Tips<\/b>: If you want to create multiple zram block devices in RAM, create another file via command below:<\/p>\n<pre>sudo nano \/etc\/modprobe.d\/zram.conf<\/pre>\n<p>Then write <code>options zram num_devices=1<\/code> (change number as you want).<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-rule.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47125\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-rule-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-rule-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-rule-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/load-zram-rule.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>2.<\/b> Next, run command to create a <b>udev rule file to configure zram device options<\/b>.<\/p>\n<pre>sudo nano \/etc\/udev\/rules.d\/99-zram.rules<\/pre>\n<p>When file opens, add line below (<b>NOTE<\/b>: the line include both &#8220;==&#8221; and &#8220;=&#8221;):<\/p>\n<pre>ACTION==\"add\", KERNEL==\"zram0\", ATTR{comp_algorithm}=\"lto\", ATTR{disksize}=\"4G\", ATTR{mem_limit}=\"2G\"<\/pre>\n<p>Here change the value of &#8220;disksize&#8221;, &#8220;mem_limit&#8221;, and &#8220;comp_algorithm&#8221; according to your need. For multiple zram devices, add more lines with different device names for KERNEL (zram1, zram2, &#8230;).<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/udev-rules-zram.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47128\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/udev-rules-zram-700x393.webp\" alt=\"\" width=\"610\" height=\"342\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/udev-rules-zram-700x393.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/udev-rules-zram-300x169.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/udev-rules-zram-768x431.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/udev-rules-zram.webp 906w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>3.<\/b> The <a href=\"https:\/\/wiki.archlinux.org\/title\/Zram\" target=\"_blank\" rel=\"noopener\">Arch Linux wiki<\/a> tells to use udev rule + <code>\/etc\/fstab<\/code> to automatically make and use the zram device as swap at startup. It however causes &#8220;Failed to activate swap dev-zram0.swap&#8221; issue in my case in Ubuntu 24.04.<\/p>\n<p>Instead, create a systemd service works in my case to automatically <b>setup and enable zram block device as swap<\/b>. First, run command to create a service file:<\/p>\n<pre>sudo nano \/etc\/systemd\/system\/zram.service<\/pre>\n<p>When file opens, add the content below and save (Ctrl+S, then Ctrl+X).<\/p>\n<pre>[Unit]\r\nDescription=Swap with zram\r\nAfter=multi-user.target\r\n\r\n[Service]\r\nType=oneshot\r\nRemainAfterExit=true\r\nExecStartPre=\/sbin\/mkswap \/dev\/zram0\r\nExecStart=\/sbin\/swapon -p 100 \/dev\/zram0\r\nExecStop=\/sbin\/swapoff \/dev\/zram0\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/pre>\n<p>After saving the file, run <code>systemctl enable zram<\/code> command to <b>enable the service<\/b>.<br \/>\n<a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zram-service.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-47127\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zram-service.webp\" alt=\"\" width=\"698\" height=\"441\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zram-service.webp 698w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/08\/zram-service-300x190.webp 300w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/a><\/p>\n<p>(<b>Optional<\/b>) If you want to make use the Pop!_OS swap configuration to take full potential of the zram advantages, then, run command to create a config file:<\/p>\n<pre>sudo nano \/etc\/sysctl.d\/99-vm-zram-parameters.conf<\/pre>\n<p>add the lines below and save.<\/p>\n<pre>vm.swappiness = 180\r\nvm.watermark_boost_factor = 0\r\nvm.watermark_scale_factor = 125\r\nvm.page-cluster = 0<\/pre>\n<p>When everything&#8217;s done, restart computer and run <code>swapon<\/code> and <code>zramctl<\/code> command to verify.<\/p>\n<p><b>To undo &#8220;Option 2&#8221;<\/b>, first run <code>systemctl disable zram<\/code> first to disable the service, then delete all the files you created, by replace <code>nano<\/code> with <code>rm<\/code> in the commands above.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is a step by step tutorial shows how to enable zram Kernel module and use as Swap device for better system performance in Ubuntu 24.04 PC\/laptop with limited RAM. As you may know, Ubuntu usually includes a swap space. It works like MS Windows&#8217; &#8220;virtual memory&#8221;, that uses the hard disk for additional 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,2205,2204],"class_list":["post-47119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-ram","tag-zram","tag-zswap"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47119","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=47119"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47119\/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=47119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}