Android Process Priority: Low level was lost Kill

I. Definition

  • Android system, when there is insufficient memory will kill off some programs (or processes) to release program memory to be used needs to run.
  • The higher the priority of the process, will finally kill off, on the contrary, the lowest priority is kill.

Two. Android system in the existing process

1.Zygote process

Zygote, the fertilized egg mean ... in the Android system, all system services and processes SystemServer application process is a process Zygote Fork (breeding) out.

2.App main process

Each App is running in a separate process, of course, this process is a process Zygote Fork out. App name is the package name of the process by AMS (ActivityManagerService) management Incidentally send BroadcastReceiver is also done by the AMS.

3.App worker process

App can have a number of processes, in particular AndroidManifest.xmlfor the Activity or Service carried out a configuration file android:process="". Similarly, the process is assisted by the Zygote Fork out by the AMS management.

4.Native process

Android Dalvik run in a virtual machine, NDK allows the user to execute a part program using the native code language similar to the C / C ++ or the like. Fork out by the NDK The process is called Native process. Native high degree of freedom can not AMS process management. Another, NDK can also be used to do the process keep alive.

Priority three. Android system processes

Priority Defines the Android system processes in the com.android.server.am.ProcessListclass, this class can be found in the SDK.
Mainly in the following priority definitions (oom_adj value):

name Explanation
UNKNOWN_ADJ = 16 Set aside lowest level cache may be set to use this
CACHED_APP_MAX_ADJ = 15 Activity invisible hold the process is a background process, the system is low on memory Kill
CACHED_APP_MIN_ADJ = 9 Caching process, a process that is empty
SERVICE_B_ADJ = 8 Inactive process
PREVIOUS_APP_ADJ = 7 Switching process, the process saved before switching applications
HOME_APP_ADJ = 6 Home to interact with the process
SERVICE_ADJ = 5 Service of process contains, in general, Kill out the impact on users is not too large
HEAVY_WEIGHT_APP_ADJ = 4 High weighting process, the system runs in the background but try not to fall Kill
BACKUP_APP_ADJ = 3 Managed backup process, Kill off is a bad idea
PERCEPTIBLE_APP_ADJ = 2 It may be perceived by the user process, such as music players
VISIBLE_APP_ADJ = 1 Visible process, the user can see the process, but not necessarily a foreground process, it may be covered Activity
FOREGROUND_APP_ADJ = 0 Foreground process running in the foreground process App
PERSISTENT_SERVICE_ADJ = -11 Important processes, systems or processes important binding process
PERSISTENT_PROC_ADJ = -12 Core processes, continuous process systems, such as telephone
SYSTEM_ADJ = -16 System Process
NATIVE_ADJ = -17 Native relevant processes help system administration

View process priority:

 

  //oom_adj的值就是进程的优先级
  //查看oom_adj值
  cat /proc/${pid}/oom_adj

Four. Android system common process

From highest to lowest priority:

1. foreground process (Foreground process)

Meet the following conditions be considered as a foreground process:

  • Activity process holds a positive interaction with the user.
  • Process holds one of the following conditions are met Service:
    1, and Activity is interacting binding;
    2, startForeground()start the front desk;
    3, Service is implementing lifecycle methods, onCreate(), onStart(), or onDestroy().
  • Executing a process holds onReceive()the BroadcastReceiver.

2. visible process (Visible process)

  • Process holds a visible Activity, such as Dialog is blocked into the onPause()Activity state.
  • Process and hold a visible (or foreground) Service Activity binding.

3 service process (Service process)

  • Use a process holds startService()open the service, and does not belong to two or more higher priority Service, it is regarded as a service process. Such as background music, download files.

4 background process (Background process)

  • Process holds invisible call onStop()but did not call onDestroy()the Activity, is considered to be a background process. The reason for this is to save some of Activity for the user to select and jump. They will be stored in a LRU (least recently used) list, it may fall Kill the earliest Activity insufficient system memory can be rewritten onSaveInstanceState()to store data before it is Kill.

5 empty process

  • Process does not contain active components, the process is considered to be empty. The reason the preservation of this process is the need for caching.

V. improve the process priority

  • Configure AndroidManifest.xmlandroid:persistent="true"
  • Open thread to do time-consuming operation in the Service, the service process will be judged.
  • startForeground()Start the front desk, remember to use stopForeground()closed.
  • NotificationManager interact with, let the process become perceptible process.
  • Broadcast, to avoid becoming empty process.
  • Use Native process to make the process keep alive (just a thought).

References:

Android App platform process priority
Android summing up - process priority and priority ways to improve (Service immortality of the law as much as possible

Reprinted: https://www.jianshu.com/p/35727ad2296a 

发布了49 篇原创文章 · 获赞 2 · 访问量 8586

Guess you like

Origin blog.csdn.net/yangjunjin/article/details/105030775