Skip to content

Commit 01ec4b9

Browse files
klutchellbasejump-app
authored andcommitted
Adjust host zram percentage of total memory
To alleviate memory pressure, increase zram to 50% or a given value of total memory at runtime. This allows the host to compress inactive pages to reduce contention. This service optionally allows for the maximum zram percentage and compression algorithm to be modified at runtime using variables. Change-type: minor Signed-off-by: Kyle Harding <kyle@balena.io>
1 parent b4251dc commit 01ec4b9

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docker-compose.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,61 @@ services:
209209
labels:
210210
io.balena.features.dbus: '1'
211211

212+
zram:
213+
image: alpine:3.22
214+
restart: no
215+
privileged: true
216+
labels:
217+
io.balena.features.procfs: '1'
218+
entrypoint:
219+
- /bin/sh
220+
- -c
221+
environment:
222+
ENABLED: true
223+
ZRAM_PCT: 50
224+
ZRAM_ALGO: lz4
225+
command:
226+
- |
227+
set -ex
228+
229+
case ${ENABLED} in
230+
true|1|y|yes|on)
231+
;;
232+
*)
233+
exit 0
234+
;;
235+
esac
236+
237+
apk add --no-cache util-linux-misc
238+
239+
echo "Disabling existing swap devices..."
240+
swaps=$(awk 'NR>1 { print $1 }' /proc/swaps)
241+
242+
if [ -n "${swaps}" ]; then
243+
for swap in ${swaps}; do
244+
# Swap devices reported by /proc in the container are missing the /dev/ prefix
245+
echo "Disabling swap device /dev${swap}..."
246+
swapoff /dev${swap}
247+
done
248+
fi
249+
250+
echo "Removing existing zram devices..."
251+
zrams=$(ls /dev/zram* 2>/dev/null || true)
252+
if [ -n "${zrams}" ]; then
253+
for d in ${zrams}; do
254+
echo "Removing zram device ${d}..."
255+
zramctl -r ${d}
256+
done
257+
fi
258+
259+
echo "Creating a new zram device with ${ZRAM_PCT}% of total memory..."
260+
memtotal=$(grep MemTotal /proc/meminfo | awk '{ print $2 }')
261+
zram_size=$((memtotal * ${ZRAM_PCT} / 100))
262+
zram_dev=$(zramctl --find --size ${zram_size}K --algorithm ${ZRAM_ALGO})
263+
264+
echo "Creating a swap device on the zram block device and enabling it..."
265+
mkswap ${zram_dev} && swapon ${zram_dev}
266+
212267
# create DNS record
213268
upsert-dns:
214269
image: bash:alpine3.14

0 commit comments

Comments
 (0)