ZRAM expansion is the best option for expanding memory for Linux systems that for lower performance Cortex-A53 series within 1 GiB system memory.
How to activate ZRAM low performance systems as like Cortex-A53
A bah script file for ZRAM, zram.sh
is here,
#!/bin/bash export LANG=C export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" if [ "$1" == --help ] || [ "$1" == -h ];then echo -e "This is a script made by Botspot to increase usable RAM by setting up ZRAM. ZRAM uses compression to fit more memory in your available RAM. This script will setup a ZRAM swapspace that is 4 times larger than usable RAM. It also configures high-speed RAM-based file-storage at /zram. Usage: sudo zram.sh Setup zram-swap and storage (if enabled) sudo zram.sh stop Disable all ZRAM devices and exit sudo zram.sh storage-off Disable the file-storage at /zram on next run sudo zram.sh storage-on Enable the file-storage at /zram on next run zram.sh --help, -h Display this information and exit" exit 0 fi if [ $(id -u) -ne 0 ]; then echo "$0 must be run as root user" exit 1 fi #avoid creating /zram storage if storage-off flag passed if [ "$1" == storage-off ]; then #retain this flag for next boot if ! grep -qxF "ExecStart=/usr/bin/zram.sh storage-off" /etc/systemd/system/zram-swap.service ;then sed -i "s+^ExecStart=/usr/bin/zram.sh$+ExecStart=/usr/bin/zram.sh storage-off+g" /etc/systemd/system/zram-swap.service fi echo -e "zram.sh will not set up file-storage at /zram from now on." #the /zram storage can be re-enabled with storage-on flag elif [ "$1" == storage-on ]; then sed -i "s+^ExecStart=/usr/bin/zram.sh storage-off$+ExecStart=/usr/bin/zram.sh+g" /etc/systemd/system/zram-swap.service echo -e "zram.sh will set up file-storage at /zram from now on." fi # Load zram module if ! lsmod | awk "{print $1}" | grep -qxF zram ;then if ! modprobe zram ;then echo "Failed to load zram kernel module" exit 1 fi fi # disable all zram devices echo -n "Disabling zram... " IFS=$'\n' for device_number in $(find /dev/ -name zram* -type b | tr -cd "0123456789\n") ;do #if zram device is a swap device, disable it swapoff /dev/zram${device_number} 2>/dev/null #if zram device is mounted, unmount it umount /dev/zram${device_number} 2>/dev/null #remove device echo $device_number >/sys/class/zram-control/hot_remove done echo Done rm -rf /zram #exit script now if "exit" flag passed if [ "$1" == stop ]; then exit 0 fi #create new zram drive - for swap drive_num=$(cat /sys/class/zram-control/hot_add) # use zstd compression if available - best option according to https://linuxreviews.org/Comparison_of_Compression_Algorithms#zram_block_drive_compression if cat /sys/block/zram${drive_num}/comp_algorithm | grep -q zstd ;then algorithm=zstd else algorithm=lz4 fi echo $algorithm > /sys/block/zram${drive_num}/comp_algorithm totalmem=$(free | grep -e "^Mem:" | awk '{print $2}') #create zram disk 4 times larger than usable RAM - compression ratio for zstd can approach 5:1 according to https://linuxreviews.org/Zram echo $((totalmem * 1024 * 4)) > /sys/block/zram${drive_num}/disksize #make the swap device (by default this will be /dev/zram0) mkswap /dev/zram${drive_num} swapon /dev/zram${drive_num} -d -p 1 #create second zram drive: for temporary user-storage at /zram if ! grep -qxF "ExecStart=/usr/bin/zram.sh storage-off" /etc/systemd/system/zram-swap.service ;then echo "Setting up ZRAM-powered file storage at /zram" #create new zram drive drive_num=$(cat /sys/class/zram-control/hot_add) # set compression algorithm echo $algorithm > /sys/block/zram${drive_num}/comp_algorithm #set the size of drive to be 4 times the available RAM echo $((totalmem * 1024 * 4)) > /sys/block/zram${drive_num}/disksize #create a partition and mount it mkfs.ext4 /dev/zram${drive_num} >/dev/null mkdir -p /zram mount /dev/zram${drive_num} /zram chmod -R 777 /zram #make writable for any user fi
And put, or write script to /usr/local/bin
or /usr/bin
with permission executable as like chmod 775
.
Enable ZRAM swap
- Required zram module and zramctl must be pre-installed on Linux system (non-installable from apt).
- Disable dphy or swap from file.
sudo /usr/sbin/dphys-swapfile uninstall
orsudo systemctl disable mkswap.service
. - And make swap service to be disabled by ..
sudo systemctl mask dphys-swapfiles.service
orsudo systemctl mask mkswap.service
- Enable ZRAM swap
sudo enable_module zram
- Make zram.sh in /usr/bin with upper content, and make it runnable with sudo chmod 775 /usr/bin/zram.sh.
- Make zram service script with this content in /etc/systemd/system/zram-swap.service.
Add these lines in /etc/sysctl.conf
# ZRAM configuration vm.swappiness=100 vm.vfs_cache_pressure=500 vm.dirty_background_ratio=1 vm.dirty_ratio=50
This setting is recommended for zram best performance.
Some trick for slower systems
ZRAM algorithm zstd
is the best compression but slower than lz4 and lzo. You can update zram.sh script to select lz4 first then lzo, later for std from line 60 to …
if cat /sys/block/zram${drive_num}/comp_algorithm | grep -q lz4 ;then algorithm=lz4 elif cat /sys/block/zram${drive_num}/comp_algorithm | grep -q lzo ;then algorithm=lzo else algorithm=zstd fi