Android 的内存管理

(1) 在init.rc中 系统定义了

Lowmeme_adj中各项数值代表阈值的警戒级数,lowmem_minfree代表对应级数的剩余内存

 

 

当系统的剩余内存为小于6MB时候,警戒级数为0,当系统内存剩余小于8M而大于6M的时候,警戒级数为1,当内存小于64M大于16MB的时候,警戒级数为12.Low memory killer 的规则就是根据当前系统的剩余内存多少来获取当前的警戒级数,如果进程的oom_adj大于警戒级数并且最大,进程将会被杀死(相同omm_adj的,则杀死占用内存较多的)。Omm_adj越小,代表进程越重要。一些前台的进程,oom_adj会比较小,而后台的服务,omm_adj会比较大,所以当内存不足的时候,Low memory killer 杀掉的必然先杀掉的是后台服务而不是前台的进程。

 

 

# 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.PERCEPTIBLE_APP_ADJ 2

    setprop ro.HEAVY_WEIGHT_APP_ADJ 3

    setprop ro.SECONDARY_SERVER_ADJ 4

    setprop ro.BACKUP_APP_ADJ 5

    setprop ro.HOME_APP_ADJ 6

    setprop ro.HIDDEN_APP_MIN_ADJ 7

    setprop ro.EMPTY_APP_ADJ 15

 

# Define the memory thresholds at which the above process classes will

# be killed.  These numbers are in pages (4k).

    setprop ro.FOREGROUND_APP_MEM 2048

    setprop ro.VISIBLE_APP_MEM 3072

    setprop ro.PERCEPTIBLE_APP_MEM 4096

    setprop ro.HEAVY_WEIGHT_APP_MEM 4096

    setprop ro.SECONDARY_SERVER_MEM 6144

    setprop ro.BACKUP_APP_MEM 6144

    setprop ro.HOME_APP_MEM 6144

    setprop ro.HIDDEN_APP_MEM 7168

    setprop ro.EMPTY_APP_MEM 8192

 

 

(2) VM 的GC 一分钟轮循一次processGC列表

当满足以下条件时会回收内存

 

while (mProcessesToGc.size() > 0) {

                ProcessRecord proc = mProcessesToGc.remove(0);

                if (proc.curRawAdj > PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {

                    if ((proc.lastRequestedGc+GC_MIN_INTERVAL)

                            <= SystemClock.uptimeMillis()) {

                        // To avoid spamming the system, we will GC processes one

                        // at a time, waiting a few seconds between each.

                        performAppGcLocked(proc);

                        scheduleAppGcsLocked();

                        return;

                    } else {

                        // It hasn't been long enough since we last GCed this

                        // process...  put it in the list to wait for its time.

                        addProcessToGcListLocked(proc);

                        break;

                    }

                }

            }

 

1 process 不是前台进程,可视进程,及优先级低于 PERCEPTIBLE,可以关闭该进程的后台程序及释放其内存

 

2 process 在进程关闭之前断开binder连接,通知VM关闭java程序申请的内存 

 

(3)VM 回收内存类型

 

 

    /* Not enough space for an "ordinary" Object to be allocated. */

    GC_FOR_MALLOC,

    /* Automatic GC triggered by exceeding a heap occupancy threshold. */

    GC_CONCURRENT,

    /* Explicit GC via Runtime.gc(), VMRuntime.gc(), or SIGUSR1. */

    GC_EXPLICIT,

    /* GC to try to reduce heap footprint to allow more non-GC'ed memory. */

    GC_EXTERNAL_ALLOC,

    /* GC to dump heap contents to a file, only used under WITH_HPROF */

    GC_HPROF_DUMP_HEAP

 

 

 

 

你可能感兴趣的:(Android 的内存管理)