ProcessList.java和adj值
简单地讲,adj值决定了在系统资源吃紧的情况下,要先杀掉哪些进程。
在Android的lowmemroykiller机制中,会对于所有进程进行分类,对于每一类别的进程会有其oom_adj值的取值范围,oom_adj值越高则代表进程越不重要,在系统执行低杀操作时,会从oom_adj值越高的开始杀。系统lowmemeorykiller机制下对于进程的级别的以变量的形式定义在framework/base/core/java/com/android/server/am/ProcessList.java类中,可总结成下表:
从上述adj值的定义中我们可以看到,值越小优先级越高,比如native进程的adj值为-17,对于这个adj值的进程来说,系统根本不会动它一分一毫,实质上当进程的adj值去到2时系统就很少会因为其它原因而去杀死它,这些在研究进程保活中都非常重要。
查看某个进程adj值的方法:
- cat /proc/进程ID/oom_adj
- dumpsys meminfo 这个会列出当前所有进程的分类情况
finalclass ProcessList {// OOM adjustments for processes in various states:// Adjustment used in certain places where we don't know it yet.// (Generally this is something that is going to be cached, but we// don't know the exact value in the cached range to assign yet.)staticfinalint UNKNOWN_ADJ = 16; // This is a process only hosting activities that are not visible,// so it can be killed without any disruption.staticfinalint CACHED_APP_MAX_ADJ = 15; staticfinalint CACHED_APP_MIN_ADJ = 9; // The B list of SERVICE_ADJ -- these are the old and decrepit// services that aren't as shiny and interesting as the ones in the A list.staticfinalint SERVICE_B_ADJ = 8; // This is the process of the previous application that the user was in.// This process is kept above other things, because it is very common to// switch back to the previous app. This is important both for recent// task switch (toggling between the two top recent apps) as well as normal// UI flow such as clicking on a URI in the e-mail app to view in the browser,// and then pressing back to return to e-mail.staticfinalint PREVIOUS_APP_ADJ = 7; // This is a process holding the home application -- we want to try// avoiding killing it, even if it would normally be in the background,// because the user interacts with it so much.staticfinalint HOME_APP_ADJ = 6; // This is a process holding an application service -- killing it will not// have much of an impact as far as the user is concerned.staticfinalint SERVICE_ADJ = 5; // This is a process with a heavy-weight application. It is in the// background, but we want to try to avoid killing it. Value set in// system/rootdir/init.rc on startup.staticfinalint HEAVY_WEIGHT_APP_ADJ = 4; // This is a process currently hosting a backup operation. Killing it// is not entirely fatal but is generally a bad idea.staticfinalint BACKUP_APP_ADJ = 3; // This is a process only hosting components that are perceptible to the// user, and we really want to avoid killing them, but they are not// immediately visible. An example is background music playback.staticfinalint PERCEPTIBLE_APP_ADJ = 2; // This is a process only hosting activities that are visible to the// user, so we'd prefer they don't disappear.staticfinalint VISIBLE_APP_ADJ = 1; // This is the process running the current foreground app. We'd really// rather not kill it!staticfinalint FOREGROUND_APP_ADJ = 0; // This is a process that the system or a persistent process has bound to,// and indicated it is important.staticfinalint PERSISTENT_SERVICE_ADJ = -11; // This is a system persistent process, such as telephony. Definitely// don't want to kill it, but doing so is not completely fatal.staticfinalint PERSISTENT_PROC_ADJ = -12; // The system process runs at the default adjustment.staticfinalint SYSTEM_ADJ = -16; // Special code for native processes that are not being managed by the system (so// don't have an oom adj assigned by the system).staticfinalint NATIVE_ADJ = -17; }