<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zram Archives - Learning to Pi</title>
	<atom:link href="https://www.learningtopi.com/tag/zram/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.learningtopi.com/tag/zram/</link>
	<description>Programming Python on SBC&#039;s and Microcontrollers</description>
	<lastBuildDate>Sun, 18 Jun 2023 02:54:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://www.learningtopi.com/wp-content/uploads/2022/04/cropped-circuit-150x150.png</url>
	<title>zram Archives - Learning to Pi</title>
	<link>https://www.learningtopi.com/tag/zram/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>ZRAM for your SBC</title>
		<link>https://www.learningtopi.com/uncategorized/zram-for-your-sbc/</link>
					<comments>https://www.learningtopi.com/uncategorized/zram-for-your-sbc/#respond</comments>
		
		<dc:creator><![CDATA[tdunteman]]></dc:creator>
		<pubDate>Sun, 18 Jun 2023 02:54:55 +0000</pubDate>
				<category><![CDATA[Raspberry Pi SBC]]></category>
		<category><![CDATA[Rock5B]]></category>
		<category><![CDATA[SBC]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[zram]]></category>
		<guid isPermaLink="false">https://www.learningtopi.com/?p=1073</guid>

					<description><![CDATA[<p>One issue that I have seen crop up multiple times on different SBC&#8217;s is memory and swap. I have seen many SBC&#8217;s out out of the box that come with no swap space enabled. For systems with no GUI running a single task, this rarely turns into an issue. For larger systems running a GUI...</p>
<p>The post <a href="https://www.learningtopi.com/uncategorized/zram-for-your-sbc/">ZRAM for your SBC</a> appeared first on <a href="https://www.learningtopi.com">Learning to Pi</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:50%">
<p>One issue that I have seen crop up multiple times on different SBC&#8217;s is memory and swap.  I have seen many SBC&#8217;s out out of the box that come with no swap space enabled.  For systems with no GUI running a single task, this rarely turns into an issue.  For larger systems running a GUI with desktop applications this can result in performance issues even on something as powerful as a Radxa Rock 5B with 8GB of RAM.  You may need ZRAM for your SBC!</p>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:50%">
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="566" src="https://www.learningtopi.com/wp-content/uploads/RAM-medium-1024x566.jpg" alt="" class="wp-image-1078" srcset="https://www.learningtopi.com/wp-content/uploads/RAM-medium-1024x566.jpg 1024w, https://www.learningtopi.com/wp-content/uploads/RAM-medium-300x166.jpg 300w, https://www.learningtopi.com/wp-content/uploads/RAM-medium-150x83.jpg 150w, https://www.learningtopi.com/wp-content/uploads/RAM-medium-768x424.jpg 768w, https://www.learningtopi.com/wp-content/uploads/RAM-medium-850x470.jpg 850w, https://www.learningtopi.com/wp-content/uploads/RAM-medium.jpg 1254w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
</div>



<div class="wp-block-ht-block-toc  is-style-outline htoc htoc--position-wide toc-list-style-plain" data-htoc-state="expanded"><span class="htoc__title"><span class="ht_toc_title">Table of Contents</span></span><div class="htoc__itemswrap"><ul class="ht_toc_list"><li class=""><a href="#htoc-why-do-we-need-swap">Why do we need swap?</a></li><li class=""><a href="#htoc-what-is-zram">What is ZRAM?</a></li><li class=""><a href="#htoc-raspberry-pi-os">Raspberry Pi OS</a></li><li class=""><a href="#htoc-ubuntu-23-04">Ubuntu 23.04</a></li><li class=""><a href="#htoc-debian-11-bullseye">Debian 11 (Bullseye)</a></li><li class=""><a href="#htoc-summary">Summary</a></li></ul></div></div>



<h2 class="wp-block-heading" id="htoc-why-do-we-need-swap">Why do we need swap?</h2>



<p>If you browse the web you&#8217;ll find many different answers to this question.  For a system running a single process, you probably don&#8217;t.  Over the years I have deployed several Raspberry Pi Zero&#8217;s, 3B&#8217;s and 4B&#8217;s that have no swap enabled.  These are all headless systems running just the python app that I wrote for that particular deployment.  All of these systems are able to run for an extended period of time (months+) with no issues.</p>



<p>Once we start running graphical OS&#8217;s, desktop applications with web browsers, file browsing, etc the need for swap becomes clear.  The kernel uses free memory as a file buffer which allows faster access for files that are commonly used.  On my Rock 5B you can see how the memory has currently being used:</p>



<pre class="wp-block-code"><code>$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.5Gi       2.7Gi       3.2Gi       133Mi      <mark> 1.8Gi</mark>       4.8Gi
Swap:          2.0Gi          0B       2.0Gi</code></pre>



<p>Here we can see of 3.2GB of free RAM, but 4.8GB of available.  Most of the difference here is due to the file buffer.  We also can see that no data has been swapped at this time, and there is plenty of free memory even with the current buffer.</p>



<p>As we start to run more applications and open more files, the memory utilization will start to shift.  Even though the kernel will free up space from the buffer when needed, without a swap I have run into application hangs long before I ran out of memory.  In other instances during burn in tests I&#8217;ve had the system kill my application since it was considered the memory &#8220;hog&#8221;.</p>



<p>After running a few web browser windows, downloading some files and launching a couple of VSCode consoles, my memory usage now looks like this:</p>



<pre class="wp-block-code"><code>$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.5Gi       3.9Gi        55Mi       190Mi       3.9Gi       3.7Gi
Swap:          2.0Gi        84Mi       1.9Gi
 </code></pre>



<p>Here you can see that the file buffer has shot up to 3.8GB, used RAM to 3.9GB, and free dropped to 55MB.  You can also see some RAM has been swapped (albiet a small amount).</p>



<h2 class="wp-block-heading" id="htoc-what-is-zram">What is ZRAM?</h2>



<p>ZRAM creates a virtual object (in memory) to hold paged data in a compressed format.  While ZRAM does use some of your memory, on systems that have slow disk I/O (i.e. those using SD cards), this allows the system to take less frequently used memory pages and compress them.</p>



<p>In old kernel versions (i.e. 3.15 and earlier) it was required to run a ZRAM object per CPU core.  This was due to the inability for multiple cores to access the same ZRAM device at the same time.  If you search for ZRAM, you&#8217;ll probably find lots of examples of systems configured with both one ZRAM device for the system as well as those with a ZRAM device per CPU core.</p>



<p>Given that it is 2023, we will assume you are running a 5.x or 6.x Linux kernel and will configure with a single ZRAM device for simplicity.  HOWEVER, a lot of the default scripts and tools still assume one device per core, so there may be some adjustments required to ensure proper operations.</p>



<p>You can read more about ZRAM here: <a href="https://www.kernel.org/doc/html/next/admin-guide/blockdev/zram.html" target="_blank" rel="noreferrer noopener">https://www.kernel.org/doc/html/next/admin-guide/blockdev/zram.html</a></p>



<h2 class="wp-block-heading" id="htoc-raspberry-pi-os">Raspberry Pi OS</h2>



<p>The classic Raspberry Pi OS by default creates a swap file in <code>/var</code> sized at 100MB.  The presense of SOME swap seems to alleviate most of the issues, however since most Raspberry Pi devices run off SD, there is a VERY LARGE performance hit to having the swap on flash. </p>



<p>The swap file is created by the<code> dphys-swapfile</code> service which configures the swap file on startup.  Before we configure ZRAM, we first should disable the swapfile.  This can be done by simply disabling the service (it will take effect on next boot).</p>



<pre class="wp-block-code"><code>sudo systemctl disable dphys-swapfile</code></pre>



<p>After we disable the swapfile, the next step is to install the ZRAM tools:</p>



<pre class="wp-block-code"><code>sudo apt install zram-tools</code></pre>



<p>The ZRAM configuration will all be done in <code>/etc/defaults/zramswap</code> file.  The default configuration will all be commented.  I will only edit the size value (configured in MB) to switch from 256MB to 2GB (my Pi4B is an 8GB model).  Set the size appropriate for your device (i.e. a Pi Zero with 512MB of RAM I would set to 128MB).</p>



<pre class="wp-block-code"><code>#ALGO=lz4
#PERCENT=50
SIZE=2048
#PRIORITY=100</code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p id="htoc-note-the-compression-algorithm-can-be-changed-to-increase-the-compression-or-decrease-the-cpu-usage-i-ll-leave-the-default-here-you-can-identify-what-algorithms-are-supported-by-your-kernel-by-running-cat-sys-block-zram0-comp-algorithm-after-you-have-booted-with-your-zram">NOTE:  The compression algorithm can be changed to increase the compression or decrease the CPU usage.  I&#8217;ll leave the default here.  You can identify what algorithms are supported by your kernel by running <code>cat /sys/block/zram0/comp_algorithm</code> after you have booted with your ZRAM.</p>
</blockquote>



<p>Reload to apply your changes.  At this point you should see 2GB of swap when you run <code>free -h</code>.  If you don&#8217;t see your swap space, check the status of the zramswap service to see what caused the failure.</p>



<pre class="wp-block-code"><code>$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.6Gi       141Mi       7.3Gi       1.0Mi       195Mi       7.4Gi
<mark>Swap:          2.0Gi          0B       2.0Gi</mark>
$ sudo systemctl status zramswap.service 
* zramswap.service - Linux zramswap setup
     Loaded: loaded (/lib/systemd/system/zramswap.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2023-06-16 14:08:33 MST; 20min ago
       Docs: man:zramswap(8)
    Process: 565 ExecStart=/usr/sbin/zramswap start (code=exited, status=0/SUCCESS)
   Main PID: 565 (code=exited, status=0/SUCCESS)
        CPU: 122ms

Jun 16 14:08:33 ... systemd&#91;1]: Starting Linux zramswap setup...
Jun 16 14:08:33 ... root&#91;575]: Starting Zram
Jun 16 14:08:33 ... zramswap&#91;575]: &lt;13&gt;Jun 16 14:08:33 root: Starting Zram
Jun 16 14:08:33 ... zramswap&#91;630]: Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
Jun 16 14:08:33 ... zramswap&#91;630]: no label, UUID=142f8653-e64b-4985-9c89-c129397cea8a
Jun 16 14:08:33 ... systemd&#91;1]: Finished Linux zramswap setup.
pi@rpi-kvm1:~ $ </code></pre>



<h2 class="wp-block-heading" id="htoc-ubuntu-23-04">Ubuntu 23.04</h2>



<p>I am currently running Ubuntu 23.04 on my Radxa Rock 5B, so I&#8217;ll use this as my example.  First we need to make sure the ZRAM packages are installed:</p>



<pre class="wp-block-code"><code>sudo apt install zram-config zram-tools</code></pre>



<p>After this is installed, then we can configure the <code>/etc/defaults/zramswap</code> just like we did with the Raspberry Pi.  One difference though, my kernel didn&#8217;t have LZ4 support, so I needed to swap to LZO-RLE:</p>



<pre class="wp-block-code"><code>ALGO=lzo-rle
#PERCENT=50 
SIZE=2048 
#PRIORITY=100</code></pre>



<p>After a reboot, we can check to see that the swap space was created.  Check the status of the zramswap service to see what caused the error.</p>



<pre class="wp-block-code"><code>~$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.5Gi       692Mi       6.1Gi        45Mi       846Mi       6.8Gi
Swap:          2.0Gi          0B       2.0Gi
$ sudo systemctl status zramswap.service 
● zramswap.service - Linux zramswap setup
     Loaded: loaded (/lib/systemd/system/zramswap.service; enabled; preset: enabled)
     Active: active (exited) since Fri 2023-06-16 14:21:15 MST; 9min ago
       Docs: man:zramswap(8)
    Process: 916 ExecStart=/usr/sbin/zramswap start (code=exited, status=0/SUCCESS)
   Main PID: 916 (code=exited, status=0/SUCCESS)
        CPU: 32ms

Jun 16 14:21:15 ... systemd&#91;1]: Starting zramswap.service - Linux zramswap setup...
Jun 16 14:21:15 ... zramswap&#91;923]: &lt;13&gt;Jun 16 14:21:15 root: Starting Zram
Jun 16 14:21:15 ... root&#91;923]: Starting Zram
Jun 16 14:21:15 ... zramswap&#91;936]: Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
Jun 16 14:21:15 ... zramswap&#91;936]: no label, UUID=14f66260-16d7-4031-894c-59245d11f6be
Jun 16 14:21:15 ... systemd&#91;1]: Finished zramswap.service - Linux zramswap setup.</code></pre>



<h2 class="wp-block-heading" id="htoc-debian-11-bullseye">Debian 11 (Bullseye)</h2>



<p>Debian only has a single package to install.  I&#8217;m using my Rock 5B for testing again, so no LZ4 compression available (this simply depends on the kernel).</p>



<pre class="wp-block-code"><code>sudo apt install zram-tools</code></pre>



<p>The /etc/defaults/zramswap is configured the same way:</p>



<pre class="wp-block-code"><code>ALGO=lzo-rle 
#PERCENT=50  
SIZE=2048  
#PRIORITY=100</code></pre>



<h2 class="wp-block-heading" id="htoc-summary">Summary</h2>



<p>Don&#8217;t forget to add some type of swap space!  For SBC&#8217;s ZRAM is ideal to prevent writing to the SD card for swap.  Raspberry Pi OS defaults to using a swap file, so disabling this and enabling ZRAM is ideal.  For Ubuntu or other Linux based distributions, be sure to install the ZRAM tools and configure the service.  If you are experiencing slow performance and system hangs (in particular in a GUI), be sure to check your swap configuration before you go any further!</p>



<p>If you are looking for more info on the Radxa Rock 5B, check out <a href="https://www.learningtopi.com/category/sbc/rock5b/">related articles</a>.</p>
<p>The post <a href="https://www.learningtopi.com/uncategorized/zram-for-your-sbc/">ZRAM for your SBC</a> appeared first on <a href="https://www.learningtopi.com">Learning to Pi</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.learningtopi.com/uncategorized/zram-for-your-sbc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
