This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we’ll learn about what a Process is and explore how Android decides which Processes to kill in a low memory situation!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
We've learned all about threads and
services and we've already seen the word
0:04
process a few times, but
we still don't know much about processes.
0:09
Let's fix that by taking a closer
look at process in Android.
0:14
Let's start in the Android device monitor.
0:20
Everything over here on
the left is a process, and
0:26
if we click to show the threads,
say for our music machine app,
0:30
we can see the threads
associated with this process.
0:35
So a good way to think about processes
would be as a group of threads.
0:40
We already know that by default
everything in an app runs on one thread,
0:45
the main thread.
0:49
Now let's add to that that by default,
each app also runs in its own process.
0:51
So when we launch an app, all of the code
execution will happen on the main thread
0:58
and this thread will be
a part of our apps process.
1:03
When we create new threads,
they're also a part of our apps process.
1:06
Remember when we were trying to download
using just a thread and no service?
1:12
When we killed our apps process,
the download stopped.
1:17
This is because the new thread we
created was part of our app's process.
1:21
So when we killed the process,
we killed the thread.
1:25
However, we are not
limited to one process.
1:29
There are a few occasions
where it might make sense for
1:34
an app to run in more than one process.
1:36
A good example of this is,
not surprisingly, a music player.
1:39
In Android, each process is only allowed
to use a certain amount of memory
1:44
based on the total amount
of RAM on the device.
1:48
Using one process for our activity and
a separate process for
1:51
our service gives both
access to more memory.
1:55
But more than that,
having two separate processes lets
2:00
Android reclaim the activities
process when it's no longer needed.
2:04
If we've been listening to
music in the background and
2:09
haven't interacted with
the activity in a while,
2:11
then Android will be able to kill
just the activities process.
2:14
This way,
2:19
we can free up system resources while
making sure that our music keeps playing.
2:20
Cool, right?
2:24
[SOUND] Another important thing about
processes is how they get killed.
2:25
We already know that when
Android needs to reclaim memory,
2:31
it will start killing processes.
2:35
But how does Android pick
which processes to kill?
2:37
Well luckily, Android has a method
to determine which processes to kill
2:41
first and which to kill last.
2:46
It does this by grouping processes into
five groups of varying importance.
2:49
The first group is foreground processes.
2:54
These are the most important processes and
will be the last to be killed.
2:57
More specifically these are processes that
a user is currently interacting with.
3:02
When we're clicking buttons
in our music machine app,
3:07
our apps process would be
considered a foreground process.
3:11
The second group is visible processes.
3:15
These are processes that don't have
any foreground components, but
3:18
can still affect what
the user sees on the screen.
3:22
An activity in the pause state would
likely be running in a visible process.
3:25
The third group is service processes.
3:31
This is for processes which
are running started services.
3:33
The importance level of a process running
a bound service, on the other hand,
3:38
is determined by the importance level
of what the service is bound to.
3:42
The fourth group is background processes.
3:47
If our activity isn't visible,
3:50
then our apps process is
likely a background process.
3:52
The fifth, and least important group,
is empty processes.
3:56
An empty process is what's left
after all of our app's components
4:00
have been destroyed.
4:04
The only reason to keep an empty
process around is for caching.
4:06
When Android needs more memory,
these processes will be the first to go.
4:10
Hopefully that clears up some of the
mystery surrounding processes and gives
4:16
you a good idea about how Android decides
which processes can be safely killed.
4:19
In the next video, we'll see how
to use two separate processes for
4:25
our music machine.
4:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up