{"id":83511,"date":"2020-06-01T07:42:29","date_gmt":"2020-06-01T05:42:29","guid":{"rendered":"https:\/\/prohoster.info\/blog\/administrirovanie\/proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra"},"modified":"2020-06-01T07:42:29","modified_gmt":"2020-06-01T05:42:29","slug":"proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra","title":{"rendered":"Raspberry Pi Performance: Adding ZRAM and Adjusting Kernel Parameters","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>A couple of weeks ago, I published <noindex><a rel=\"nofollow\" href=\"https:\/\/haydenjames.io\/pinebook-pro-my-first-impressions-and-setup-tips\/\">a review of the Pinebook Pro.<\/a><\/noindex>Since the Raspberry Pi 4 is also based on ARM, some of the optimizations mentioned in the previous article are quite suitable for it. I would like to share these tips and find out if you experience similar performance improvements.<\/p>\n<p>After setting up the Raspberry Pi in my <noindex><a rel=\"nofollow\" href=\"https:\/\/haydenjames.io\/home-lab-beginners-guide-hardware\/\">home server<\/a><\/noindex> , I noticed that during moments of memory shortage it became very unresponsive and even froze. To address this problem, I added ZRAM and made several adjustments to the kernel parameters.<br \/>\n<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h1>Activating ZRAM on Raspberry Pi<\/h1>\n<p>\n<img decoding=\"async\" alt=\"Raspberry Pi Performance: Adding ZRAM and Adjusting Kernel Parameters\" src=\"\/wp-content\/uploads\/2020\/06\/d5e0d7c2e50dd17a97f008c7ee6dfe30.jpg\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/www.kernel.org\/doc\/Documentation\/blockdev\/zram.txt\">ZRAM<\/a><\/noindex> creates a block storage in RAM named \/dev\/zram0 (or 1, 2, 3, etc.). Pages written to it are compressed and stored in memory. This allows for very fast I\/O operations and also frees up memory through compression.<\/p>\n<p>The Raspberry Pi 4 comes with 1, 2, 4, or 8 GB of RAM. I will be using the 1 GB model, so adjust the instructions according to your model. With 1 GB, the default swap file (slow!) will be used less frequently. I used the following script <noindex><a rel=\"nofollow\" href=\"https:\/\/github.com\/foundObjects\/zram-swap\">zram-swap<\/a><\/noindex> for installation and automatic configuration.<\/p>\n<p>Instructions are provided in the repository linked above. To install:<\/p>\n<pre><code class=\"bash\">git clone https:\/\/github.com\/foundObjects\/zram-swap.git\ncd zram-swap &amp;&amp; sudo .\/install.sh<\/code><\/pre>\n<p>\nIf you want to edit the config:<\/p>\n<pre><code class=\"bash\">vi \/etc\/default\/zram-swap<\/code><\/pre>\n<p>\nIn addition, ZRAM can be activated by installing <code>zram-tools<\/code>. <b>If you are using this method, be sure to edit the config<\/b> in the file <code>\/etc\/default\/zramswap<\/code>, and set about 1 GB of ZRAM:<\/p>\n<pre><code class=\"bash\">sudo apt install zram-tools<\/code><\/pre>\n<p>\nAfter installation, you can check the ZRAM storage statistics with the following command:<\/p>\n<pre><code class=\"bash\">sudo cat \/proc\/swaps\nFilename\t\t\t\tType\t\tSize\tUsed\tPriority\n\/var\/swap                               file\t\t102396\t0\t-2\n\/dev\/zram0                              partition\t1185368\t265472\t5\npi@raspberrypi:~ $<\/code><\/pre>\n<p><\/p>\n<h1>Adding kernel parameters for better ZRAM utilization<\/h1>\n<p>\nNow let's fix the system behavior when the Raspberry Pi resorts to swapping at the last moment, which often leads to freezes. We will add a few lines to the file <b>\/etc\/sysctl.conf<\/b> and reboot.<\/p>\n<p>These lines will 1) <b>delay the inevitable memory exhaustion<\/b>, increasing pressure on the kernel cache, and 2) <b>start preparing for memory exhaustion earlier<\/b>, initiating swapping in advance. But this will be a much more efficient swap with compressed memory via ZRAM!<\/p>\n<p>Here are the lines to add to the end of the file <b>\/etc\/sysctl.conf<\/b>:<\/p>\n<pre><code class=\"plaintext\">vm.vfs_cache_pressure=500\nvm.swappiness=100\nvm.dirty_background_ratio=1\nvm.dirty_ratio=50<\/code><\/pre>\n<p>\nThen restart the system or activate the changes with the following command:<\/p>\n<pre><code class=\"bash\">sudo sysctl --system<\/code><\/pre>\n<p>\n<b>vm.vfs_cache_pressure=500<\/b> increases pressure on the cache, which makes the kernel more inclined to reclaim memory used for caching directory objects and indexes. You will use less memory over a longer period. Sudden drops in performance are mitigated by earlier swapping.<\/p>\n<p><b>vm.swappiness=100<\/b> increases the parameter that determines how aggressively the kernel will swap memory pages, as we are first using ZRAM.<\/p>\n<p><b>vm.dirty_background_ratio=1 &amp; vm.dirty_ratio=50<\/b> \u2014 background processes will start writing immediately upon reaching the 1% limit, but the system will not forcibly perform synchronous I\/O until it reaches the dirty_ratio of 50%.<\/p>\n<p>These four lines (when used with ZRAM) will help improve performance in case you <u>inevitably<\/u> run out of RAM and start swapping, as I have. Knowing this fact, and considering that memory compression in ZRAM is three times better, it's better to start this swapping earlier.<\/p>\n<p>Cache pressure helps because we are effectively telling the kernel: 'Hey, listen, I don't have extra memory to use for cache, so please clear it out as soon as possible and keep only the most frequently used\/important data.'<\/p>\n<p>Even with reduced caching, if over time most of the installed memory is occupied, the kernel will start opportunistic swapping much earlier, so that the CPU (compression) and I\/O for swapping will not drag on until the last moment and utilize all resources at once when it's already too late. ZRAM uses a bit of CPU for compression, but in most low-memory systems, this impacts performance much less than swapping without ZRAM.<\/p>\n<h1>In conclusion<\/h1>\n<p>\nLet's take a look at the result again:<\/p>\n<pre><code class=\"plaintext\">pi@raspberrypi:~ $ free -h\ntotal used free shared buff\/cache available\nMem: 926Mi 471Mi 68Mi 168Mi 385Mi 232Mi\nSwap: 1.2Gi 258Mi 999Mi\n\npi@raspberrypi:~ $ sudo cat \/proc\/swaps \nFilename Type Size Used Priority\n\/var\/swap file 102396 0 -2\n\/dev\/zram0 partition 1185368 264448 5<\/code><\/pre>\n<p>\n264448 in ZRAM is nearly one gigabyte of uncompressed data. Everything went into ZRAM, and nothing ended up in the much slower swap file. Try these settings for yourself; they work on all Raspberry Pi models. My unusable, hanging system has turned into a functional and stable one.<\/p>\n<p>In the near future, I hope to continue and update this article with some results from testing the system before and after installing ZRAM. Right now, I just don\u2019t have the time for it. Meanwhile, feel free to perform your own tests and let me know in the comments. The Raspberry Pi 4 is simply a beast with these settings. Enjoy!<\/p>\n<blockquote><p><b>On the topic:<\/b><\/p>\n<ul>\n<li><noindex><a rel=\"nofollow\" href=\"https:\/\/haydenjames.io\/linux-performance-almost-always-add-swap-space\/\">Linux Performance: Why You Almost Always Need to Increase the Swap Area<\/a><\/noindex> (2017)\n<\/li>\n<li><noindex><a rel=\"nofollow\" href=\"https:\/\/haydenjames.io\/linux-performance-almost-always-add-swap-part2-zram\/\">Linux Performance: Always Increase the Swap Area. Part 2: ZRAM<\/a><\/noindex> (2020)<\/li>\n<\/ul>\n<\/blockquote>\n<p>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/504468\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041f\u0430\u0440\u0443 \u043d\u0435\u0434\u0435\u043b\u044c \u043d\u0430\u0437\u0430\u0434 \u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043e\u0431\u0437\u043e\u0440 Pinebook Pro. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 Raspberry Pi 4 \u0442\u043e\u0436\u0435 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 ARM, \u0442\u043e \u0434\u043b\u044f \u043d\u0435\u0451 \u0432\u043f\u043e\u043b\u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439, \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0445 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. \u0425\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u044d\u0442\u0438\u043c\u0438 \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u0443\u0437\u043d\u0430\u0442\u044c, \u043f\u0440\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u043b\u0438 \u0443 \u0432\u0430\u0441 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 Raspberry Pi \u0432 \u0441\u0432\u043e\u044e \u0434\u043e\u043c\u0430\u0448\u043d\u044e\u044e \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u044f \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u0447\u0442\u043e [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":83512,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-83511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041f\u0430\u0440\u0443 \u043d\u0435\u0434\u0435\u043b\u044c \u043d\u0430\u0437\u0430\u0434 \u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043e\u0431\u0437\u043e\u0440 Pinebook Pro.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c Raspberry Pi: \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c ZRAM \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u044f\u0434\u0440\u0430 | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041f\u0430\u0440\u0443 \u043d\u0435\u0434\u0435\u043b\u044c \u043d\u0430\u0437\u0430\u0434 \u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043e\u0431\u0437\u043e\u0440 Pinebook Pro.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-06-01T05:42:29+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-06-01T05:42:29+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Raspberry Pi Performance: Adding ZRAM and Modifying Kernel Parameters | ProHoster","description":"A couple of weeks ago, I published a review of the Pinebook Pro.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c Raspberry Pi: \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c ZRAM \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u044f\u0434\u0440\u0430 | ProHoster","og:description":"\u041f\u0430\u0440\u0443 \u043d\u0435\u0434\u0435\u043b\u044c \u043d\u0430\u0437\u0430\u0434 \u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043e\u0431\u0437\u043e\u0440 Pinebook Pro.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/proizvoditelnost-raspberry-pi-dobavlyaem-zram-i-izmenyaem-parametry-yadra","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-06-01T05:42:29+00:00","article:modified_time":"2020-06-01T05:42:29+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"83511","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 15:16:22","updated":"2022-09-29 22:32:04","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/83511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=83511"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/83511\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media\/83512"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=83511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=83511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=83511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}