Android uses lowmemorykiller driver to kill selected process and free the memory, when some kind of low memory thresholds is met. Two files are generated in /sys file system for system tuning.

1. /sys/module/lowmemorykiller/parameters/adj
This file includes the oomadj array. If the corresponding minfree data is met, one of the process which oomadj are larger than this number will be killed.
About oomadj, the factor is introduced by OOMkiller module in the kernel. It is a property of the process. The range is from -17 to +15, the higher the score, more likely the associated process is to be killed by oom-killer. Lowmemorykiller use this score as well.
http://www.chineselinuxuniversity.net/articles/19534.shtml

2. /sys/module/lowmemorykiller/parameters/minfree
This file includes the threshold numbers for minfree memory size. The unit is page.

Example in Android
The default setting is in system/core/rootdir/init.rc.
# Write value must be consistent with the above properties.
write /sys/module/lowmemorykiller/parameters/adj 0,1,2,7,14,15

write /proc/sys/vm/overcommit_memory 1
write /sys/module/lowmemorykiller/parameters/minfree 1536,2048,4096,5120,5632,6144

# Set init its forked children's oom_adj.
write /proc/1/oom_adj -16

This means if the free memory is smaller than 1536*4K=6MB, one of the processes which adj is larger than 0 will be killed. If the free memory is smaller than 2048*4K=8MB, the process adj larger than 1 will be killed. And so on.

On the other hand, Android put the processes into 6 classes.
# Define the oom_adj values for the classes of processes that can be
# killed by the kernel. These are used in ActivityManagerService.
setprop ro.FOREGROUND_APP_ADJ 0
setprop ro.VISIBLE_APP_ADJ 1
setprop ro.SECONDARY_SERVER_ADJ 2
setprop ro.HIDDEN_APP_MIN_ADJ 7
setprop ro.CONTENT_PROVIDER_ADJ 14
setprop ro.EMPTY_APP_ADJ 15

Say, the FOREGROUND application has the highest priority, the least likely to be killed. The CONTENT_PROVIDER and EMPTY_APP will be killed at first. Confusion here is we know the activity is the one people can see from UI. It actually means the process has foreground activity will have the highest priority, and the process which hosts the activity visible to user has the second priority.

Actually it defines two more priorities as below, in frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
// This is a process running a core server, such as telephony. Definitely
// don't want to kill it, but doing so is not completely fatal.
static final int CORE_SERVER_ADJ = -12;

// The system process runs at the default adjustment.
static final int SYSTEM_ADJ = -16;

In the ActivityManagerService, it starts some core service in a process and assigns it to SYSTEM_ADJ score.
ActivityManagerService.java did some other works as well. For example, it tracks which activity is running in background and foreground, and so it has use-count reference for the process. The java file is 11508 lines, like a monster.

To customize for some short-of-memory devices, we could change the threshold value accordingly. For example:
write /sys/module/lowmemorykiller/parameters/minfree 1536, 4096, 4096, 5120,5632,6144
This makes the driver kill the processes as early as possible. So the new application could get more memory.

These need some tunings and adjustment on hardware. I will try next week

原文地址: http://swcai.blogspot.com/2009/02/lowmemorykiller-in-android.html

更多相关文章

  1. 【转】Android(安卓)SDK版本与API Level对照表
  2. Android上运行QT项目Necessitas的基本原理
  3. Android(安卓)开源项目
  4. Qt之Qt5.7开发Android应用程序小试牛刀
  5. Android(安卓)SDK 在线更新镜像服务器资源
  6. Android(安卓)SDK 在线更新镜像服务器资源
  7. Android(安卓)根据坐标获取地址
  8. Building Android(安卓)and Linux Kernel for Nexus One
  9. 从零开始 WIN8.1 下Android(安卓)开发环境搭建

随机推荐

  1. win10下mysql 8.0.12 安装及环境变量配置
  2. mysql 8.0.15 安装图文教程及数据库基础
  3. Win10下mysql 8.0.15 安装配置图文教程
  4. win10家庭版64位下mysql 8.0.15 安装配置
  5. MySQL在不知道列名情况下的注入详解
  6. MYSQL本地安装以及出现的问题解决
  7. mysql 8.0.15 安装配置图文教程
  8. MySQL5.7中的JSON基本操作指南
  9. win10下MySQL 8.0登录Access denied for
  10. mysql 8.0.15 安装配置方法图文教程