In this topic you will learn some information about android process management. First let's take a look at the launched processes during Android booting.
User PID ppid vsize RSS wchan PC name
Root 1 0 264 176 c00acc6c unzip c36c S/init
Root 28 1 724 308 c0051354 afe0c4cc S/system/bin/sh
System 30 1 796 248 c026516c afe0b74c S/system/bin/servicemanager
Root 31 1 1824 316 ffffffff afe0b50c S/system/bin/mountd
Root 32 1 652 248 c02976e0 afe0c0bc S/system/bin/debugadh
Radio 33 1 5344 664 ffffffff afe0bdbc S/system/bin/rild
Root 34 1 71028 18828 c00ad308 afe0b874 s zygote
Media 37 1 16812 3456 ffffffff afe0b74c S/system/bin/mediaserver
Root 39 1 788 288 c02f9ae4 afe0b50c S/system/bin/installd
System 86 34 187756 21836 ffffffff afe0b74c s system_server
Radio 118 34 103476 13896 ffffffff afe0c824 s com. Android. Phone
App_4 124 34 117848 19248 ffffffff afe0c824 s Android. process. acore
App_5 139 34 98672 11516 ffffffff afe0c824 s com. Android. MMS
App_3 151 34 92096 10976 ffffffff afe0c824 s com. Android. alarmclock
App_6 161 34 94436 12616 ffffffff afe0c824 s com. Android. Calendar
App_9 173 34 93248 11728 ffffffff afe0c824 s Android. process. Media
App_15 182 34 91848 9764 ffffffff afe0c824 s com. Android. voicedialer
App_16 190 34 94524 10812 ffffffff afe0c824 s Android. process. Im
They can be divided into three kinds.
Root Process
Init is the first process after kernel booting. The major task it performs:
L parser and execute init. RC and init. % hardware %. RC
L automatically generate device node under/dev
L start log and property service
L Monitor for device, property set and child process exit events
Native application process
According to init. RC, init will fork the following native application process.
Console: Star a shell.
Servicemanager: Start binder IPC Service Manager.
Mountd: Mount all Fs defined in/system/etc/mountd. conf if started, receive commands through local socket to mount any FS.
Debugadh: Start debug system.
Rild: Start radio interface layer daemon.
Zygote: Start Android Java VM runtime and start system server. It's the most important process.
Mediaserver: Start audioflinger, mediaplayerservice and cameraservice.
Installd: Start install package daemon.
Java application process
Every Java application process is forked from zygote process. system_server is a special Java Process, which is directly forked from zygote .. other Java Process is created from activitymanagerservice (run in system_server process) like this.
Int pid = process. Start ("android. App. activitythread ",
Msimpleprocessmanagement? App. processname: NULL, uid, uid,
Gids, (app.info. Flags & applicationinfo. flag_debuggable )! = 0), null );
While process. Java use Unix domain socket to communicate with zygote. So the overall picture is shown as following.
System server
It's the first Java application launched by zygote. It starts the core Android services, e.g. activitymanager, windowmanager, packagemanager etc. It's the android Core Engine.
Persistent Application
During booting, activitymanagerservice. systemready will start all persistent applications.
List apps = activitythread. getpackagemanager ().
Getpersistentapplications (packagemanager. get_shared_library_files );
If (apps! = NULL ){
Int n = apps. Size ();
Int I;
For (I = 0; I <n; I ++ ){
Applicationinfo info
= (Applicationinfo) apps. Get (I );
If (info! = NULL &&
! Info. packagename. Equals ("android ")){
Addapplocked (Info );
}
}
}
Currently only phone application is registered as a persistent app in androidmanifest. xml like this.
<Application Android: Name = "phoneapp"
Android: Persistent = "true"
Android: Label = "@ string/dialericonlabel"
Android: icon = "@ drawable/ic_launcher_phone">
So during booting, only phone application is automatically launched. It's the "com. Android. Phone" process.
The first activity
The first activity is launched by senting intent. category_home intent from activitymanagerservice.
Intent intent = new intent (
Mtopaction,
Mtopdata! = NULL? Uri. parse (mtopdata): NULL );
Intent. setcomponent (mtopcomponent );
If (mfactorytest! = Systemserver. factory_test_low_level ){
Intent. addcategory (intent. category_home );
}
Activityinfo ainfo =
Intent. resolveactivityinfo (mcontext. getpackagemanager (),
Packagemanager. get_shared_library_files );
If (ainfo! = NULL ){
Intent. setcomponent (New componentname (
Ainfo. applicationinfo. packagename, ainfo. Name ));
// Don't do this if the Home app is currently being
// Instrumented.
Processrecord APP = getprocessrecordlocked (ainfo. processname,
Ainfo. applicationinfo. UID );
If (APP = NULL | app. instrumentationclass = NULL ){
Intent. setflags (intent. getflags () | intent. flag_activity_new_task );
Startactivitylocked (null, intent, null, null, 0, ainfo,
Null, null, 0, 0, 0, false );
}
}
It's the "android. process. acore" process. (The process name is defined in androidmanifest. XML)
Auto-launched application after booting
When activity idle is detected in activitymanagerservice, it will broadcast action_boot_completed intent at the first time.
If (mfactorytest! = Systemserver. factory_test_low_level ){
// Tell anyone interested that we are done booting!
Synchronized (this ){
Broadcastintentlocked (null, null,
New intent (intent. action_boot_completed, null ),
Null, null, 0, null, null,
Android. manifest. Permission. receive_boot_completed,
False, false, my_pid, process. system_uid );
}
}
Currently, MMS, alarmclock, calendar, mediaprovider, voicedialer and Im have registered as a guest ER for action_boot_completed intent in their androidmanifest. XML. so they will be automatically launched. (This explains the remained Java Process .)
Email also registers as a caller for action_boot_completed intent in its androidmanifest. XML, But it defines Android: Enable = "false". So it won't be launched.
<Cycler Android: Name = ". Service. bootreceiver"
Android: enabled = "false"
>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. device_storage_low"/>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. device_storage_ OK"/>
</Intent-filter>
</Cycler>
Downloadprovider also registers as a receiver ER for action_boot_completed intent in its androidmanifest. XML, But it defines Android: exported = "false". So it won't be launched.
<Javaser Android: Name = ". downloadcycler" Android: exported = "false">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
<Action Android: Name = "android.net. Conn. connectivity_change"/>
</Intent-filter>
</Cycler>
Behind the Java Process
System_server is a special case. It cballs activitythread. Java's systemmain static function, which creates an instance of activitythread. activitythread then creates an instance of applicationthread, application and applicationcontext.
Every other Java Process works in a different way. it's controlled by system_server while forked by zygote. when any JAVA process other than system_server is forked from zygote, it fig. java's main static function (see process. java and the following code snippet ).
Try {
Zygoteinit. invokestaticmain (cloader, classname, mainargs );
} Catch (runtimeexception ex ){
Logandprinterror (newstderr, "error starting.", ex );
}
the activitythread. java's main function creates an instance of activitythread. activitythread then creates an instance of applicationthread. the applicationthread will work as an ibinder object to interact with activitymanagerservice in system_server. the new process does nothting at this time other than waiting IPC call from system_server. the application and applicationcontext object won't be created at this time. actually it's deferred to when the process really works, eg. start an activity, receive intent or start a service.
for example, when start an activity, activitymanagerservice know which process the to-be-launched activity shoshould run in, so it will RPC call applicationthread's schedulelaunchactivity to launch a new activity in that process. applicationthread then post a message to let activitythread know it needs to start an activity. activitythread then creates application and applicationcontext object. after that, it CILS instrumentation, then instrumentation finally CILS Java Dalvik VM to really create an activity Java object.