Low Memory Killer in Android

Source: Internet
Author: User

Reprint Please indicate the source:

Http://blog.csdn.net/yujun411522/article/details/46334123
This article is from: "Yujun411522 's Blog"


under the existing technical conditions, memory is always a tight resource, needless to say, there will be a lack of memory on the PC, not to mention on the mobile device. A lack of memory can cause the system to lag and affect the user experience. While Android is running on Linux, the principle of Linux memory usage is not to waste memory, so when the program exits in memory for a period of time, which is the next time we open the program to find the reason for the faster But the downside is that if there are many programs that reside in memory, it can easily lead to oom.
in Linux there is a memory monitoring mechanism oomkiller, once found that memory usage into a critical value will automatically follow a certain strategy to clean up, its core idea is:
1 kill processes by priority, from low to Takaki, reclaim memory resources21 aspects to consider the killing process damage to the system as small as possible, on the other hand to release as much memory as possiblespecifically, Oomkiller will calculate a Oom_score score based on a number of reference factors such as process consumption memory, run time, oom weight, and so on, the lower the probability of the process being killed, the later the kill time. and for the Android system is to achieve a "different level" Killer, the module name is low Memory Killer, the code in the Drivers/staging/android/lowmemorykiller.c file
static int __init lowmem_init (void) {         register_shrinker (&lowmem_shrinker);         return 0;}
<span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "text-align:-webkit-auto;" > Code in </span><span style= "text-align:-webkit-auto;" > Register a Shrinker Listener, if the System space page is below a certain threshold, perform this lowmem_shrinker function </span></span>
two important arrays are defined in the file:
static int Lowmem_adj[6] = {       0,       1,       6,       };static int lowmem_adj_size = 4;static size_t lowmem_minfree[6 ] = {       3 * +,/* 6MB */       2 * 1024x768,/       * 8MB */       4 * 1024x768,/       * 16MB */       * * *,      */* 64MB */ };static int lowmem_minfree_size = 4;

each of the two arrays defined above corresponds, where Lowmem_adj represents the value of the adj that is being processed at a certain level, and Lowmen_minfree represents the memory threshold for that level. For example, the level of adj=0, which corresponds to a memory threshold of 6M, that is, when available memory is less than 6M, all processes with adj greater than or equal to 0 are cleared. So it can be seen that the smaller the adj, the less likely it is to be killed. The system defaults are defined above, and you can modify both sets of values by setting the appropriate files:/sys/module/lowmemorykiller/parameters/adj/sys/module/lowmemorykiller/parameters/minfree (in pages, typically 4KB size)
several adj values have been defined in the Android system, with the following meanings:

These are the adj provided by the system, and we can change the adj value of our own process in the following two ways: 1 Write file write/proc/pid/oom_adj value, this kind of statement is often seen in the init.rc fileOn early-initwrite/proc/1/oom_adj-16set the Adj value of the init process to 16, which belongs to the system process and is never killed
2 Set the Persistent property. Setting this property to True in the Androidmanifest.xml file sets the value of its adj to-12, and the process at that level is basically not killed, such as a telephone.







Low Memory Killer in Android

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.