🛠 Survivre avec 512 Mo : zram, swap compressé et services légers

Banana Pi BPI-M2 Zero (Allwinner H2+ / H3) · intermediaire · Schéma : IMG-bananapi-m2-zero-intermediaire-3-zram-memoire-512mo-m2-zero.svg — BPI-M2 Zero avec jauge de RAM à moitié pleine ; diagramme en médaillon : la RAM se scinde en applications + bloc zram compressé utilisé comme swap, la microSD reste à l'écart du swap.

Objectives

512 MB of DDR3 shared with the Mali-400 GPU: this is the ceiling for the BPI-M2 Zero, and the reference review that ran Geekbench 5 on the board had to enable zram and push it to 350 MB to complete the measurement. zram creates a compressed swap block in RAM: you gain bytes without wearing out the microSD (the board's only storage, even more precious here than elsewhere). You will set up zram properly, lighten the services, and verify that the board handles a real load.

Hardware

Complete code

# 1) État des lieux : y a-t-il déjà un swap ? (ne pas doubler les dispositifs)
free -h && swapon --show && lsmod | grep zram

# 2) Créer un zram de 384 Mo en LZ4 et l'activer comme swap
sudo modprobe zram
echo lz4 | sudo tee /sys/block/zram0/comp_algorithm
sudo zramctl /dev/zram0 --size 384M
sudo mkswap /dev/zram0 && sudo swapon /dev/zram0

# 3) Le rendre résistant au reboot via un service systemd
sudo tee /etc/systemd/system/zram-swap.service >/dev/null <<'EOF'
[Unit]
Before=swap.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh -c 'modprobe zram && echo lz4 > /sys/block/zram0/comp_algorithm && zramctl /dev/zram0 --size 384M && mkswap /dev/zram0 && swapon /dev/zram0'
ExecStop=/bin/sh -c 'swapoff /dev/zram0 && zramctl /dev/zram0 --reset'
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now zram-swap.service

# 4) Alléger : désactiver ce qui est inutile sur un rôle précis
systemctl list-unit-files --state=enabled | head -30
sudo systemctl disable --now bluetooth    # si le BT ne sert pas

# 5) Épreuve de feu
sudo apt install -y stress-ng && stress-ng --vm 2 --vm-bytes 200M --timeout 60s
free -h && dmesg | tail    # aucun OOM-killer sur sshd

Expected verification

swapon --show lists /dev/zram0 (high priority, no file on the SD); during stress-ng, swap fills up but the SSH session remains alive and dmesg does not report the OOM-killer; zramctl displays the actual compression ratio (often 3 to 4×).

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials