Low Memory Killer in Android

Source: Internet
Author: User

Low Memory Killer in Android

 

Under the existing technical conditions, memory is always a tight resource, not to mention the possibility of insufficient memory on the PC, not to mention on mobile devices. If the memory is insufficient, the system gets stuck and the user experience is affected. While Android runs on Linux, the memory usage principle of Linux is not to waste memory, so it remains in the memory for a period of time when the program exits, this is also the reason why we found faster when opening the program next time; but the downside is that if there are more programs residing in the memory, it is easy to cause OOM.
In Linux, there is a memory monitoring mechanism OOMKiller. Once the memory usage enters a critical value, it is automatically cleared according to certain policies. Its core idea is:
1. Kill the process from low to high according to the priority and reclaim memory resources. 2. consider killing the process to minimize the damage to the system, on the other hand, to release as much memory as possible, OOMKiller will calculate an oom_score based on some reference factors such as memory consumption, running time, OOM weight, and other indicators. The lower the score, the lower the probability of a process being killed, the later the time it takes to be killed. For Android systems, a different level of killer is implemented. The module name is Low Memory Killer and the code is in the drivers/staging/android/LowMemoryKiller. c file.
 
 static int __init lowmem_init(void) {         register_shrinker(&lowmem_shrinker);         return 0;}
Register a shrinker listener in the Code. If the system space page falls below a certain threshold, execute this lowmem_shrinker function.
This file defines two important Arrays:
 
static int lowmem_adj[6] = { 0, 1, 6, 12, }; static int lowmem_adj_size = 4; static size_t lowmem_minfree[6] = { 3 * 512, /* 6MB */ 2 * 1024, /* 8MB */ 4 * 1024, /* 16MB */ 16 * 1024, /* 64MB */ }; static int lowmem_minfree_size = 4;

The two arrays defined above correspond One to One. lowmem_adj indicates the value of the adj to be processed at a certain level, and lowmen_minfree indicates the memory threshold corresponding to this level. For example, the memory threshold of an adj = 0 is 6 MB, that is, when the available memory is less than 6 MB, all processes with an adj greater than or equal to 0 will be cleared. So we can see that the smaller the adj, the less likely it is to be killed. The above is defined as the system default. You can set the corresponding files to modify these two sets of values: /sys/module/lowmemorykiller/parameters/adj/sys/module/lowmemorykiller/parameters/minfree (in the unit of page, generally 4 KB)
In the Android system, several values of the adj are defined. Their meanings are as follows:

These are the systems that provide them. We can also change the value of our processes in either of the following ways: 1. Write a fileWrite the/proc/pid/oom_adj value in init. rc files often see this statement on early-init write/proc/1/oom_adj-16 set the value of the init process to-16, which will never be killed by system processes.
2. Set the persistent attribute.If this attribute is set to true in the AndroidManifest. xml file, the value of the adj parameter can be set to-12, and processes at this level will not be killed, such as phones.







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.