[LG Optimus 2x] Raum1807:
[DEV-HINT] Implementing a swap partition or swap file for new and old bootloader

Wikipedia defines swap file (or paging) as: "[..] use disk storage for data that does not fit into physical random-access memory (RAM)."

With CM10 or CM10.1 around, the 'about phone' screen usually shows 343 MB of usable RAM on the LG-P990, so one gets ideas. With CM9 it was a bit better, but still.... Take Chrome for example: nice to the eye, but not so nice to the RAM of devices that have been around a lot longer than the Nexus 4 with its whopping two Gigabytes. So what's the catch with swap? For example, a HDD swap partition on a PC offers some sort of relief when RAM is scarce by moving some parts of the RAM (pages) to the HDD and moving them back when it needs it. Of course, it comes for a price: performance! But it works. So why not using it on Android too? To hell with the fact that virtual RAM on the internal memory card is a lot slower than physical RAM, there are times when it would be just nice to have the extra megabytes at hand.

Were you a bit envious too when you saw that the 'new' partition layout that came with the ICS bootloader on our beloved LG-P990 had a swap partition included? Well, I was. I even changed to a customized layout to get one for a little while too... But that is not really necessary. Never was, actually. I came back to the original layout and old bootloader weeks ago. What still bugged me was the missing swap partition. CM10.1, CM10 or even CM9 could really use swap from time to time but commonly don't use it.

So, is there really a big difference between the two partition layouts? Yes, but regarding the swap it doesn't count. In fact, the situation for both user groups is pretty much the same: both do not use any kind of swap space whatsoever. First, there are the users with the new bootloader. They have the swap partition but actually don't make use of it. As compared with users with the old bootloader: they may not have the swap partition on their partition table but even they can easily implement a solution very similar to a standard swap partition: a swap file. Then they can benefit as well from some extra Megabytes in certain low memory situations. You don't have a swap partition/file in use yourself yet? In just a few minutes you can change that no matter what bootloader you have.

First you have to find out which bootloader/partition layout is on your LG-P990:

here is how you determine your bootloader/partition layout: reboot!

If you see a pink LG logo when the phone boots then you have the new bootloader/new partition layout (ICS)

If you see a white LG logo when the phone boots then you have the old bootloader / old partition layout(GB)

All commands used in the terminal are shown with a '$'-sign or '#'-sign (root). Copy just the commands without the signs to the terminal, unless stated otherwise.

New bootloader/partition layout (ICS) - Pink LG logo as splash screen - You have the old bootloader/partition layout? Skip to the second part below.

If you are using the new partition layout, then it is easy to use the swap mechanism. The 350MB swap partition is just waiting for you to be switched on.

Use 'adb shell' or the terminal:

check the situation with

$ free

Does it show 0 0 0 in the swap line? Then swap is not working at the moment and has to be switched on.

Use

$ su

to get superuser rights

Make the swap partition usable (Be extra careful! Triple-check the command!)

# mkswap /dev/block/mmcblk0p4

Switch it on

# swapon /dev/block/mmcblk0p4

Change swappiness to zero at runtime

# sysctl -w vm.swappiness=0

To keep the swap partition switched on, after reboot and even after flashing ROM updates

create the file /data/local/userinit.sh

Put these lines inside (with the '#' sign)

#!/system/bin/sh
swapon /dev/block/mmcblk0p4
sysctl -w vm.swappiness=0

swappiness=0 lets the kernel use the swap partition only when there is no more physical RAM left.

Save the file.

Now reboot.

Check it via adb shell or terminal if it is working.

$ free

The swap line should show that 350 MB are available (total/free).

Check the swappiness with

$ cat /proc/sys/vm/swappiness

It should show 0 now.

Testdrive your work. Start Chrome or a game or whatever uses a lot of RAM on your system. Use it heavily and then check the swap usage of your kernel again with a final

$ free

Just try it!

Old bootloader/partition layout (GB) - White LG logo as splash-screen

The task is to create a 128 MB Swap-File on the data partition. 128 MB seems enough since it is just for memory emergencies. And it's best to create it on the data partition and not on the sdcard because of the USB mass-storage support that would interfere with any swap file on sdcards. By the way, it doesn't format your data partition. It just makes your usable space of the data partition 128 MB smaller than it is right now. Basically, it creates an empty file with the fixed size of 128 MB and makes it usable as a swap 'partition'. It has the same functionality as a swap partition. And the same speed. And everything else. It just doesn't reside in its own partition. It is even better: when you don't want it anymore or change the size of it, just stop the swap-file with 'swapoff' and delete the file 'swapfile' in your data partition and it's gone for good. Immediately you can use the freed-up space for something else.

Use 'adb shell' or the terminal:

check the situation with

$ free

Does it show 0 0 0 in the swap line? Then it is not working at the moment and has to be switched on.

See with

$ df

whether there is enough free space left on /data. 200 MB or more should be free on /data. 128 MB will be used.

Get superuser rights

$ su

Optional: To see more interesting memory stats you can use

# cat /proc/meminfo

Now create the file named 'swapfile' with the size of 128 MB

# dd if=/dev/zero of=/data/swapfile bs=1024 count=128000

This can take a moment.

Limit the rights of the swapfile

# chmod 600 /data/swapfile

now set up the swapfile for swap usage

# mkswap /data/swapfile

Switch it on

# swapon /data/swapfile

Hint: switch off would be swapoff /data/swapfile

Now it is already working!

Tune it to your needs: we just want the swap file to be used in emergencies when there is no RAM left anymore and Android would turn Project Butter in Project Stutter.

Check the status of swappiness

# cat /proc/sys/vm/swappiness

Default is 60. That is much too high. swappiness=0 lets the kernel use the swap file only when there is no more physical RAM left.

Change it to zero

# sysctl -w vm.swappiness=0

With this sysctl command you can change the swappiness setting (0-100) on runtime.

If you want to make these settings permanent no matter if you reboot or even flash a ROM update without wiping data then

create the file /data/local/userinit.sh

Put these lines inside (with the '#' sign)

#!/system/bin/sh
swapon /data/swapfile
sysctl -w vm.swappiness=0

Save it.

Reboot and check it with a last

# free

in terminal. Now testdrive your swap file with two browsers and a game simultanously or whatever suits your needs to stress your P990.

Just try it yourself!

Have fun!