How to force a paused process' memory into swap?

1.9k views Asked by At

I'm looking for ways to improve UI against lagging for Android devices with 256MB of memory. The memory is so limited that large frontend apps trigger a lot of swap-outs for other apps.

The idea is: If an background app (a Facebook app for example) is not necessary to run continuously, unlike, say telephony, which must stay uninterrupted, just pause that app's process, and continue to run it every a few minutes to retrieve updates, only when the cellphone is idle in the pocket, and then pause the process again.

For reducing lag swapping out other apps' memory, I want the paused apps' memory can be proactively swapped to disk/flash, so the RAM can be instantly released in order the frondend app needs. (reference speed: class 10 SDHC: 10MB/s write, so the browser lags for 2 seconds if a webpage need to claim 20MB RAM)

So my question is: How to force the system to swap a paused process' private memory into disk/flash?

2

There are 2 answers

1
aurelien On

An inactive process goes automatically in swap if ram is required, because he has a lower priority (see priorities on android).

Theorically, a class 10 card is good and could only make swap in 2 seconds, but the card's memory controller is so long that you will have 5 or 6 seconds. Having more that a class 6 is not usefull.

0
Chilledrat On

Android already handles memory of paused apps, but rather than swapping out the processes to some virtual memory, which could be expensive, it can just dump them to free the memory if required.

From The Android Developers documentation - Managing the Activity Lifecycle:

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.

An activity's onPause() is the last of the life-cycle callbacks that is guaranteed to run, allowing it to save its state, after which Android may happily dump it as it sees fit.

From further in same document:

...if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage. However, you should be selective about what information must be retained during onPause(), because any blocking procedures in this method block the transition to the next activity and slow the user experience.

(emphasis added)

So the bottle neck you might experience is likely to be with the apps themselves, not with the OS, and saving the Activity state is still likely to be much quicker than that of trying to store the memory contents of a whole app.