Take the 2-minute tour ×
Android Enthusiasts Stack Exchange is a question and answer site for enthusiasts and power users of the Android operating system. It's 100% free, no registration required.

Some friends told me that their Android phones have very little (<100MB) available memory for regular apps, because the OS and some un-killable services take most of the RAM. For example, one phone with 512MB only shows 90MB available memory, so only 2-3 apps can run at the same time.

I'm wondering if creating a swap partition/file will help, but I saw mixed opinions all over the web and I'm not sure what to do. Here are my questions:

(1) Will there be many not-oftenly-used memory pages to swap out, especially in OS and those un-killable services?

(2) How will swapping out small memory pages hurt flash memory life span? Does the flash (or SD card?) do wear-leveling automatically?

(3) Will swapping really help performance when running multiple apps? I assume if the answer to (1) is yes, it will.

(4) Will writing to flash be too slow and thus hurt responsiveness when swapping happens?

(5) Is it true that Android keeps the swapping mechanism from the Linux kernel, so as long as there is a swapping partition/file, it will do swapping without extra configuration?

Thanks for reading.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

I don't think there will be much benefit with swapping for Android, as the Android Application Life Cycle is a much more advanced form of swapping.

(1) Will there be many not-oftenly-used memory pages to swap out, especially in OS and those un-killable services?

You can't swap the OS kernel even in Desktop Linux and Android already kills services when it needs more RAM. If your device vendor persists that you have to have useless services running all the time, then root your device.

(2) How will swapping out small memory pages hurt flash memory life span? Does the flash (or SD card?) do wear-leveling automatically?

Even if the SD-card does wear-leveling, swapping will hurt quite a lot.

(3) Will swapping really help performance when running multiple apps? I assume if the answer to (1) is yes, it will.

Not in the context of Android. Unlike traditional OS that will keep trying to chug in processes as you requested it, Android will Force Kill older, unused processes and reclaims their memory; this killing is fast as applications will already save their state when you task switch.

(4) Will writing to flash be too slow and thus hurt responsiveness when swapping happens?

Probably, probably not. Will need benchmarks for that.

(5) Is it true that Android keeps the swapping mechanism from the Linux kernel, so as long as there is a swapping partition/file, it will do swapping without extra configuration?

I don't know whether Android keeps the swapping mechanism, but even if it does (or if you compile your own kernel), you will still need some configurations. It's usually just having an fstab file that points to the swap file and doing swapon -a.

In fact I have reasons why swapping may actually hurt performance: it prevents Application Life Cycle, Android's memory management scheme, to work correctly.

share|improve this answer
    
Thanks for the answer. I still have questions though. On a Samsung Fascinate, 10 un-killable services occupy about 110M memory. There are 22 other killable services running but "Running Application" shows no application running. It's my friend's phone and I didn't know what exactly she did before, but it seems that it is quite possible that many services/apps could run at the same time. I don't think all these background services/apps use all the pages they required when they started. Besides, save app state + reload and restart app + restore state would be slow as well. –  evergreen Oct 25 '10 at 5:44
    
Another way to think of this problem is this: assume flash life span is not a problem, should Android do swapping? If not, what is the key difference between Android apps and desktop apps that makes swapping not useful on Android? I don't think Android Life Cycle works for desktop/server applications. –  evergreen Oct 25 '10 at 5:51
    
@evergreen: Application state is saved when the application goes to background (e.g. when user press home to switch task). Later, when some other application needs memory, older applications or background services will be force killed (FK takes practically no time). This means task killing is immediate, since application state is already saved a long time ago. When you reload an application that's already in memory, you will get the fastest reload time; but if the application is already unloaded, then the application needs to do full reload (which is the same situation with swapping) –  Lie Ryan Oct 25 '10 at 16:32
    
@evergreen: if you use a task killer, then you will force applications to unload, and you will always have to do full reload, instead of the fast reload when the application stays in memory. If you use a swap file/partition, (older) applications will be swapped out, and restoring will take almost the same amount of work as full reload. Except: with full reload, the application developer has separated out persistent data with temporary data, and can optimize what to save and what not to improve storage. While with restoring swap, you leave application developer no say, and swap out everything. –  Lie Ryan Oct 25 '10 at 16:37
    
@evergreen: Even when assuming flash life is not a problem, Android should not do swapping, because the Application Life Cycle does a better job. Android Life Cycle is designed for memory constrained environment, in desktop and server environment, you have a gigantic amount of memory, and users do much more multitasking, and they do not expect background programs to be killed by the OS. The weakness of Android Life Cycle is it requires application support. Application must be ready to be killed any time, and must save state at predefined points. Such complexity is extraneous for desktop apps. –  Lie Ryan Oct 25 '10 at 16:41
show 1 more comment

There is definitely a benefit to swapping, despite what everyone else on the Internet will tell you. Try it and see for yourself. Empirically, on a G1 or other phone with low memory, swap makes the phone run better and faster.

Yes, Android has Life Cycle "task management" built in, but it's not very good. It routinely kills processes when it runs out of memory, and the apps are expected to save their state using "Bundles" so that when you restart them, they restart in the same state they were last in.

Once Android determines that it needs to remove a process, it does this brutally, simply force-killing it. The kernel can then immediately reclaim all resources needed by the process, without relying on that application being well written and responsive to a polite request to exit. Allowing the kernel to immediately reclaim application resources makes it a lot easier to avoid serious out of memory situations.

This would be great if it actually worked, and would be better than indiscriminate swap. But the apps don't really save their state; they just save the minimal amount of information to get back to that state. (And some apps don't even bother to do this.) Getting back to that state takes time. Since swap actually saves the entire state of the app, which just has to be reloaded, it makes switching between apps much faster.

If you switch to another app from the browser, for instance, the browser almost always gets killed, and then it has to reload the entire page from the Internet when you switch back to it. This takes wayyyy longer than reloading the state from swap, wastes your money if you're on a data plan, and causes state problems when the web page is dynamic.

Many apps take much longer to start up than they should, or don't actually return to the same state when they're restarted, so swapping them out works better.

I'm guessing the people who are happy with the stock system use their phones differently.

Should I use a swap partition with Cyanogenmod?

share|improve this answer
    
If you leave it to the Life Cycle, it will be the application's decision to cache extra data to improve performance (e.g. loaded web pages). If you use swap, it's the OS' decision, but the OS isn't in the best position to determine what to save and what not to save to best optimize performance, so it have to save and restore everything. The Browser does not redownload pages from the Internet if you switch tasks, I've often open Browser, switching to a bunch of heavy games and other activities, and return to Browser with my last page with everything in place loaded instantly. –  Lie Ryan Oct 22 '10 at 16:28
    
Right. Neither system is optimal, but swap is better for most apps. The browser only reloads the page if it is killed before you re-open it. On a phone with low memory, it will almost always get killed. –  endolith Oct 22 '10 at 16:33
    
@LieRyan - "return to Browser with my last page with everything in place loaded instantly" does you browser restore scroll position on the opened pages after reload? That is what Chrome doesn't do (though I like this browser very much) which is very unfortunate when you stopped reading on a middle of some rather log web page before switching to, say, take a call... –  kerim Mar 6 '13 at 10:13
add comment

I have a Spice mi-435(only sold in India) which has a RAM of only 340M. Hell ya, the swap files help a lot. My phone runs ICS on such low RAM and definitely entered many out of memory states and there the swap file helps a lot. All the application states are transfered to swap when not in active use. This gives my phone room for more applications and RAM hungry apps to run smoothly with much much much less lag than before. Though there comes a problem when i open the app i have been using before. It takes time to load its state as the SD is much more slower than the high speed RAM on board. But, the swap option is better for so less RAM. I would recommend to swap partition for RAM less than 512M but not for more than 512M.

Hope it is helpful.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.