Contrary to the traditional computing world, where programs won't quit without the user asking for it, in the mobile world (and Android in particular), it's generally considered up to the system to decide which app to quit and when, to the point where many if not most apps don't even have a quit option.
Up to some extent, it's justified due to :
- The limited amount of RAM available (contrary to the PC/Mac world where we've gone too far in the other direction, and the system by default considers a program can ask for as much virtual memory as it wants, which is a security problem, but that's another subject)
- The devices being for the general public, in a context where people don't necessarily have the knowledge/will/time to manage their phone usage on an app by app basis
- The apps are often designed to be used for short periods of time (which is linked to the previous point somehow)
However, there are cases where one doesn't want a given app to quit unless it's asked to explicitly by the end user. The reality of that need seems to have been taken into account by some mobile phone manufacturers (if I'm not mistaken, Samsung has been providing such an option for some time now).
So my question is : how shall I tell the system to not kill a given app ? I know there are apps that pretend to achieve this, but how shall I do that myself, without resorting to third party tools, only the stock Android command line tools and APIs ?
Afaik, there seem to be three criteria to decide to kill an app :
- Memory usage (the LMK daemon being in charge, unless a specific driver is present, in my case I have lmkd running)
- A limit on the number of running processes
- Battery optimisation, but there's already an option to disable that per-app, so I don't consider it a problem
So basically, only points 1 and 2 remain to be solved. You advices are welcome !
For what it's worth, I run Android x86 9.0 on an emulator (qemu).
EDIT : I checked the code of a program that's called App Settings, that's part of the Xposed framework. The program pretends to be able to set a program to stay resident in memory. After checking the code, it seems it modifies some application process attributes that are defined in the
ProcessRecord class, namely
setAdj, setting those to the value of
I wasn't able to test the program since my x86_64 Android 9.0 doesn't seem compatible with the framework, but I found this page, explaining in more detail how the activity manager service updates OOM values according to the app status, and communicates with LMK. It explains what the OOM values can be and what they mean. So clearly, trying to change the OOM values in
/proc is useless, as the activity manager will change them, and probably LMK doesn't even read them anyway, and only takes what the activity manager transmits.
Is there any way to hint the Activity Manager into giving chosen OOM values to an app ? I could for example set the value to
NATIVE_ADJ, or even no more than
FOREGROUND_APP_ADJ at all times. Or maybe passing those parameters through a specially crafted intent ?
REEDIT : for those interested, the
stop lmkd command should stop the LMK daemon until next reboot at least, which leaves only the OOM killer from Linux in charge. I didn't test it so I can't say for sure, but that should do it for some people. Beware of the consequences, however, if you continue using many apps at the same time, as a performance hit can be expected.