Android Process
What is process in OS?
In OS, process is an environment which executes machine level code (0’s and 1’s are machine readable instructions). In Android OS, Process is an Instance which has ART/Dalvik VM( run time environment which executes .Dex format code which present in apk file), memory space and resource file.
For Ex, If user clicks a music player app in the phone, the android system creates a process. The process have a new instance of ART/Dalvik VM, memory space (like heap,stack )for instruction execution and resource files
Does the process in OS alone handle application?
Nope, Along with process, system creates a new thread called “Main Thread”. This main thread will handle all the code execution, user input action’s. By default, each process has minimum one thread to handle execution. Programmatically, we can able create and handle multiple thread for simultaneous execution.
By short Example, If you want to build a house, you need Environment/Land, Materials and workers.Next, Workers build your house on the land using those materials. Similarly Android process is the environment which executes the instruction(code) within the thread. Memory space stores the object,reference’s and value in runtime
Does Android Support Multiple process for single app?
Absolutely Yes.
We can run each individual component like (Activity, service, Receiver etc.) in Individual process. To run in individual process we have to set android: process=”process_name” attribute in AndroidManifest.xml file.
In below code snippets have two activity with two different process name.
Get Raja kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
AndroidManifest.xml
In MainActivity.java, just set the name of Main Thread as “ProcessOneMain”. After that, thread name, thread Id and process id are printed using Log
Main2Activity.java, just print the thread name, thread Id and process id in Log.
Output:
In the below output, If both Activity have same process then the process Id, thread Id and Thread name(“ProcessOneMain” which is the name set to thread in MainActivity.java) are same for both activities.
If two Activity has Individual process then the Thread name and process id’s are different for both activity. MainActivity.java has thread name as “ProcessOneMain” and Main2Activity.java has “main” as thread name,because both activity run in two different process. Each process has their own Main Thread, So the two activity’s has their own process and Main Thread
Process lifecycle:
The system automatically kill the process if the system needs memory to perform some prior task. Below image represents the priority order
1. Cached process means, the process is alive in the background, but it does not have any visible activity or does not have any background service is currently alive. When the system need memory, It first kills all the available Cached process
2.Service process means, the process that handles background service using startService(). If the system needs memory for a currently visible application, then the process is killed if the service is alive system doesn’t care. Background process which run’s more than 30 mins are also consider as Service Process
3. Visible process are, the process that handle’s some foreground service or any activity in pause state.
4. Foreground process are, the process have any activity in onResume() state or service currently execute onCreate() are considered as foreground process. The system doesn’t care about the user interaction with the activity, But this state is last chance for system to recover memory if needed.
Thanks for reading till the last…. If you really like this post give me the clap. Leave your thought in the comment








