【Android 进程保活】oom_adj 值 ( oom_adj 值对应的进程优先级 | oom_adj 值动态改变 | 进程保活优化方向 )
文章目錄
- 一、oom_adj 值對應的進程優(yōu)先級
- 二、oom_adj 值動態(tài)改變
- 1、正常運行時的 oom_adj 值
- 2、按下 Home 鍵后的 oom_adj 值
- 3、按下回退鍵后的 oom_adj 值
- 二、進程保活優(yōu)化方向
一、oom_adj 值對應的進程優(yōu)先級
oom_adj 值對應的進程優(yōu)先級 : 優(yōu)先級從上到下越來越高 , 最下面的優(yōu)先級最高 , 最上面的優(yōu)先級最低 ;
- UNKNOWN_ADJ = 16 : 緩存進程 ;
- CACHED_APP_MAX_ADJ = 15 : 不可見進程的 oom_adj 最大值 ;
- CACHED_APP_MIN_ADJ = 9 : 不可見進程的 oom_adj 最小值 ;
- SERVICE_B_ADJ = 8 : 在 B 列表中的 Service 服務 , 這些服務比較老 , 再次使用的可能性較小 ;
- PREVIOUS_APP_ADJ = 7 : 上一個應用程序進程 , 即上一次按下返回鍵退出的應用 , 緩存應用中的第一個應用 ;
- HOME_APP_ADJ = 6 : Home 應用所在的進程 , 不能殺 ;
- SERVICE_ADJ = 5 : 服務進程 ;
- HEAVY_WEIGHT_APP_ADJ = 4 : 后臺的重量級繼承 , 啟動時在 system/rootdir/init.rc 配置中設置 ;
- BACKUP_APP_ADJ = 3 : 進入后臺的進程 , 按下 Menu 鍵可查看 , 備份進程 , 可喚醒 ;
- PERCEPTIBLE_APP_ADJ = 2 : 可感知進程 , 如后臺播放音樂 , 鈴聲 , 震動 , 閃光燈 等除視覺外的其它可感知效果 ;
- VISIBLE_APP_ADJ = 1 : 可見進程 ;
- FOREGROUND_APP_ADJ = 0 : 前臺進程 ;
- PERSISTENT_SERVICE_ADJ = -11 : 系統(tǒng)或持久化進程綁定的進程 ;
- PERSISTENT_PROC_ADJ = -12 : 系統(tǒng)持久化進程 , 如電話進程 ;
- SYSTEM_ADJ = -16 : 系統(tǒng)進程 ;
- NATIVE_ADJ = -17 : 本地進程 , 不受系統(tǒng)控制 ;
打印出來的值是上述值 , 不是常量中定義的值 ;
這些 ADJ 值都在 frameworks/base/services/core/java/com/android/server/am/ProcessList.java 源碼中以常量形式記錄 :
/*** Activity manager code dealing with processes.*/ public final class ProcessList {// 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.static final int SERVICE_B_ADJ = 800;// 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.static final int PREVIOUS_APP_ADJ = 700;// 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.static final int HOME_APP_ADJ = 600;// This is a process holding an application service -- killing it will not// have much of an impact as far as the user is concerned.static final int SERVICE_ADJ = 500;// 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.static final int HEAVY_WEIGHT_APP_ADJ = 400;// 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.)static final int UNKNOWN_ADJ = 1001;// This is a process only hosting activities that are not visible,// so it can be killed without any disruption.static final int CACHED_APP_MAX_ADJ = 906;static final int CACHED_APP_MIN_ADJ = 900;// This is a process currently hosting a backup operation. Killing it// is not entirely fatal but is generally a bad idea.static final int BACKUP_APP_ADJ = 300;// This is a process currently hosting a backup operation. Killing it// is not entirely fatal but is generally a bad idea.static final int BACKUP_APP_ADJ = 300;// 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.static final int PERCEPTIBLE_APP_ADJ = 200;// This is a process only hosting activities that are visible to the// user, so we'd prefer they don't disappear.static final int VISIBLE_APP_ADJ = 100;// This is the process running the current foreground app. We'd really// rather not kill it!static final int FOREGROUND_APP_ADJ = 0;// This is a process that the system or a persistent process has bound to,// and indicated it is important.static final int PERSISTENT_SERVICE_ADJ = -700;// This is a system persistent process, such as telephony. Definitely// don't want to kill it, but doing so is not completely fatal.static final int PERSISTENT_PROC_ADJ = -800;// The system process runs at the default adjustment.static final int SYSTEM_ADJ = -900;// Special code for native processes that are not being managed by the system (so// don't have an oom adj assigned by the system).static final int NATIVE_ADJ = -1000;// Memory pages are 4K.static final int PAGE_SIZE = 4*1024;}參考源碼 : frameworks/base/services/core/java/com/android/server/am/ProcessList.java
二、oom_adj 值動態(tài)改變
1、正常運行時的 oom_adj 值
一個程序的 oom_adj 值是不斷動態(tài)改變的 , 當程序處于前臺時 , 該前臺進程的 oom_adj 的值為 0 ,
運行程序 ,
在 Android Studio 中 , 可以看到該運行的程序的進程號 PID 為 308563085630856 ,
進入 adb shell 命令行 , su 獲取 root 權限 , 使用如下命令 , 查詢指定 PID 的 oom_adj 值 ;
cat /proc/30856/oom_adj2、按下 Home 鍵后的 oom_adj 值
點擊 Home 鍵 , 程序退出 , 顯示 Home 程序 ,
此時查詢該 PID 為 308563085630856 的 oom_adj 值為 121212 , 不可見進程范圍是 999 ~ 151515 , 此時的狀態(tài)是不可見狀態(tài) ;
3、按下回退鍵后的 oom_adj 值
如果是按下回退鍵退出界面 , 此時查詢該 PID 為 308563085630856 的 oom_adj 值為 161616 , 此時進程的狀態(tài)是緩存進程 , 隨時都可能被殺掉 ;
二、進程保活優(yōu)化方向
優(yōu)先級越高 , oom_adj 值越小 , 越不能被殺死 ;
如果想要使得進程盡可能長的保留在內存中 , 那么就要減小 oom_adj 的值 ;
在 oom_adj 值相同時 , 內存占用量越大的進程 , 被殺的幾率就越高 , 因此這里還要盡可能降低進程占用盡可能少的內存 ;
總結一下就是 ① 降低 oom_adj 值 , ② 減小內存占用 ;
總結
以上是生活随笔為你收集整理的【Android 进程保活】oom_adj 值 ( oom_adj 值对应的进程优先级 | oom_adj 值动态改变 | 进程保活优化方向 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 进程保活】oom_ad
- 下一篇: 【Android 进程保活】提升进程优先