Wednesday, September 29, 2010

Swap memory


Earlier I was responding to XDA forum about the use of swap memory on my device. I think it's good that I share it with the rest as well.

Swap memory is not really the memory that comes manufactured or "hardwired" to your device. It's basically some disk space (maybe your sdcard) that is prepared and allocated for "extended memory" usage. When "extended memory" term is in concern, it doesn't mean that it will adds up to your total memory of your device, what it does is that it will be a temporary storage to support your total memory that is running on your device with "swapping" the memory in and out as needed. Think of the total memory your device can support is a jug, how much memory is being used is the water within the jug itself. Imagine now you are given a glass, surely you can hold more water in the jug by offsetting some water from the jug to the glass itself right ?

In android terms, you can consider this example;
You run 5 applications (Namely A,B,C,D,E in sequence) and closes all of them using the BACK button and get back to your HOME screen. Now you wish to launch back the 1st application (A) that was closed earlier, what will happen ?

Scenario without swap:
When you use the BACK button to quit an application, this doesn't mean that the application had been terminated. Most of the time, it still resides within the memory of the device in an idle state until Android needs the additional juice to run other applications and which it will then terminate any applications that had been sitting idle for too long and claim the memory used. In the scenario above, if the applications B,C,D,E needs a lot of memory to function, application A would usually be terminated already. This causes the relaunch of application A would be from the disk itself. Depending on how much data needs to load, sometimes starting an application from scratch is quite painfully slow (i.e. Launcher with many widgets)

Scenario with swap:
Setting swap tells Android that there is a part of extended memory on the disk in which it can use to "swap out" data from the memory. This causes more applications can be launched without getting terminated that early. In the above scenario, application A may still resides in the swap memory and Android would just have to "swap in" the data from the disk back to memory and continue running it. This may help in terms of performance because the application doesn't need to start from scratch.

Sounds useful ? Well, you also need to understand that using SDCard as a swap area would degrade the life of your SDCard. Your SDCard have a limited amount of write cycles and swapping memory to/from SDCard would deplete the amount faster than your standard usage without swap. I reckoned that SDCards are cheap and by the time it starts to die on me, I'm sure I ought to be buying a higher capacity SDCard with higher speed to be used as swap. But at the end, the choice is up to you...

Another drawbacks is that swapping would require a bit of resource. Accessing SDCards requires power and swapping data would require CPU cycles. If your test shows that the performance benefit/gain is justifiable, then I'm sure these factors would not be much of a problem for you.

To use swap on your device, download an application called SWAPPER2 from market. It's free and easy to use and setup. You will also need to set the swappiness (how frequent to swap data) from the application as to fine tune your device in it's new environment. I am using Swappiness 20 but you can start from 10 and see if it helps or not, if it doesn't move on to a higher number until you're happy about it.

Oh btw, did I tell you that you need ROOT in order to use this application ?

No comments:

Post a Comment