在Android OS 中每个应用程序都有一个自己进程,运行在自己的进程中,每个进程都有自己的ID即PID,这个ID有时候对我们开发很有用,那么如何来获取这个对应应用的PID呢?如何比较两个进程的优先级呢?下面我们就来进行详解:
1. 查看当前手机的进程
DOS下面cmd(win+r),然后打开adb shell,直接ps命令,显示当前手机所有进程。
如下图所示即为执行完ps命令后打印出的系统进程如下:
2.利用(ps|grep 条目名称)命令,过滤自己需要的进程
执行 ps |grep com.choicemmed
选择出指定包名的进程,如图选择的进程ID为12807
各列参数意义:
USER 进程当前用户;
PID Process ID,进程ID;
PPID Process Parent ID,进程的父进程ID;
VSIZE Virtual Size,进程的虚拟内存大小;
RSS Resident Set Size,实际驻留”在内存中”的内存大小;
WCHAN 休眠进程在内核中的地址;
PC Program Counter;
NAME 进程名;
3.比较进程优先级
进程的Importance等级以及adj值,决定了系统在资源吃紧的情况下该杀掉哪些进程。通过cat /proc/进程id/oom_adj可以看到当前进程的adj指,比如输入cat /proc/12807/oom_adj
cat查看进程的adj值后我们会得到其返回结果“0”,说明当前进程正位于前台。
同样,我按home键,把app退到后台,这个时候,再输入cat /proc/12807/oom_adj
cat 查看进程的adj值后得到其返回结果为“1”,说明当前进程位于后台。
相较于Importance等级而言adj值可以赋予我们更多的参考价值,从上述adj值的定义中我们可以看到,值越小优先级越高,比如native进程的adj值为-17,对于这个adj值的进程来说,系统根本不会动它一分一毫,实质上当进程的adj值去到2时系统就很少会因为其它原因而去杀死它,这些在研究进程保活中都非常重要。
final class ProcessList {
// The minimum time we allow between crashes, for us to consider this
// application to be bad and stop and its services and reject broadcasts.
static final int MIN_CRASH_INTERVAL = 60*1000;
// 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.
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.
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.
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 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).
static final int NATIVE_ADJ = -17;
// Memory pages are 4K.
static final int PAGE_SIZE = 4*1024;
// The minimum number of cached apps we want to be able to keep around,
// without empty apps being able to push them out of memory.
static final int MIN_CACHED_APPS = 2;
// The maximum number of cached processes we will keep around before killing them.
// NOTE: this constant is *only* a control to not let us go too crazy with
// keeping around processes on devices with large amounts of RAM. For devices that
// are tighter on RAM, the out of memory killer is responsible for killing background
// processes as RAM is needed, and we should *never* be relying on this limit to
// kill them. Also note that this limit only applies to cached background processes;
// we have no limit on the number of service, visible, foreground, or other such
// processes and the number of those processes does not count against the cached
// process limit.
static final int MAX_CACHED_APPS = 24;
// We allow empty processes to stick around for at most 30 minutes.
static final long MAX_EMPTY_TIME = 30*60*1000;
// The maximum number of empty app processes we will let sit around.
private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS);
// The number of empty apps at which we don't consider it necessary to do
// memory trimming.
static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2;
// The number of cached at which we don't consider it necessary to do
// memory trimming.
static final int TRIM_CACHED_APPS = ((MAX_CACHED_APPS-MAX_EMPTY_APPS)*2)/3;
// Threshold of number of cached+empty where we consider memory critical.
static final int TRIM_CRITICAL_THRESHOLD = 3;
// Threshold of number of cached+empty where we consider memory critical.
static final int TRIM_LOW_THRESHOLD = 5;
参考:感谢博主分享
如有疑问欢迎大家留言指正。祝大家生活愉快。
最后欢迎对Android开发感兴趣的老哥一起讨论。