Android Process Recycling mechanism

Source: Internet
Author: User

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. When the system is out of memory, the system begins to determine which processes to kill by its own set of process-recycling mechanisms to free up memory to supply the needed apps. The mechanism for recovering memory from this process is called low memory Killer, which is the creation of an OOM Killer (out-of-memory Killer) mechanism based on the Linux kernel.

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, which requires the following two shell commands:

1 ps| grepPackageName //获取你指定的进程信息

<ignore_js_op>

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:
<ignore_js_op>

Then we'll come back to get the Oom_adj of three processes:

1 cat/proc/进程ID/oom_adj

<ignore_js_op>

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 this:
<ignore_js_op>

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 the background, 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: Foreground process, Visible process, 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: http://www.cnblogs.com/angeldevil/archive/2013/05/21/3090872.html

Summary of this article


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!

Attached: Android low Memory killer about Android process keepalive, everything you need to know

Android Process Recycling mechanism

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.