<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Zram on LEl_FENG Blog</title><link>https://blog.xpdbk.com/en/categories/zram/</link><description>Recent content in Zram on LEl_FENG Blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>LEl_FENG Copyright</copyright><lastBuildDate>Thu, 18 Jul 2024 20:20:43 +0700</lastBuildDate><atom:link href="https://blog.xpdbk.com/en/categories/zram/index.xml" rel="self" type="application/rss+xml"/><item><title>How to enable Zram in Linux</title><link>https://blog.xpdbk.com/en/posts/linux-zram%E7%94%9F%E6%88%90/</link><pubDate>Thu, 18 Jul 2024 20:20:43 +0700</pubDate><guid>https://blog.xpdbk.com/en/posts/linux-zram%E7%94%9F%E6%88%90/</guid><description>&lt;img src="https://blog.xpdbk.com/en/posts/linux-zram%E7%94%9F%E6%88%90/install-zram-ubuntu.webp" alt="Featured image of post How to enable Zram in Linux" />&lt;blockquote>
&lt;p>&lt;strong>TL;DR / [Geek Summary]:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Memory Alchemy: Enable Zram to create compressed RAM devices, boosting multitasking limits without physical upgrades.&lt;/li>
&lt;li>Performance Trade-off: Unlike disk-based Swap, Zram trades minor CPU cycles for lightning-fast I/O, perfect for low-end VPS or IoT.&lt;/li>
&lt;li>One-Click Deployment: Use community scripts to deploy a Zram+Swap combo, providing elastic hedging against memory pressure.&lt;/li>
&lt;/ul>
&lt;/blockquote>
&lt;p>Zram is usually used in embedded devices such as Android phones.
For example, in a 2.0GB running memory, there are 1.8 real running memory and 0.2 zram memory to make up the number. Another advantage of zram is that it does not occupy hard disk space or wear hard disk&lt;/p>
&lt;h2 id="the-difference-between-zram-and-swap">
&lt;a href="#the-difference-between-zram-and-swap" class="heading-anchor" aria-label="Anchor for The difference between zRAM and Swap">#&lt;/a>
The difference between zRAM and Swap
&lt;/h2>
&lt;p>zRAM and traditional swap space (Swap) are technologies used by Linux systems to increase available memory, but their working methods and usage scenarios are different.&lt;/p>
&lt;p>Traditional Swap Space (Swap):&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Storage Medium: Swap usually uses a hard disk drive (HDD) or solid-state drive (SSD) as a swap area.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Performance: Because hard disks are much slower than RAM, using swap may significantly slow down the system, especially when memory is exhausted.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Reliability: Swap is on a different physical medium, so it does not affect the life of RAM.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Purpose: It can be used as an overflow area when physical memory is exhausted, but due to its slow speed, it is usually only suitable for very low memory pressure.&lt;/p>
&lt;p>zRAM:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Storage Medium: zRAM is a compressed RAM block device that uses the memory itself to store compressed data.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Performance: zRAM reads and writes much faster than traditional swap because it operates directly on RAM. Compressing and decompressing data requires some CPU resources, but usually this overhead is less than the time to read and write data from the hard disk.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Reliability: Because zRAM uses RAM, frequent reading and writing may affect the life of RAM, although the actual impact is very small.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Purpose: zram is very suitable for devices with limited RAM or that need a fast swap area, such as embedded devices or old computers, and is also commonly used in Android devices.&lt;/p>
&lt;h2 id="summary">
&lt;a href="#summary" class="heading-anchor" aria-label="Anchor for Summary:">#&lt;/a>
Summary:
&lt;/h2>
&lt;p>zRAM provides a way to increase the effective memory capacity without increasing physical memory, by creating a compressed block device in RAM.&lt;/p>
&lt;p>Traditional swap space (Swap) is a part of the hard disk that is used as virtual memory when the system&amp;rsquo;s physical memory is insufficient.&lt;/p>
&lt;p>zram is particularly suitable for systems with small memory because it can add some &amp;ldquo;virtual&amp;rdquo; memory with little performance overhead, although this will increase the burden on the CPU.&lt;/p>
&lt;p>Traditional swap is suitable for situations where there is enough hard disk space and the memory pressure is not very large, especially when the system load is not high, but occasionally more memory is needed.&lt;/p>
&lt;p>The best practice may be to use zRAM and traditional swap together for more flexible and optimized performance. When memory is tight, zRAM can provide faster data exchange, and when zRAM is also exhausted, traditional swap can be used as a last backup.&lt;/p>
&lt;h2 id="one-click-script-setting">
&lt;a href="#one-click-script-setting" class="heading-anchor" aria-label="Anchor for One-click script setting">#&lt;/a>
One-click script setting
&lt;/h2>
&lt;p>&lt;a class="link" href="https://github.com/spiritLHLS/addzram" target="_blank" rel="noopener"
>https://github.com/spiritLHLS/addzram&lt;/a>
&lt;span style="white-space: nowrap;">&lt;svg width=".7em"
height=".7em" viewBox="0 0 21 21" xmlns="http://www.w3.org/2000/svg">
&lt;path d="m13 3l3.293 3.293l-7 7l1.414 1.414l7-7L21 11V3z" fill="currentColor" />
&lt;path d="M19 19H5V5h7l-2-2H5c-1.103 0-2 .897-2 2v14c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2v-5l-2-2v7z"
fill="currentColor">
&lt;/svg>&lt;/span>
&lt;/p>
&lt;p>&lt;a class="link" href="https://github.com/spiritLHLS/addswap" target="_blank" rel="noopener"
>https://github.com/spiritLHLS/addswap&lt;/a>
&lt;span style="white-space: nowrap;">&lt;svg width=".7em"
height=".7em" viewBox="0 0 21 21" xmlns="http://www.w3.org/2000/svg">
&lt;path d="m13 3l3.293 3.293l-7 7l1.414 1.414l7-7L21 11V3z" fill="currentColor" />
&lt;path d="M19 19H5V5h7l-2-2H5c-1.103 0-2 .897-2 2v14c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2v-5l-2-2v7z"
fill="currentColor">
&lt;/svg>&lt;/span>
&lt;/p>
&lt;h3 id="swap-memory-occupies-hard-disk-space">
&lt;a href="#swap-memory-occupies-hard-disk-space" class="heading-anchor" aria-label="Anchor for Swap memory (occupies hard disk space)">#&lt;/a>
Swap memory (occupies hard disk space)
&lt;/h3>
&lt;p>Update time: 2023.08.27&lt;/p>
&lt;p>Add swap partition (virtual memory) for openvz and kvm virtualized linux servers&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">curl -L https://raw.githubusercontent.com/spiritLHLS/addswap/main/addswap.sh -o addswap.sh &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> chmod +x addswap.sh &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> bash addswap.sh
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Added automatic addition of swap when restarting the openvz architecture&lt;/p>
&lt;p>Openvz&amp;rsquo;s addition = stealing the bell from the ears, which is actually due to virtualization restrictions and should not be added. It can only be controlled by the virtualized host. Similarly, LXC virtualization can only be controlled by the host, and virtual memory SWAP cannot be added autonomously&lt;/p>
&lt;p>Unit conversion: input 1024 to generate 1G SWAP memory&lt;/p>
&lt;h3 id="zram-memory-cpu-usage">
&lt;a href="#zram-memory-cpu-usage" class="heading-anchor" aria-label="Anchor for Zram memory (CPU usage)">#&lt;/a>
Zram memory (CPU usage)
&lt;/h3>
&lt;p>Updated: 2023.08.30&lt;/p>
&lt;p>Enable zram device (compressed memory) for linux server&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">curl -L https://raw.githubusercontent.com/spiritLHLS/addzram/main/addzram.sh -o addzram.sh &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> chmod +x addzram.sh &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> bash addzram.sh
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>It’s just that zram is compressed memory usage, and swap is additional virtual memory to increase memory space. Both will occupy CPU resources. zram is better used on machines with redundant CPU performance&lt;/p>
&lt;p>(In theory, zram will have lower performance usage than swap, but it has not been actually tested)&lt;/p>
&lt;p>Unit conversion: Enter 1024 to generate 1G of zram device compressed memory. The size of the zram device should not be larger than the actual memory size.&lt;/p></description></item></channel></rss>