21

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.

4 Answers 4

8

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.

5
  • 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, 2010 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, 2010 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, 2010 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, 2010 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, 2010 at 16:41
6

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?

3
  • 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, 2010 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, 2010 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, 2013 at 10:13
2

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.

1

Swap file helps even on systems with higher RAM. My quad core 1gb ram 7 inch tablet gets a major multi-tasking boost from chucking in a 2gb swap, on the uhs-1 micro sd card. And of course, the price of memory goes down rapidly, and even at constant write, nand flash is supposed to last years.

Im basically of the opinion, because the performance difference is so radical, that probably anyone with less than a really large amount of RAM should be doing this. In fact it should probably come as a standard option in android (along with bigger app caches, and a proper sized internet cache).

I mean you have a 64-128gb micro sd (uhs-1), or even the uhs-2 that came out this year, plus a 16-64gb internal nand, often "fast" nand, which is a kind of primitive ssd or even SSD, as are used on windows slates - your not exactly filling all that high speed space up with much. The speed only really comes into play with video, or loading apps into memory - which is exactly the use here.

Of course ideally, manufacturers would focus more on RAM, than resolution or extra cores, because clearly the system speed benefits a lot from higher ram - and they should probably also up the speed of the internal storage too (like switching to full ssd type systems, or at minimum "fast" nand. But in the mean time, while high resolutions tend to cause a sort of RAM deficit in all devices, a swap file, and caching to a reasonably snappy external sd makes a lot of sense.

Indeed it would be nice to see some browsers keep fuller caching too, most browsers are either slow, or low feature, rarely fast and full featured - and thats partly because the space for the app cache on the internal drive is tiny. They are designed to operate within a low memory and storage environment, and that whole story is changing. Even if the ram isn't up to chop as much as it should be, the storage certainly is, and it can be pretty darn fast on UHS-1 standards (or the uhs-2 released this year), even if its not "fast" or ssd.

And the cost of external memory, is peanuts really, even for higher capacity uhs-1. At least a 64gb uhs-1 is perfectly affordable (50 bucks where I am, but it would be less USD), much cheaper than the device, and 32 gb and under is basically a couple of tenners.

Plenty of lessons to be learned from the desktop really, especially now the specs are getting higher - the slowest performance link is loading from storage to ram. The more you can do to mitigate that, the better.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .