swapfileIncreasing memory for processes by replacing zRAM with swapfile on internal storage
Take a backup of your system partition and set a custom recovery for your phone with this guide so that you can restore your system partition back to its original state in the case of failure.
Being the successor of the discontinued Firefox OS, KaiOS relies on the Gecko browser engine to run apps based on light-weight web technologies. As such, it delivers comparable performance to other fully-featured operating systems, despite having as much as 1GB of memory; and unlike basic OSes like S30+, it can also handle multiple processes at once.
However, likewise with other OSes, there's a limit on how much the phone can handle before it starts killing processes to avoid memory overflow; by default it nearly reaches that limit, with the Linux kernel already takes around 200–250MB. To resolve this issue, KaiOS compresses some of the RAM data into a zRAM image that resides within the RAM itself. While the compressed zRAM is faster as it directly uses the RAM, it reserves about half the space, leaving just the other half for active tasks. Once the memory is full, a daemon called Low Memory Killer kicks in and terminates processes in the order of priority.
Creating a swapfile on your phone's internal storage and direct the system to use the swapfile instead of in-built zRAM frees up that half of your RAM, leaving room for more processes and possibly make the phone faster and more capable—though you'll have to trade off a tiny degradation in speed, some storage space and storage chip longevity.
#!/system/bin/sh
#################################################################
# uncomment the following 2 lines to enable existing swapfile...
#################################################################
swapon /data/opt/swapfile
swapoff /dev/block/zram0
#################################################################
busybox telnetd -l /system/bin/sh -b 127.0.0.1
Get a temporary root access. Remember, the following commands are written if you use Telnetd app, but you can get root in a easier way using Wallace and ADBRoot. Using them you don't need to write busybox or busybox telnet localhost and just write exit one time.
Create the swapfile and push the file init into a new folder:
adb shell
busybox telnet localhost
mount -o remount,rw /system
mount -o remount,rw /data
busybox mkdir /data/opt
busybox dd if=/dev/zero of=/data/opt/swapfile bs=1024 count=524288
busybox mkswap /data/opt/swapfile
exit
exit
adb push init /data/opt/init
adb shell chmod +x /data/opt/init
NOTE: This will interrupt system updates, so make a backup of the "system" partition so that you can restore everything in the future, when necessary.
adb shell
busybox telnet localhost
mount -o remount,rw /system
echo '/data/opt/init & ###bootstrap###' >> /system/etc/init.qcom.post_boot.sh
sync
mount -o remount,ro /system
reboot
To verify that everything works correctly, run from the terminal:
adb shell
B2G-info
Check that the "SwapTotal" item corresponds to the value 512.0 MB:
SwapTotal = 256.0 MB value, in this case the procedure must be repeated, surelly some steps have been skipped, the swapfile doesn't work.
SwapTotal is the value in MB of the swapfile size. Commands for using a swapfile instead of zram exists but aren't enabled.
Zram is swap as a compressed ramdisk - fast but it's reserving about half of the physical RAM (practically while using your device convinced to have 512 MB of RAM available in reality you are using only 256, or even less considering the system processes).
If you use a swapfile instead of Zram you have more physical RAM for caches and usable RAM is also bigger (apps aren't killed for low-memory as often as without swap).