[TIP] Enable swap

650 posts
Thanks Meter: 87
By sfjuocekr, Senior Member on 16th June 2010, 12:13 PM
Post Reply Email Thread
After using A2SD to format a swap partition it is not active.

So to enable it, do the following:

Install ROM Manager trough market.
Start ROM Manager and select the first option "Flash ClockworkMod Recovery".
Turn off your device.
Turn on device with volume down pressed.
Press volume down, selection will switch to RECOVERY then press power button.
At the black screen with red triangle press volume up and power button.
Press volume down, selection will switch to Apply and then press power button.
Open a console, navigate to the android SDK tools folder.
Type "adb mount /system", without the quotes doh!
Type "adb shell".
At the shell copy paste the following commands one by one and press enter after pasting them.

echo "#!/system/bin/sh" > /system/etc/init.d/05userinit
echo "swapon -a" >> /system/etc/init.d/05userinit
echo "/dev/block/mmcblk0p3 swap swap" >> /system/etc/fstab
chmod +x /system/etc/init.d/05userinit
chmod g-w /system/etc/init.d/05userinit
chmod o-w /system/etc/init.d/05userinit
chmod g-w /system/etc/fstab
chmod o-w /system/etc/fstab

Now you are back at the normal command prompt, type "adb reboot".
Device will reboot, when its done open a terminal and type "free".
It should show swap space allocated.

And with some programs in memory you can see swap being used:

adb shell
total used free shared buffers
Mem: 407860 399940 7920 0 872
Swap: 249596 16596 233000
Total: 657456 416536 240920

Altho it was just using a small bit of swap, this obviously helps.

I was running two benchmarks, had maps open and a radio stream playing.

Quadrant showed my Desire rating at 559 while I was trying to get the phone to actually USE the swap space ...

Btw this is my first Android phone, but I have a rich linux background since 1998. Let's try Android 2.2 as this would boost the benchmark quite notably I recon!
The Following 3 Users Say Thank You to sfjuocekr For This Useful Post: [ View ] Gift sfjuocekr Ad-Free
16th June 2010, 12:40 PM |#2  
Senior Member
Flag Ealing
Thanks Meter: 386
For the most part, swap is not a good idea on Android, because is prevents Androids internal process management from operating efficiently. In some cases, swap can increase performance but in general use it doesn't.


16th June 2010, 09:17 PM |#3  
OP Senior Member
Thanks Meter: 87
Donate to Me
I have to disagree that enabling swap prevents memory management from being effective.

Altho enabling swap "might" degrade the lifetime of your SD card, it still holds memory which is not in use for some time.

You can confirm this by booting up the phone with swap enabled, check the free command and you will notice swap space not being used at all for a fairly long time and even using some applications.

After roughly a day usage, battery is still not empty (Im amazed by this HTC phone!) and swap space is being used by quite abit! Its using over 120mb right now and I still have roughly 10mb free normal memory. Programs still load near instantly and thus tells me swap management works like it should.

To perform a sanity check I started Maps again, free memory dropped to 3mb and some swap was freed. Which confirms that I had not used Maps for the whole day (was sleeping in bed lol) and when it started requested memory which was swapped away and obviously released when it was needed again.

Benchmarks tell me the same story, phone is scoring higher numbers than I tested earlier but this is due to me trying to fill swap during the benchmarks earlier this day. 571 compared to 559, it's not alot but it is "some".

To make a long story short, swap increased performance only when memory is being used too much. Will the performance be noticeable? Probably marginal, but you might notice the difference when your phone has been on for a few days straight. Because the longer you use it, more memory will be used and more unused parts of memory will be swapped.

You might also notice the swap increase when using lots of apps at the same time, mostly using multiple webpages and Maps at the same time.

Some arguments about SD cards being slow, this is only true for writes and not reads.
19th June 2010, 01:48 AM |#4  
OP Senior Member
Thanks Meter: 87
Donate to Me
After further investigation and testing ...

The memory manager from Android could even be optimised for swap.

The current state makes absolutely no use of available swap space, except when memory get filled and then it will start swapping.

In fact it makes TERRIBLE use of swap space ...

You can test this yourself pretty easily yourself. Just open as many applications as you can to fill swap space and leave the phone running for a few hours. If you are like me you are playing with it, testing and/or whatnot you like todo.

After a few hours your swap space will be well filled, in my case around 120mb.

Now I run Advanced Task Killer to kill everything that was running, now swap space used dropped to around 30-40mb and I have almost 100mb free memory ... (free 96924).

Now I reboot the phone and check again after killing all tasks, result is .... rebooting to make an acurate post ... 61528.

So that is roughly 35mb of swapped memory which WAS inuse by Android, HTC Sence and whatnot that was not needed. Ofcourse if the memory would be needed again, it would simple be put back into memory and as this happens alot with lots of applications running.

I can safely conclude that memory management on the Android can be improved by the use of swap ... if it were actually used properly. The only way now is to use a task killer and see your memory increase over time, which is great as my Desire shows absolutely now slow downs or choppyness at all.

On the other hand, testing without swap enabled showed that the memory would increase after bootup too. But not close to 100mb, more like 70mb.

Another point to defeat the point against using swap, the swapped memory is placed on SD and SD write speeds are usualy slow. The slow write speed however does not matter if the memory is not filled already and the to be swapped memory was not inuse for a given time. The other way round when the memory is indeed needed again, SD reads are usualy much faster than writing and as it is such a small amount the read is faster than one would notice.

So when I get time for it, I'll dive into the kernel source. See if we can get a better memory/swap manager going for our already snappy fast phones and make them even faster!
19th June 2010, 02:02 AM |#5  
OP Senior Member
Thanks Meter: 87
Donate to Me
double post :s
19th June 2010, 08:57 AM |#6  
Senior Member
Flag Ealing
Thanks Meter: 386
Originally Posted by sfjuocekr

I have to disagree that enabling swap prevents memory management from being effective.

If you use swap, you are effectively telling Android you have more memory than you actually have. Therefore, it doesn't reach a memory low condition until you've used up swap, which means it doesn't start killing processes.

Use of swap practically mandates the use of a task killer to kill processes that
Android task management would have usually killed to keep the phone operating smoothly.


19th June 2010, 07:33 PM |#7  
OP Senior Member
Thanks Meter: 87
Donate to Me
You aren't telling Android that you have more memory, there is memory and there is swap.

If Android's process manager doesn't kill processes because it thinks you have more memory, it's clearly stupid as it doesn't know the difference between swap and memory. Besides that both memory and swap are reported independenty.

After 17 hours uptime my phone still holds around 35mb in swap and around 40-50mb free memory, so Androids process/memory manager can clearly be improved to facilitate swap space.
2nd October 2011, 12:36 AM |#8  
Flag Milano
Thanks Meter: 4
Hi sfjuocekr,
first of all thanks for your little tutorial.

I just wanted to know, that method is still working? I mean on CM7 etc.
And will stay after updating the ROM (e.g. nightlies) or has to be done again every time?

Then, in your case the swap partition was mmcblk0p3, but it's not always like that and should be checked right?
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes