In android, we can intercept the low memory event for our application or enable it by setting it via registerComponentCallbacks as a handler for the successor from the ComponentCallbacks interface by redefining its onLowMemory method.
It is understood that in this handler we can free up non-critical resources when the low memory event occurs, clean up the internal cache and use other methods to reduce the amount of memory used, thereby avoiding that our process will be closed.
The system also guarantees that after calling onLowMemory, the system garbage collector will be called
')
And so, if our application implements any behavior when this event occurs, it would be good to test this behavior.
The standard way is simple - we start artificially increasing the amount of resources consumed (for example, uploading pictures two times) until we rest on the limit.
And there is a second way - to change this limit, which actually will be discussed below.
Unfortunately, this method will only work on rooted devices.
And so, the task scheduler in android divides running applications into the following 6 types:
FOREGROUND_APP:
// This is the process running the current foreground app. We'd really
// rather not kill it! Value set in system/rootdir/init.rc on startup.
VISIBLE_APP:
// This is a process only hosting activities that are visible to the
// user, so we'd prefer they don't disappear. Value set in
// system/rootdir/init.rc on startup.
SECONDARY_SERVER:
// This is a process holding a secondary server -- killing it will not
// have much of an impact as far as the user is concerned. Value set in
// system/rootdir/init.rc on startup.
HIDDEN_APP:
// This is a process only hosting activities that are not visible,
// so it can be killed without any disruption. Value set in
// system/rootdir/init.rc on startup.
CONTENT_PROVIDER:
// This is a process with a content provider that does not have any clients
// attached to it. If it did have any clients, its adjustment would be the
// one for the highest-priority of those processes.
EMPTY_APP:
// This is a process without anything currently running in it. Definitely
// the first to go! Value set in system/rootdir/init.rc on startup.
// This value is initalized in the constructor, careful when refering to
// this static variable externally.
For each of these types of applications there is a memory limit at which, if the memory remains less than this limit, the scheduler starts to kill applications of this type that are not used from its point of view.
These limits can be viewed as follows.
adb shell cat /sys/module/lowmemorykiller/parameters/minfree
For Samsung Galaxy Nexus, the values will be as follows
7469,9396,11324,13372,15299,19034
Values are measured in memory pages, each page is 4 kilobytes.
If you have a rooted phone, you can change these values arbitrarily in real time, with a simple command
echo "1536,2048,4096,5120,15360,23040" > /sys/module/lowmemorykiller/parameters/minfree
When the device is rebooted, the values are restored.
And so, by changing these values in runtime, you can easily call your onLowMemory handler, and also potentially test the application's behavior on other devices that have different values (although this will not replace full testing on the device itself, of course).