当Android运行了很多进程后,由于内存不足,系统会杀掉一下不重要的进程,提供内存给更重要的进程使用。那么系统是如何对进程的“重要性“进行判定的呢?
命令行中输入cat /proc/process_id/oom_adj 可以查看到某一进程的adj数值,系统就是根据这一数值来判定进程是否被杀或者保留。
ADJ等级定义在frameworks/base/services/core/java/com/android/server/am/ProcessList.java
// 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.)
static final int UNKNOWN_ADJ = 16;
// This is a process only hosting activities that are not visible,
// so it can be killed without any disruption.
// 只持有不可用activity的进程,可以随时被杀掉
static final int CACHED_APP_MAX_ADJ = 15;
static final int 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.
// B List中的老旧,不常用的服务
static final int 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.
// 上一个应用,因为用户很可能切换回上一个应用,所以显得格外重要
static final int 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.
// 桌面程序的进程,使用率高
static final int 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.
// 应用程序服务进程
static final int 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.
// 后台重量级应用进程
static final int 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.
// 托管备份操作的进程
static final int 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.
// 持有用户可感知组件的进程,他不会立即可见。比如后台音乐播放器
static final int 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.
// 用户可见的activity进程
static final int VISIBLE_APP_ADJ = 1;
// 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 = -11;
// 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 = -12;
// The system process runs at the default adjustment.
// 系统进程
static final int 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).
// native本地进程,不由系统管理,所以不会分配oom_adj
static final int NATIVE_ADJ = -17;
来看一个实例:
我们经常会有这样的需求:希望我们程序的某一个服务尽可能的不被系统杀掉。我们常用的服务有如下几种,可以通过查看oom_adj来查看他们的优先级。
1.同一进程中的后台服务
2.同一进程中的前台服务
3.新的进程中的后台进程
4.新的进程中的前台进程
对比上面四种情况,2和4是比较稳定的,但是使用新的进程启动服务,可以在不需要主进程的时候,手动杀掉主进程而不会影响到服务进程,是应用内存使用进一步降低。因此,4应该是最好的方法。
前台进程:
一定要有一个notification与之相绑定。
// 设置为前台服务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("tag", "MusicService onStartCommand");
Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
builder.setSmallIcon(R.drawable.ic_music_note_white_18dp);
builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_music_note_white_36dp));
builder.setContentTitle("下拉通知中的标题");
builder.setContentText("下拉通知中要显示的内容");
builder.setWhen(System.currentTimeMillis());
Intent clickIntent = new Intent(this, MainActivity.class);
builder.setContentIntent(PendingIntent.getActivity(this, 0, clickIntent, 0));
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
startForeground(100, notification);
return super.onStartCommand(intent, flags, startId);
}
// 结束前台服务
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true); //参数表示是否移除通知
}