[ASUS ZenFone 8] bnsmb:
How to create or change a swap device in the OmniROM using Magisk

The init script in the OmniROM running on the ASUS Zenfone 8 to create a swap device is:

/vendor/bin/init.asus.zram.sh

This script will create a swap device while booting the OS if that functionality is enabled.

To check if there is a swap device configured use the OS command free:

root@ASUS_I006D:/ # free
total used free shared buffers
Mem: 7612493824 4732076032 2880417792 12857344 15298560
-/+ buffers/cache: 4716777472 2895716352
Swap: 4294963200 0 4294963200
root@ASUS_I006D:/ #

If the result of the script is not okay you can create a Magisk script to correct the config for the swap device.

The script /vendor/bin/init.asus.zram.sh uses the property

vendor.zram.enable

to determine if a swap device should be created and the property

vendor.zram.disksize

for the (new) size for the swap device.

If the property vendor.zram.enable is set to 1 but the property vendor.zram.disksize is not set the script uses a default value for the size of the swap device depending on the amount of installed memory.

To create a swap device via a Magisk Script (or change the config of the swap device created by default) it's sufficient to just set the two properties to the approbiate values and execute the init script , e.g.

Note:

This must be done as user root.

cat >/data/adb/service.d/0010create_swap_device.sh <<EOT
#!/system/bin/sh

# there is a missing leading slash (/) in one of the commands in the file /vendor/bin/init.asus.zram.sh
# therefor the working directory when executing the script must be /
#
cd /

#
# enable creating the swap device
#
setprop vendor.zram.enable 1

#
# use a 512 MB swap device
# The default unit for vendor.zram.disksize is bytes; use the prefix M for megabytes or G for gigabytes
#
#
# Do not set this property to use the default value for the swap device (which is depending on the installed memory)
#
setprop vendor.zram.disksize 512M

#
# now (re)create the swap device
#
/vendor/bin/init.asus.zram.sh

EOT

chmod 755 /data/adb/service.d/0010create_swap_device.sh

To disable the swap device at all use this Magisk script:

cat >/data/adb/service.d/0010disable_swap_device.sh <<EOT
#!/system/bin/sh

# there is a missing leading slash (/) in one of the commands in the file /vendor/bin/init.asus.zram.sh
# therefor the working directory when executing the script must be /
#
cd /

#
# enable creating the swap device (-> necessary to delete an existing swap device)
#
setprop vendor.zram.enable 1

# new size for the swap device (0 = do not configure a swap device)
#
setprop vendor.zram.disksize 0

#
# now (re)create the swap device (-> only delete an existing swap device)
#
/vendor/bin/init.asus.zram.sh

EOT

chmod 755 /data/adb/service.d/0010disable_swap_device.sh

Notes:

See https://xdaforums.com/t/how-to-disa...droid-12-from-asus-for-the-zenfone-8.4495085/ for how to reconfigure swap in the Original Android 12 from ASUS for the Zenfone 8

Please note that the device /dev/block/zram0 is created in the RAM but the memory for the device /dev/block/zram0 device is only allocated on demand - e.g.:

#
# free memory with a free swap device on zram0 with 6 GB is about 2783735808 (about 2 GB)
#
root@ASUS_I006D:/ # free
total used free shared buffers
Mem: 7612489728 4828753920 2783735808 13639680 15556608
-/+ buffers/cache: 4813197312 2799292416
Swap: 6442446848 0 6442446848
root@ASUS_I006D:/ #
root@ASUS_I006D:/ # setprop vendor.zram.disksize 0

root@ASUS_I006D:/ # /vendor/bin/init.asus.zram.sh
root@ASUS_I006D:/ #

#
# free memory with no swap device on zram0 is about 2802741248 (about 2 GB)
#
root@ASUS_I006D:/ # free
total used free shared buffers
Mem: 7612489728 4809748480 2802741248 13750272 15577088
-/+ buffers/cache: 4794171392 2818318336
Swap: 0 0 0
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # setprop vendor.zram.disksize 4G
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # /vendor/bin/init.asus.zram.sh
Swapspace size: 4194300k, UUID=4a914375-e28f-4181-850a-87cc792eacbe
root@ASUS_I006D:/ #

#
# free memory with a free swap device on zram0 with 4 GB is about 2773835776 (about 2 GB)
#
root@ASUS_I006D:/ # free
total used free shared buffers
Mem: 7612489728 4838653952 2773835776 13877248 15581184
-/+ buffers/cache: 4823072768 2789416960
Swap: 4294963200 0 4294963200
root@ASUS_I006D:/ #

Update 04.10.2022 /bs

See How to trigger an action when a property is changed for how to implement enabling or disabling the swap device on ZRAM via a property value in the OmniROM or another OS