15

I am writing this question not as part of any specific application, but as part of potential use for future applications. I know that the answers to this question can be very application specific, but I hope you will bear with me. I will convey my understanding as is, and hopefully you can help me by extending it. Surprisingly in vain, I have searched the web for a 'complete' overview and have not been able to find it. Any links to relevant pages are welcome obviously.

An android application by default runs in a single process. Each Activity or Service that you start will by default even run in the main thread. All actions by the user are queued by the Looper of the main thread, and the respective callbacks are handled in the main thread.

In order to provide concurrency, threads can be started in lots of different ways, single or in pools. There is not explicit need in this regard for multiple processes. Using multiple processes to allow the multiple cores of your device to work in parallel is not necessary since Threads can be run in parallel as well, maybe even your main thread? But perhaps it will be easier to actually achieve?

In order to let an Activity or Service work in a specific (potentially different) process you only set the android:process attribute in the Manifest file. So easy to implement?

The Android framework is specifically built for mobile devices, which typically have limited memory to go around with. Therefore processes can be killed under a variety of circumstances, clearly stated here. As long as you implement the lifecycle callbacks of Activities and Services, such as onStop or onDestroy, this should not give any real problems. But obviously compartmentalizing pieces of your application, by using multiple processes, could potentially keep more important things alive. For instance a background download Service could be kept alive (level 3 in importance), while the process with the initial Activity that started this Service, now in the background (level 4) could be freed for its resources. Furthermore the fact that you are isolating core features of your application might allow your device to make better use of its resources?

The Binder framework makes IPC fairly easy to handle, and is something you will use generally, regardless of actually using multiple processes. I think the main cost of having multiple processes to the application is the access to shared resources, or the sending of those resources between processes, excluding the additional resources required for forking a process from the Zygote. I wonder whether using multiple processes will actually force you to implement your application differently?

I think that conceptually the use of multiple processes will not increase ease of implementation?

To summarize the pros of multiple processes:

  • Isolation potentially gives more lifetime protection.
  • Compartmentalization gives the device more manoeuvrability in regaining resources.
  • Parallelization performance boost?

Cons:

  • A new process must be forked from the Zygote using resources
  • Within an application resources will need to be shared over multiple processes, which strain both the device and the perhaps application performance.

The main use case I can think of:

  • Use a foreground activity for any user interactions and run a continuously used bound Service to do useful synchronization that might outlive the session that the user has with your activity and/or application.

If you have any remarks on my understanding please state so (Note several question marks in my explanation). If you have any pros and/or cons to add please reply as well, I will add them to the list.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Vincent Ketelaars
  • 1,069
  • 1
  • 14
  • 35
  • This should help you: http://developer.android.com/training/articles/memory.html#MultipleProcesses Other than that voted to close this, because it is too broad. – MaciejGórski Nov 02 '13 at 17:54
  • @MaciejGórski. Obviously I don't agree with you there, pros and cons questions are all over the places on a variety of topics, but I do appreciate the link. Thanks. – Vincent Ketelaars Nov 02 '13 at 18:40
  • Limited heap memory is the main reason and stability. – Lothar Aug 09 '16 at 21:43

1 Answers1

7

Using multiple processes to allow the multiple cores of your device to work in parallel is not necessary since Threads can be run in parallel as well, maybe even your main thread?

Live (non-blocked) threads will be run in parallel across multiple cores automatically.

But perhaps it will be easier to actually achieve?

I would consider threads easier than processes, but "easier" is generally an opinion.

For instance a background download Service could be kept alive (level 3 in importance), while the process with the initial Activity that started this Service, now in the background (level 4) could be freed for its resources.

Except that you wasted more resources by having the two processes in the first place, and keeping a service running for long periods of time is generally an anti-pattern.

The Binder framework makes IPC fairly easy to handle, and is something you will use generally, regardless of actually using multiple processes

Developers do not normally use Binder directly. Only those implementing bound services need it, and that is a fairly small percentage of Android apps.

I wonder whether using multiple processes will actually force you to implement your application differently?

Yes.

Isolation potentially gives more lifetime protection

IMHO, this is not a valid excuse for wasting RAM, CPU, and battery on multiple processes.

Parallelization performance boost?

Threads cover this.

The main use case I can think of

While there may be scenarios in which your use case actually is a net benefit to the user, it is far from certain. Given that you have to implement your app differently to handle multiple processes, using multiple processes is a last resort approach, not the sort of thing that you do routinely, IMHO.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The most important reason is that processes are very memory limited. So for example an Image browser could start a second "cache" thread to store another 48MB (or 128MB) of heap memory. – Lothar Aug 09 '16 at 21:42
  • 1
    @Lothar: It is simpler to use `android:largeHeap="true"` and avoid the need for IPC. – CommonsWare Aug 09 '16 at 21:52
  • Yes and then you still get a doubled heap of maybe 48 MB still way to small. I think 128MB was the largest permission i have heared of (not doing a lot of Android in the last years), – Lothar Aug 10 '16 at 07:04
  • 1
    @Lothar: "Yes and then you still get a doubled heap of maybe 48 MB still way to small" -- then why did you suggest a solution that only gives double the heap, if you feel that would be too small? `android:largeHeap` will give whatever the manufacturer decides to give, which for high-end devices will be far more than twice the regular heap limit. – CommonsWare Aug 10 '16 at 10:57