Android ActivityManagerService根据oom_adj数值内存回收机制

来源:互联网 发布:北京骏嘉财通弘历软件 编辑:程序博客网 时间:2024/05/22 06:12

转自:http://blog.csdn.net/zhoumushui/article/details/51197747


当系统的内存不足时, Android系统将根据进程优先级选择杀死一些不太重要的进程。

那么进程的优先级是怎样判别的呢?对,就是这个根据进程的oom_adj值。oom_adj的值越小,进程的优先级越高。


如何查看某个应用的oom_adj数值?

首先adb shell#ps查看应用的PID

然后#cat  /proc/PID/oom_adj的结果就是。


ProcessList中对oom_adj的定义,里面加了一些我个人的翻译,可能不是很精确。

frameworks/base/services/Java/com/android/server/am/ProcessList.java:

[java] view plain copy
  1. // The minimum time we allow between crashes, for us to consider this  
  2. // application to be bad and stop and its services and reject broadcasts.  
  3. static final int MIN_CRASH_INTERVAL = 60*1000;  
  4.   
  5. // OOM adjustments for processes in various states:  
  6.   
  7. // Adjustment used in certain places where we don't know it yet.  
  8. // (Generally this is something that is going to be cached, but we  
  9. // don't know the exact value in the cached range to assign yet.)  
  10. // ZMS:用作对一些特定的我们未知的地方进行调整。  
  11. //(通常是一些缓存,但是我们并不知道确切的缓存划分)  
  12. static final int UNKNOWN_ADJ = 16;   
  13.   
  14. // This is a process only hosting activities that are not visible,  
  15. // so it can be killed without any disruption.  
  16. // ZMS:这个是一个仅仅拥有后台不可见Activity的进程,可以被无破坏杀掉。  
  17. static final int CACHED_APP_MAX_ADJ = 15;  
  18. static final int CACHED_APP_MIN_ADJ = 9;  
  19.   
  20. // The B list of SERVICE_ADJ -- these are the old and decrepit  
  21. // services that aren't as shiny and interesting as the ones in the A list.  
  22. // ZMS:B列表Service ADJ--B列表Service是一些老旧的Service,没有A列表里的Service新。  
  23. static final int SERVICE_B_ADJ = 8;  
  24.   
  25. // This is the process of the previous application that the user was in.  
  26. // This process is kept above other things, because it is very common to  
  27. // switch back to the previous app.  This is important both for recent  
  28. // task switch (toggling between the two top recent apps) as well as normal  
  29. // UI flow such as clicking on a URI in the e-mail app to view in the browser,  
  30. // and then pressing back to return to e-mail.  
  31. // ZMS:这是用户前一个使用应用的进程。此进程优先级之所以排到其他缓存进程之上,  
  32. // 是因为切回前一个应用的场景很常见……  
  33. static final int PREVIOUS_APP_ADJ = 7;  
  34.   
  35. // This is a process holding the home application -- we want to try  
  36. // avoiding killing it, even if it would normally be in the background,  
  37. // because the user interacts with it so much.  
  38. // ZMS:主界面进程--尽管它经常在后台,我们同样想避免杀掉它,毕竟用户和主界面交互很多。  
  39. static final int HOME_APP_ADJ = 6;  
  40.   
  41. // This is a process holding an application service -- killing it will not  
  42. // have much of an impact as far as the user is concerned.  
  43. // ZMS:服务进程--由于用户关切,杀掉它会有不小影响。  
  44. static final int SERVICE_ADJ = 5;  
  45.   
  46. // This is a process with a heavy-weight application.  It is in the  
  47. // background, but we want to try to avoid killing it.  Value set in  
  48. // system/rootdir/init.rc on startup.  
  49. // ZMS:重量级应用进程。它在后台,但是我们想避免杀掉它。  
  50. static final int HEAVY_WEIGHT_APP_ADJ = 4;  
  51.   
  52. // This is a process currently hosting a backup operation.  Killing it  
  53. // is not entirely fatal but is generally a bad idea.  
  54. // ZMS:执行备份操作的进程。杀掉它不完全致命,但通常是个坏想法。  
  55. static final int BACKUP_APP_ADJ = 3;  
  56.   
  57. // This is a process only hosting components that are perceptible to the  
  58. // user, and we really want to avoid killing them, but they are not  
  59. // immediately visible. An example is background music playback.  
  60. // ZMS:拥有用户可感知组件的进程,所以我们尽量要避免杀掉它,尽管它不是立即可见。  
  61. // 例如:后台音乐播放。  
  62. static final int PERCEPTIBLE_APP_ADJ = 2;  
  63.   
  64. // This is a process only hosting activities that are visible to the  
  65. // user, so we'd prefer they don't disappear.  
  66. // ZMS:此进程仅仅拥有用户可见的Activity,所以我们不希望它消失。  
  67. static final int VISIBLE_APP_ADJ = 1;  
  68.   
  69. // This is the process running the current foreground app.  We'd really  
  70. // rather not kill it!  
  71. // ZMS:前台应用进程。最好不要杀掉它!  
  72. static final int FOREGROUND_APP_ADJ = 0;  
  73.   
  74. // This is a system persistent process, such as telephony.  Definitely  
  75. // don't want to kill it, but doing so is not completely fatal.  
  76. // ZMS:系统常驻进程,比如电话。绝对不想要杀死它,但是即便杀死也不是很致命。  
  77. static final int PERSISTENT_PROC_ADJ = -12;  
  78.   
  79. // The system process runs at the default adjustment.  
  80. // ZMS:系统进程  
  81. static final int SYSTEM_ADJ = -16;  
  82.   
  83. // Special code for native processes that are not being managed by the system (so  
  84. // don't have an oom adj assigned by the system).  
  85. // ZMS:为native进程保留,他们不被系统管理。  
  86. static final int NATIVE_ADJ = -17;  
  87.   
  88. // Memory pages are 4K.  
  89. static final int PAGE_SIZE = 4*1024;  
  90.   
  91. // The minimum number of cached apps we want to be able to keep around,  
  92. // without empty apps being able to push them out of memory.  
  93. static final int MIN_CACHED_APPS = 2;  
  94.   
  95. // The maximum number of cached processes we will keep around before killing them.  
  96. // NOTE: this constant is *only* a control to not let us go too crazy with  
  97. // keeping around processes on devices with large amounts of RAM.  For devices that  
  98. // are tighter on RAM, the out of memory killer is responsible for killing background  
  99. // processes as RAM is needed, and we should *never* be relying on this limit to  
  100. // kill them.  Also note that this limit only applies to cached background processes;  
  101. // we have no limit on the number of service, visible, foreground, or other such  
  102. // processes and the number of those processes does not count against the cached  
  103. // process limit.  
  104. static final int MAX_CACHED_APPS = 24;  
  105.   
  106. // We allow empty processes to stick around for at most 30 minutes.  
  107. static final long MAX_EMPTY_TIME = 30*60*1000;  
  108.   
  109. // The maximum number of empty app processes we will let sit around.  
  110. private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS);  
  111.   
  112. // The number of empty apps at which we don't consider it necessary to do  
  113. // memory trimming.  
  114. static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2;  
  115.   
  116. // The number of cached at which we don't consider it necessary to do  
  117. // memory trimming.  
  118. static final int TRIM_CACHED_APPS = ((MAX_CACHED_APPS-MAX_EMPTY_APPS)*2)/3;  
  119.   
  120. // Threshold of number of cached+empty where we consider memory critical.  
  121. static final int TRIM_CRITICAL_THRESHOLD = 3;  
  122.   
  123. // Threshold of number of cached+empty where we consider memory critical.  
  124. static final int TRIM_LOW_THRESHOLD = 5;  
  125.   
  126. // These are the various interesting memory levels that we will give to  
  127. // the OOM killer.  Note that the OOM killer only supports 6 slots, so we  
  128. // can't give it a different value for every possible kind of process.  
  129. private final int[] mOomAdj = new int[] {  
  130.         FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ,  
  131.         BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ  
  132. };  
  133. // These are the low-end OOM level limits.  This is appropriate for an  
  134. // HVGA or smaller phone with less than 512MB.  Values are in KB.  
  135. private final long[] mOomMinFreeLow = new long[] {  
  136.         81921228816384,  
  137.         245762867232768  
  138. };  
  139. // These are the high-end OOM level limits.  This is appropriate for a  
  140. // 1280x800 or larger screen with around 1GB RAM.  Values are in KB.  
  141. private final long[] mOomMinFreeHigh = new long[] {  
  142.         491526144073728,  
  143.         8601698304122880  
  144. };  

以上代码的后面又两个数组,定义了开始回收的阈值(单位KB)。

adb shell#cat /sys/module/lowmemorykiller/parameters/minfree

查看机器当前的设定,结果乘以PAGE_SIZE 即是对应的内存阈值。


---------------------------------------------------------------------------------------

修改指定包名应用的oom_adj,避免被系统回收:

Android 4.4:frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

Android 5.1:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java


[java] view plain copy
  1. @@ -8903,6 +8903,11 @@ public final class ActivityManagerService extends ActivityManagerNative  
  2.              app.persistent = true;  
  3.              app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;  
  4.          }  
  5. +       if("com.zms.test".equals(info.packageName)){  
  6. +           app.persistent = true;  
  7. +           app.maxAdj = ProcessList.SYSTEM_ADJ;  
  8. +       }  
  9.          if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {  
  10.              mPersistentStartingProcesses.add(app);  
  11.              startProcessLocked(app, "added application", app.processName);  

原创粉丝点击