{"id":2716,"date":"2025-01-06T14:02:18","date_gmt":"2025-01-06T05:02:18","guid":{"rendered":"https:\/\/rageworx.info\/?p=2716"},"modified":"2025-06-16T14:28:34","modified_gmt":"2025-06-16T05:28:34","slug":"best-performance-with-zram-expansion","status":"publish","type":"post","link":"https:\/\/rageworx.info\/?p=2716","title":{"rendered":"Best performance with zram expansion"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2717\" src=\"https:\/\/rageworx.info\/wp-content\/uploads\/2025\/01\/zram-on-1gb-ram-aarch64.png\" alt=\"\" width=\"645\" height=\"665\" srcset=\"https:\/\/rageworx.info\/wp-content\/uploads\/2025\/01\/zram-on-1gb-ram-aarch64.png 645w, https:\/\/rageworx.info\/wp-content\/uploads\/2025\/01\/zram-on-1gb-ram-aarch64-624x643.png 624w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/p>\n<p><em><strong>Notice:<\/strong> <\/em>Update this article to correct some inaccurate information.<\/p>\n<p><a href=\"https:\/\/www.kernel.org\/doc\/html\/latest\/admin-guide\/blockdev\/zram.html\" target=\"_blank\" rel=\"noopener\">ZRAM expansion<\/a> is the best option for expanding memory for Linux systems that for lower performance Cortex-A53 series within 1 GiB system memory.<\/p>\n<h2>How to activate ZRAM low performance systems as like Cortex-A53<\/h2>\n<p>A bah script file for ZRAM, <code>zram.sh<\/code> is here,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">#!\/bin\/bash\r\nexport LANG=C\r\nexport PATH=\"\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin\"\r\nif [ \"$1\" == --help ] || [ \"$1\" == -h ];then\r\n  echo -e \"This is a script made by Botspot to increase usable RAM by setting up ZRAM.\r\nZRAM uses compression to fit more memory in your available RAM.\r\nThis script will setup a ZRAM swapspace that is 4 times larger than usable RAM.\r\nIt also configures high-speed RAM-based file-storage at \/zram.\r\nUsage:\r\nsudo zram.sh               Setup zram-swap and storage (if enabled)\r\nsudo zram.sh stop          Disable all ZRAM devices and exit\r\nsudo zram.sh storage-off   Disable the file-storage at \/zram on next run\r\nsudo zram.sh storage-on    Enable the file-storage at \/zram on next run\r\nzram.sh --help, -h         Display this information and exit\"\r\n  exit 0\r\nfi\r\nif [ $(id -u) -ne 0 ]; then\r\n  echo \"$0 must be run as root user\"\r\n  exit 1\r\nfi\r\n#avoid creating \/zram storage if storage-off flag passed\r\nif [ \"$1\" == storage-off ]; then\r\n  #retain this flag for next boot\r\n  if ! grep -qxF \"ExecStart=\/usr\/bin\/zram.sh storage-off\" \/etc\/systemd\/system\/zram-swap.service ;then\r\n    sed -i \"s+^ExecStart=\/usr\/bin\/zram.sh$+ExecStart=\/usr\/bin\/zram.sh storage-off+g\" \/etc\/systemd\/system\/zram-swap.service\r\n  fi\r\n  echo -e \"zram.sh will not set up file-storage at \/zram from now on.\"\r\n#the \/zram storage can be re-enabled with storage-on flag\r\nelif [ \"$1\" == storage-on ]; then\r\n  sed -i \"s+^ExecStart=\/usr\/bin\/zram.sh storage-off$+ExecStart=\/usr\/bin\/zram.sh+g\" \/etc\/systemd\/system\/zram-swap.service\r\n  echo -e \"zram.sh will set up file-storage at \/zram from now on.\"\r\nfi\r\n# Load zram module\r\nif ! lsmod | awk \"{print $1}\" | grep -qxF zram ;then\r\n  if ! modprobe zram ;then\r\n    echo \"Failed to load zram kernel module\"\r\n    exit 1\r\n  fi\r\nfi\r\n# disable all zram devices\r\necho -n \"Disabling zram... \"\r\nIFS=$'\\n'\r\nfor device_number in $(find \/dev\/ -name zram* -type b | tr -cd \"0123456789\\n\") ;do\r\n  #if zram device is a swap device, disable it\r\n  swapoff \/dev\/zram${device_number} 2&gt;\/dev\/null\r\n  #if zram device is mounted, unmount it\r\n  umount \/dev\/zram${device_number} 2&gt;\/dev\/null\r\n  #remove device\r\n  echo $device_number &gt;\/sys\/class\/zram-control\/hot_remove\r\ndone\r\necho Done\r\nrm -rf \/zram\r\n#exit script now if \"exit\" flag passed\r\nif [ \"$1\" == stop ]; then\r\n  exit 0\r\nfi\r\n#create new zram drive - for swap\r\ndrive_num=$(cat \/sys\/class\/zram-control\/hot_add)\r\n# use zstd compression if available - best option according to https:\/\/linuxreviews.org\/Comparison_of_Compression_Algorithms#zram_block_drive_compression\r\nif cat \/sys\/block\/zram${drive_num}\/comp_algorithm | grep -q zstd ;then\r\n  algorithm=zstd\r\nelse\r\n  algorithm=lz4\r\nfi\r\necho $algorithm &gt; \/sys\/block\/zram${drive_num}\/comp_algorithm\r\ntotalmem=$(free | grep -e \"^Mem:\" | awk '{print $2}')\r\n#create zram disk 4 times larger than usable RAM - compression ratio for zstd can approach 5:1 according to https:\/\/linuxreviews.org\/Zram\r\necho $((totalmem * 1024 * 4)) &gt; \/sys\/block\/zram${drive_num}\/disksize\r\n#make the swap device (by default this will be \/dev\/zram0)\r\nmkswap \/dev\/zram${drive_num}\r\nswapon \/dev\/zram${drive_num} -d -p 1\r\n#create second zram drive: for temporary user-storage at \/zram\r\nif ! grep -qxF \"ExecStart=\/usr\/bin\/zram.sh storage-off\" \/etc\/systemd\/system\/zram-swap.service ;then\r\n  echo \"Setting up ZRAM-powered file storage at \/zram\"\r\n  #create new zram drive\r\n  drive_num=$(cat \/sys\/class\/zram-control\/hot_add)\r\n  # set compression algorithm\r\n  echo $algorithm &gt; \/sys\/block\/zram${drive_num}\/comp_algorithm\r\n  #set the size of drive to be 4 times the available RAM\r\n  echo $((totalmem * 1024 * 4)) &gt; \/sys\/block\/zram${drive_num}\/disksize\r\n  #create a partition and mount it\r\n  mkfs.ext4 \/dev\/zram${drive_num} &gt;\/dev\/null\r\n  mkdir -p \/zram\r\n  mount \/dev\/zram${drive_num} \/zram\r\n  chmod -R 777 \/zram #make writable for any user\r\nfi<\/pre>\n<p>And put, or write script to\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">\/usr\/bin<\/code> with permission executable as like <code>chmod 775<\/code>.<\/p>\n<h3>Enable ZRAM swap<\/h3>\n<ol>\n<li>Required zram module and zramctl must be pre-installed on Linux system (non-installable from apt).<\/li>\n<li>Disable dphy or swap from file.<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">sudo \/usr\/sbin\/dphys-swapfile uninstall<\/code> or <code class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">sudo systemctl disable mkswap.service<\/code>.<\/li>\n<li>And make swap service to be disabled by ..<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">sudo systemctl mask dphys-swapfiles.service<\/code> or <code class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">sudo systemctl mask mkswap.service<\/code><\/li>\n<li>Enable ZRAM swap<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">sudo enable_module zram<\/code><\/li>\n<li>Make zram.sh in <code>\/usr\/bin<\/code> with upper content, and make it runnable with sudo chmod 775 <code>\/usr\/bin\/zram.sh<\/code>.<\/li>\n<li>Make zram service script with this content in <code>\/etc\/systemd\/system\/zram-swap.service<\/code>.<\/li>\n<\/ol>\n<h3>\/etc\/systemd\/system\/zram-swap.service<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-title=\" \/etc\/systemd\/system\/zram-swap.service\">[Unit]\r\nDescription=Configures zram swap device\r\nAfter=local-fs.target\r\n\r\n[Service]\r\nType=oneshot\r\nExecStart=\/usr\/bin\/zram.sh\r\nExecStop=\/usr\/bin\/zram.sh stop\r\nRemainAfterExit=yes\r\n\r\n[Install]\r\nWantedBy = multi-user.target\r\n<\/pre>\n<h3>Add these lines in <code>\/etc\/sysctl.conf<\/code><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\"># ZRAM configuration\r\nvm.swappiness=100\r\nvm.vfs_cache_pressure=500\r\nvm.dirty_background_ratio=1\r\nvm.dirty_ratio=50<\/pre>\n<p>This setting is recommended for zram the best performance.<\/p>\n<h2>Some trick for slower systems<\/h2>\n<p>ZRAM algorithm <code>zstd<\/code> is the best compression but slower than lz4 and lzo. You can update zram.sh script to select lz4 first then lzo, later for std from line 60 to &#8230;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">if cat \/sys\/block\/zram${drive_num}\/comp_algorithm | grep -q lz4 ;then\r\n  algorithm=lz4\r\nelif cat \/sys\/block\/zram${drive_num}\/comp_algorithm | grep -q lzo ;then\r\n  algorithm=lzo\r\nelse\r\n  algorithm=zstd\r\nfi\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Notice: Update this article to correct some inaccurate information. ZRAM expansion is the best option for expanding memory for Linux systems that for lower performance Cortex-A53 series within 1 GiB system memory. How to activate ZRAM low performance systems as like Cortex-A53 A bah script file for ZRAM, zram.sh is&#8230; <a href=\"https:\/\/rageworx.info\/?p=2716\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":2722,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[580,3,12],"tags":[807,153,806],"class_list":["post-2716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-raphs","category-toughts","tag-expansion","tag-linux","tag-zram"],"_links":{"self":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/2716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2716"}],"version-history":[{"count":7,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/2716\/revisions"}],"predecessor-version":[{"id":2806,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/2716\/revisions\/2806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/media\/2722"}],"wp:attachment":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}