[Defy Android Development] YetAnotherForumUser:
No need for a swap app

DISCLAIMER: As to whether you'd want to use swap on a mobile, battery-constrained, flash-storage-based device I'll leave in the middle. I don't feel the need for it, but if you happen to need more memory to run some superheavy app it might come in handy.

Don't forget that Android is a Linux distribution (if an odd one) and comes with the usual tools to do systems administration. This includes creating, mounting and releasing swap space. To create swap space you get yourself a shell (through a terminal app or remote through adb) and use dd (to create an empty file) and mkswap (to format it):

~ # dd if=/dev/zero of=/mnt/sdcard/swap bs=1024 count=524288
524288+0 records in
524288+0 records out
536870912 bytes (512.0MB) copied, 74.874878 seconds, 6.8MB/s
~ # mkswap -L swap_on_SD /mnt/sdcard/swap
Setting up swapspace version 1, size = 536866816 bytes
~ #

Notice that dd tells you the writing speed (on a class 10 card (Samsung 32GB)) is only 6.8 MB/s? Using swap on an SD-card is *s*l*o*w*. Using internal eMMC flash storage (eg. /data/local/swap) is not much faster and risks wearing out the non-replaceable internal storage.

One more remark about that app you mentioned. If the values you show are the default values I wonder what the author was thinking when he set swappiness to 100 (meaning 'swap as much as possible'). On a slow, battery-powered flash storage device you want to swap as little as possible as it is much better to purge the page cache than to swap out working storage. I'd set swappiness to a low value (eg. 10 or lower) instead which gives better interactive performance, fewer writes to the swap device and with that lower power consumption. This is a contentious issue (to swap or not to swap, how much to swap, etc) but given the (lack of) speed of the swap device there should not be any doubt here - to swap equals to crawl. As to the option to create 3GB of swap I can be short: nuts. Use it if you want to use your phone as a hand warmer on cold, wintery days...

To actually use the swap file you'd use swapon. To show the effect of this command, I'll precede and follow it with some memory info, and I'll lower swappiness (from the default of 60) to10 before mounting swap:

~ # cat /proc/meminfo |grep -i swap
SwapCached: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
~ # sysctl -w vm.swappiness=10
vm.swappiness = 10
~ # swapon /mnt/sdcard/swap
~ # cat /proc/meminfo |grep -i swap
SwapCached: 0 kB
SwapTotal: 524280 kB
SwapFree: 524280 kB
~ #

I would not use swap full-time, only when it is really needed. Once the need for extra memory (and with that, swap space) has expired you can u(n)mount the swap space using the swapoff command (again preceded and followed by some memory info to show the effect of the command):

~ # cat /proc/meminfo |grep -i swap
SwapCached: 0 kB
SwapTotal: 524280 kB
SwapFree: 524280 kB
~ # swapoff /mnt/sdcard/swap
~ # cat /proc/meminfo |grep -i swap
SwapCached: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
~ #

To use swap on a regular basis you'd want to create a few scripts to create, mount and u(n)mount your swap file(s) and some icons to launch them. I'll leave that as an exercise to the reader. Maybe it could be added as a configurable, off by default memory tuning parameter?