If you want to use zram swap on Artix Linux without relying on zramen or zramctl, here’s a dinit service setup that initializes a zram device at boot and cleans it up on stop, using the kernel sysfs interface directly.
Why Use zram?
zram creates a compressed block of memory that acts like swap or a temporary drive. Instead of writing less-used data to your slower SSD or hard drive, the system compresses it and keeps it in RAM. This gives you more effective memory, keeps things faster, and reduces disk wear.
When to Use It:
- Systems with 2–8 GB RAM – Helps keep your system responsive under load by compressing memory instead of using slow disk swap
- SSD-based systems – Reduces swap-related disk writes, helping to extend the lifespan of your SSD
- Systems without a swap partition – Easy way to add swap-like functionality without touching your disk
- Embedded devices and containers – Saves memory and improves performance in resource-constrained environments
---
1. Optional configuration file
# /etc/dinit.d/config/zram.conf
# zram configuration
ZRAM_DEVICE=/dev/zram0
ZRAM_ALGO=zstd
---
2. Dinit service
# /etc/dinit.d/zram
type = scripted
command = /usr/bin/init-zram.sh
stop-command = /usr/bin/stop-zram.sh
env-file = /etc/dinit.d/config/zram.conf
logfile = /var/log/dinit/zram.log
depends-on = early-devices.target
after = early-fs-local.target
---
3. Initialization script
#!/bin/sh
# /usr/bin/init-zram.sh (manual sysfs version)
# Load zram module
modprobe zram || { echo "Failed to load zram module" >&2; exit 1; }
# Add a zram device (usually /dev/zram0)
echo 1 > /sys/class/zram-control/hot_add
# Default values
ZRAM_DEVICE=${ZRAM_DEVICE:-/dev/zram0}
ZRAM_ALGO=${ZRAM_ALGO:-zstd}
# Determine zram device number from path
ZRAM_NUM=$(basename "$ZRAM_DEVICE" | sed 's/zram//')
# Configure device
echo "$ZRAM_ALGO" > "/sys/block/zram$ZRAM_NUM/comp_algorithm"
# Set size to half of RAM
SIZE_KB="$(($(grep -Po 'MemTotal:\s*\K\d+' /proc/meminfo)/2))"
echo "${SIZE_KB}K" > "/sys/block/zram$ZRAM_NUM/disksize"
# Format as swap and enable
mkswap -U clear "$ZRAM_DEVICE"
swapon --discard --priority 100 "$ZRAM_DEVICE"
---
4. Stop script
#!/bin/sh
# /usr/bin/stop-zram.sh
# Disable zram swap and unload module
swapoff "${ZRAM_DEVICE:-/dev/zram0}"
modprobe -r zram
---
5. Make scripts executable
sudo chmod +x /usr/bin/init-zram.sh
sudo chmod +x /usr/bin/stop-zram.sh
---
Notes:
- Defaults: /dev/zram0 and zstd compression, configurable via /etc/dinit.d/config/zram.conf.
- Logs are written to /var/log/dinit/zram.log.
- Starts after early devices and filesystem are ready.
- No external wrapper (zramen) or zramctl is required—everything uses kernel interfaces directly.
- Enable the service with: sudo dinitctl enable zram
- Check if it’s working with:
swapon --show
cat /sys/block/zram0/comp_algorithm
cat /sys/block/zram0/disksize