Skip to content

Commit 06f38ff

Browse files
committed
raspbiantools: Add option to enable compressed memory (ZRAM)
Compressed memory (ZRAM) can be used with low memory embedded devices. ZRAM has compression ratio of up to 4:1 if zstd algorithm is used. 256MB of reserved physical memory can store up to 1024MB. It is only used if free memory is getting low. It is much faster than disk swapping. It also prevents linux from using disk swap until ZRAM is full. https://wiki.debian.org/ZRam https://wiki.ubuntuusers.de/zRam/ https://haydenjames.io/raspberry-pi-performance-add-zram-kernel-parameters/ Add an option which enables ZRAM with a swap size of 50% of physical memory size. Example: Raspberry Pi4, 2GB, RetroPie 4.8 Meminfo without ZRAM pi@retropie:~ $ cat /proc/meminfo MemTotal: 1917108 kB MemFree: 1567396 kB MemAvailable: 1759312 kB Buffers: 24592 kB Cached: 240928 kB SwapCached: 0 kB Active: 115120 kB Inactive: 170404 kB Active(anon): 616 kB Inactive(anon): 29056 kB Active(file): 114504 kB Inactive(file): 141348 kB Unevictable: 16 kB Mlocked: 16 kB HighTotal: 1232896 kB HighFree: 959980 kB LowTotal: 684212 kB LowFree: 607416 kB SwapTotal: 102396 kB SwapFree: 102396 kB Dirty: 30800 kB Writeback: 0 kB AnonPages: 20052 kB Mapped: 32344 kB Shmem: 9668 kB KReclaimable: 18456 kB Slab: 35056 kB SReclaimable: 18456 kB SUnreclaim: 16600 kB KernelStack: 1136 kB PageTables: 1796 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 1060948 kB Committed_AS: 112832 kB VmallocTotal: 245760 kB VmallocUsed: 5208 kB VmallocChunk: 0 kB Percpu: 576 kB CmaTotal: 327680 kB CmaFree: 313280 kB Meminfo with 50% ZRAM pi@retropie:~ $ cat /proc/meminfo MemTotal: 1917108 kB MemFree: 1604616 kB MemAvailable: 1676408 kB Buffers: 20880 kB Cached: 152212 kB SwapCached: 0 kB Active: 45144 kB Inactive: 149896 kB Active(anon): 632 kB Inactive(anon): 55996 kB Active(file): 44512 kB Inactive(file): 93900 kB Unevictable: 25004 kB Mlocked: 16 kB HighTotal: 1232896 kB HighFree: 1020208 kB LowTotal: 684212 kB LowFree: 584408 kB SwapTotal: 1060940 kB SwapFree: 1060940 kB Dirty: 72 kB Writeback: 0 kB AnonPages: 46988 kB Mapped: 70232 kB Shmem: 34672 kB KReclaimable: 13088 kB Slab: 28820 kB SReclaimable: 13088 kB SUnreclaim: 15732 kB KernelStack: 1248 kB PageTables: 2124 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 2019492 kB Committed_AS: 177464 kB VmallocTotal: 245760 kB VmallocUsed: 7412 kB VmallocChunk: 0 kB Percpu: 576 kB CmaTotal: 327680 kB CmaFree: 283624 kB
1 parent 47c7f13 commit 06f38ff

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

scriptmodules/supplementary/raspbiantools.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,46 @@ function enable_modules_raspbiantools() {
125125
done
126126
}
127127

128+
function enable_zram_raspbiantools() {
129+
if [[ "$__os_id" == "Raspbian" ]] || [[ "$__os_id" == "Debian" ]]; then
130+
aptInstall zram-tools
131+
# Use 50% of the current memory for ZRAM
132+
local percent="50"
133+
iniConfig "=" "" "/etc/default/zramswap"
134+
# Raspbian Buster uses keyword PERCENTAGE
135+
iniSet "PERCENTAGE" "$percent"
136+
# Debian Bullseye/Bookworm use keyword PERCENT
137+
iniSet "PERCENT" "$percent"
138+
# Use zstd compression algorithm if kernel supports it
139+
[[ -f /sys/class/block/zram0/comp_algorithm ]] && [[ "$(cat /sys/class/block/zram0/comp_algorithm)" == *zstd* ]] && iniSet "ALGO" "zstd"
140+
service zramswap stop
141+
service zramswap start
142+
elif [[ "$__os_id" == "Ubuntu" ]]; then
143+
aptInstall zram-config
144+
# Ubuntu has a automatic zram configuration
145+
fi
146+
}
147+
148+
function disable_zram_raspbiantools() {
149+
if [[ "$__os_id" == "Raspbian" ]] || [[ "$__os_id" == "Debian" ]]; then
150+
apt remove -y zram-tools
151+
elif [[ "$__os_id" == "Ubuntu" ]]; then
152+
apt remove -y zram-config
153+
fi
154+
}
155+
128156
function gui_raspbiantools() {
129157
while true; do
158+
local zram_status="Enable"
159+
[[ $(cat /proc/swaps) == *zram* ]] && zram_status="Disable"
130160
local cmd=(dialog --backtitle "$__backtitle" --menu "Choose an option" 22 76 16)
131161
local options=(
132162
1 "Upgrade Raspbian packages"
133163
2 "Install Pixel desktop environment"
134164
3 "Remove some unneeded packages (pulseaudio / cups / wolfram)"
135165
4 "Disable screen blanker"
136166
5 "Enable needed kernel module uinput"
167+
6 "$zram_status compressed memory (ZRAM)"
137168
)
138169
local choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
139170
if [[ -n "$choice" ]]; then
@@ -155,6 +186,9 @@ function gui_raspbiantools() {
155186
5)
156187
rp_callModule "$md_id" enable_modules
157188
;;
189+
6)
190+
[[ "$zram_status" == "Enable" ]] && rp_callModule "$md_id" enable_zram || rp_callModule "$md_id" disable_zram
191+
;;
158192
esac
159193
else
160194
break

0 commit comments

Comments
 (0)