Skip to content

createswap

Manager for ZRAM + swapfile with a status panel, dry-run (--dry-run), automatic rollback, and a safe Magisk boot hook.

Requires root

Reads /proc/swaps, /sys/block/zram*, writes to /data/swapfile, and configures ZRAM via sysfs โ€” all root via su -c or sudo.

Golden rule

ZRAM (priority 100) is used first; the swapfile (priority 10) only afterwards.

This means: rarely-used pages go to ZRAM (compressed in RAM, fast) and only overflow to the swapfile in /data (slow but persistent and bigger) when ZRAM fills up.

Basic usage

createswap                      # interactive menu with the panel
createswap --status             # just the panel
createswap --setup-zram 50%     # ZRAM with 50% of RAM (priority 100)
createswap --setup-swap 1G -n   # simulate a 1 GB swapfile (don't apply)
createswap --setup-zram 1G --setup-swap 2G --install-boot   # apply and re-apply on boot
createswap --clear              # remove the swapfile
createswap --clear zram         # remove only ZRAM
createswap --clear all          # remove everything
createswap --uninstall-boot     # remove the Magisk boot hook

The panel (--status)

                         ๐Ÿง  RAM
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Total                โ”‚ 1.81 GB                               โ”‚
โ”‚ In use               โ”‚ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 58%    โ”‚
โ”‚ Available            โ”‚ 781 MB (42%)                          โ”‚
โ”‚ Free / cache         โ”‚ 146 MB / 508 MB                       โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
                            ๐Ÿ“ฆ ZRAM
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ State                โ”‚ โœ… Active                              โ”‚
โ”‚ Device               โ”‚ /dev/block/zram0                      โ”‚
โ”‚ Algorithm            โ”‚ lz4                                   โ”‚
โ”‚ Supported            โ”‚ lzo, lzo-rle, lz4, zstd               โ”‚
โ”‚ Size                 โ”‚ 1.00 GB                               โ”‚
โ”‚ Usage                โ”‚ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘ 29%    โ”‚
โ”‚ Priority             โ”‚ -2                                    โ”‚
โ”‚ Data โ†’ compressed    โ”‚ 286 MB โ†’ 95 MB (3.0x)                 โ”‚
โ”‚ RAM consumed         โ”‚ 105 MB                                โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
                          ๐Ÿ’พ Swapfile
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ File                        โ”‚ /data/swapfile                 โ”‚
โ”‚ State                       โ”‚ โŒ Does not exist              โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
                           ๐Ÿ”ง System
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ swappiness                  โ”‚ 60                             โ”‚
โ”‚ /data       free            โ”‚ 12.00 GB (50%)                 โ”‚
โ”‚ Swapfile max                โ”‚ 4.00 GB (40% free)             โ”‚
โ”‚ Memory pressure             โ”‚ 1.5% (avg10)                   โ”‚
โ”‚ Boot hook                   โ”‚ not installed                  โ”‚
โ”‚ Kernel                      โ”‚ 4.14.199                       โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Fields explained

Field What it measures
Total / In use / Available Physical RAM; MemAvailable from /proc/meminfo
Free / cache MemFree / Cached โ€” real buffers, reclaimable
Algorithm ZRAM compressor (lz4 recommended; zstd compresses more but uses more CPU)
Data โ†’ compressed Bytes written to ZRAM vs. actual bytes in RAM โ€” the ratio is the gain
RAM consumed How much RAM ZRAM is actually using (decompressed + metadata)
swappiness How much the kernel prefers swap vs. discarding cache (default 60)
Swapfile max Swapfile size limit โ€” 40% of free space in /data
Memory pressure psi mem avg10 โ€” how stressed memory has been in the last 10 s
Boot hook Whether the Magisk service that re-applies swap on boot is installed

Detailed flags

--setup-zram <size>

Creates and activates the ZRAM device. <size> accepts % (percent of RAM), G, M, or raw bytes.

createswap --setup-zram 50%       # half of RAM (recommended for 2 GB)
createswap --setup-zram 1G        # 1 GB fixed
createswap --setup-zram 1G --algo zstd   # use zstd instead of lz4

Always uses priority 100 โ€” first in the swap queue.

--setup-swap <size>

Creates a swapfile at /data/swapfile. Limited to 40% of free space in /data โ€” if you ask for more, createswap recalculates and warns.

createswap --setup-swap 1G
createswap --setup-swap 2G -n     # simulate

Always uses priority 10 โ€” only kicks in when ZRAM fills up.

--install-boot

Installs a Magisk boot hook (/data/adb/post-fs-data.d/createswap.sh) that re-applies ZRAM + swapfile on every boot. Idempotent โ€” if already installed, just updates the config.

--clear [target]

Removes what was configured. No argument: only the swapfile. With zram: only ZRAM. With all: everything + removes the boot hook.

Before any swapoff, createswap checks if there's enough free RAM to receive the pages that were in swap. If not, aborts with a warning instead of OOM-killing the device.

Boot hook (Magisk)

Anti-bootloop behavior:

  1. waits for the system to come up (while ! getprop sys.boot_completed);
  2. reads the saved config from /data/local/tmp/createswap.cfg;
  3. applies ZRAM + swapfile in the correct order;
  4. logs errors to /data/local/tmp/createswap-boot.log;
  5. always ends with exit 0 โ€” never breaks the boot.

How to disable

Three paths, from most elegant to most brute:

createswap --uninstall-boot                              # remove the hook
touch /data/local/tmp/createswap.disable                 # flag: hook skips
rm /data/adb/post-fs-data.d/createswap.sh                # manual removal

The createswap.disable flag is useful for diagnostics: the hook stays installed but does nothing. Remove the file to re-enable.

Rollback

If any step of --setup-zram / --setup-swap fails mid-way, createswap undoes what it had already done:

  • created swapfile but mkswap failed? โ€” deletes the file.
  • enabled swapfile but ZRAM swapon failed? โ€” swapoff the swapfile first.
  • everything failed after changing swappiness? โ€” restores the previous value.

Autocompletion

pip install argcomplete
createswap --completion

Afterwards, TAB completes --setup-zram, --setup-swap, sizes, --clear zram, etc.

Recommendations by device profile

RAM Recommended ZRAM Swapfile Why
1 GB 50% (512 MB) 1 GB Android Go needs headroom for apps
2 GB 50% (1 GB) 1โ€“2 GB Sweet spot for SSH server + Python
3 GB 33% (1 GB) optional ZRAM usually suffices
4+ GB 25% not needed The kernel manages fine on its own

See also

  • Architecture โ€” diagram of where swap fits in the stack.
  • Glossary โ€” ZRAM, swapfile, swappiness, lz4 vs zstd.
  • Troubleshooting โ€” swap not activating, bootloop after install-boot.