Android Process Recycling mechanism LMK (Low Memory Killer) "Turn"

Source: Internet
Author: User

This article was reproduced from: http://www.cnblogs.com/wytiger/p/5744752.html

Children who are familiar with the Android system know that the system does not actually kill the process, but caches it for experience and performance reasons, when the app falls back into the background. The more apps you open, the more processes you can cache in the background. In the case of low system memory, the system began to determine which processes to kill by its own set of process recovery mechanisms, in order to free up memory to supply the required app, the process of recovering memory mechanism is called low memory Killer , it is based on the Linux kernel The OOM Killer Mechanism was born. (More about low memory Killer please refer to: Android low memory Killer)

Understand the low Memory Killer, and then popular Oom_adj. What is Oom_adj? It is a value assigned to each system process by the Linux kernel, representing the priority of the process, and the process recycling mechanism is based on that priority to determine whether or not to recycle. For Oom_adj, you need to remember the following points:

    • The larger the oom_adj of the process, the lower the priority of the process, the easier it is to be killed, the smaller the process priority, the less likely it is to be killed and recycled
    • The oom_adj>=0 of the ordinary app process, the Oom_adj of the system process can <0

So how do we look at the Oom_adj value of the process, and we need to use the following two shell commands

//获取你指定的进程信息

Here is the demo code I wrote for example, the red circle in the middle of the following three process ID

UI process:Com.clock.daemon
Normal background process:COM.CLOCK.DAEMON:BG
Grey keepalive process:com.clock.daemon:gray

Of course, the IDs of these processes can also be obtained through Androidstudio


Then we'll come back and get the oom_adj of three processes.

/proc/进程ID/oom_adj

The oom_adj=0 of the UI process and the gray KeepAlive service process can be seen, while the normal background process is oom_adj=15. Here you can also understand why the normal background process is easy to recycle, and the foreground process is not easy to recycle it. But it's not enough to see it.


The above is I switch the app to the background, and then a Oom_adj test, you will find that the UI process value from 0 to 6, and the gray keepalive service process from 0 to 1. As you can see here, when the app is back in thebackground, all of its process priorities are lowered. But the UI process is the most obvious, because it consumes the most memory resources and must prioritize the memory-intensive processes to free up resources when the system is running out of memory. Therefore, in order to avoid the background UI process is killed, we need to release some of the unused resources, especially pictures, audio and video, and so on .

From the Android official documentation, we can also see that the priority levels are listed for these different types of processes:Foregroundprocess,Visibleprocess,Service process ,Background process,Empty process. What are the oom_adj of these processes, and how are they linked together? We recommend reading this article: Android low Memory Killer

Summarize

So much for a ramble, and a little summary at the end. The return to the beginning of the QQ process is not dead, I also think there is such a technology. Unfortunately I put the phone root, after killing the QQ process will never get up again. Some mobile phone manufacturers put these well-known apps into their whitelist, to ensure that the process does not die to improve the user experience (such as, QQ, Mo Mo are in the white list of millet). If removed from the whitelist, they will still be the same as the normal app to avoid the fate of the killed, in order to try to prevent the killing, or honestly to do a good job of optimization.

Therefore, the process of the fundamental plan to survive is still back to the performance optimization, the process of immortality is ultimately a false proposition! Supplemental Update (2016-04-20)

There are children's shoes asked, on the Huawei Machine found and hand Q UI process back to the background, the value of Oom_adj has not changed, is not what black technology in it. To this end, I slightly verified that the way to verify the package name of the demo project to the mobile phone QQ, compiled and run on Huawei's machine, found that my process how to kill also is not dead, back to the background Oom_adj value also does not change, and restore the original package name will not be. So, you know, the hand q is in the white list of Huawei's machine.

This concludes the article, see the Simple Code of Practice

Https://github.com/D-clock/AndroidDaemonService

Report:

Android Low Memory Killer

About Android process KeepAlive, all you need to know

Android Background KeepAlive Practice Summary: Instant Messaging applications cannot cure the "stubborn disease"

Android Process Recycling mechanism LMK (Low Memory Killer) "Turn"

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.