1852人阅读 评论(0)

# 进程

1. Zygote进程
这个是Android框架的主要进程，所有的App进程以及系统服务进程SystemServer都是由Zygote进程Fork出来的

2. App的主进程
每一个App的运行都是在一个独立的进程，进程的名字就是App的packagename，这些进程都是从Zygote进程Fork出来的，并受AMS（ActivityManagerService）管理

3. App的辅助进程
可以允许App有多个进程，在AndroidManifest.xml里边配置android:process属性，就可以开启多进程，这些进程名字都是packagename:name这种，以区分是属于哪个App，我一般称之为辅助进程。但这些进程也都跟主进程一样，也是从Zygote进程Fork出来的，并受AMS管理

4. Native进程
Android除了使用Java，还有NDK，可以使用C/C++去开发，然后这里也是可以Fork出进程的，我一般称之为Native进程，Native进程可以不受AMS管理，自由度很大，本文暂且不讲

# 如何查看App进程优先级

//oom_adj的值就是进程的优先级，
cat /proc/${pid}/oom_adj 下面以查看微信的oom_adj为例，先通过ps | grep com.tencent.mm得到进程名包含“com.tencent.mm”的微信进程pid。ps命令使用详解可参考这里 # 优先级 App进程的优先级是在com.android.server.am.ProcessList类里边定义的，这个类在Android的API里边是找不到的，要想看看里边的实现可以去Android SDK里边去找，位于${android-sdk-path}\sources\android-23\com\android\server\am\ProcessList.java

预留的最低级别，一般对于缓存的进程才有可能设置成这个级别

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.)

缓存进程，空进程，在内存不足的情况下就会优先被kill

This is a process only hosting activities that are not visible, so it can be killed without any disruption.

缓存进程，也就是空进程

不活跃的进程

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.

切换进程

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.

与Home交互的进程

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.

有Service的进程

This is a process holding an application service – killing it will not have much of an impact as far as the user is concerned.

高权重进程

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.

正在备份的进程

This is a process currently hosting a backup operation. Killing it is not entirely fatal but is generally a bad idea.

可感知的进程，比如那种播放音乐

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.

可见进程

This is a process only hosting activities that are visible to the user, so we’d prefer they don’t disappear.

前台进程
This is the process running the current foreground app. We’d really rather not kill it!

重要进程

This is a process that the system or a persistent process has bound to, and indicated it is important.

核心进程

This is a system persistent process, such as telephony. Definitely don’t want to kill it, but doing so is not completely fatal.

系统进程

The system process runs at the default adjustment.

系统起的Native进程

Special code for native processes that are not being managed by the system (so don’t have an oom adj assigned by the system).

# lowmemorykiller 机制

1. 向系统注册lowmem_shrinker回调，当系统空闲内存不足时调用，去杀进程

2. 把空闲内存低于多少，就去杀那个级别的进程，我称之为策略

3. 杀的策略由application层根据内存状况指定，写入文件传递给驱动

6. 在死亡名单里面选一个占内存最大的进程kill掉

• minfree：列举6个内存阀值。
比如：32768,61440,73728,129024,147456,184320

比如：0,1,2,3,9,15

# trimApplications机制

trimApplications是一个方法，定义在ActivityManagerService里边，Android API里边也是没有这个类的，得去SDK里边找，位于\${android-sdk-path}\sources\android-23\com\android\server\am\ActivityManagerService.java

1. package已被卸载的无用进程会被Kill
2. persistent的App会被优先照顾，进程优先级设置为
6. 进程里边没有任何的Activity的存在优先被杀
7. 空进程最容易被杀

# 如何提高后台App进程的优先级

• AndroidManifest.xml中配置persistent属性
<application android:name="App"
android:persistent="true"
android:label="@string/dialerIconLabel"
android:icon="@drawable/ic_launcher_phone">
• 重载back按键事件，让activity在后台运行，不要Destory
• 开Service，并设置前台运行方式
• 发送/接收广播，别让自己变成空进程

Android平台App进程优先级

2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：600012次
• 积分：7867
• 等级：
• 排名：第2621名
• 原创：172篇
• 转载：134篇
• 译文：0篇
• 评论：201条
博客专栏
 Android WiFi 开发 文章：6篇 阅读：9381
 Android Studio编译或构建错误汇总 文章：12篇 阅读：46829
最新评论
感谢你的关注