All right so we understand that it's important to create apps that are smart
about using a device's memory resources.
So now let's discuss one way to do this by implementing an efficient memory
strategy to display a long list of data which many mobile apps will need to do.
So this strategy is called View Recycling
which means reusing views that are no longer visible on the screen anymore.
And by the way, when I say view, I mean the whole layout for a single row.
The layout for a single row is probably created by a view group.
So something like a linear layout or
relative layout which can contain multiple views.
However, a view group is just a view itself.
And you may hear us refer to the layout of a single row just as a view.
Now the Miwok language is really big.
And one way to go about creating the app is we could create
tens of thousands of list items and add them to a vertical in your layout.
But if you have that many vocabulary words,
it can become really slow to scroll through the entire list.
And the device could crash because it can't hold all these list
items in memory.
Now remember, memory is a limited resource.
And the Android system and
other apps need some memory to keep running as well.
Now there's a more efficient way to do this.
Using the ListView and ArrayAdapter, we can create just enough views,
meaning list item rows, based on what we need to fill up the screen.
This means if we scroll upwards.
We no longer need to see the views at the bottom of the screen.
So let's just focus on the ListView and ArrayAdapter structure.
Now furthermore, we can recycle views.
This means if a view is no longer visible on screen.
We can reuse it by changing the data.
So changing out the data inside the view such as the text in a text view,
changing out the image in an image view.
And we don't have to waste time creating the entire view from scratch again.
Now we take these extra views and
put them in a scrap pile that can be recycled and reused.
And another term you may hear people refer to them by is scrap views.
So, we keep a scrap pile, which is a bunch of views that aren't currently
being shown on the screen right now available for reuse later.
So, recycling.
So, to get into more details of how recycling works say,
for example the user is scrolling the screen.
So they want to go to the top of the list to see the latest new tweets or
news feed stories.
Now the ListView realizes it needs to create more list item rows quickly.
So when the user scrolls it seems like the list items were there
just all along.
Now instead of creating a new list item from scratch,
it can just reuse it from the scrap pile.
If they exist.
So to summarize,
we've created more rows on demand by reusing views in the scrap pile.
And then it just becomes a matter of changing the data inside the views.
So it shows the correct information for that list item.
Now the ListView is also smart enough to know that the views that were at
the bottom of the screen have been scrolled off the screen.
And these views are no longer visible to the user, so
they can just be added to the scrap file.
And as the user scrolls even farther up the list,
we can use that list item row that used to be at the bottom of the screen.
Now at the top of the screen, so
the goal of all these is to create as few list items as possible.
Because it's expensive to inflate views from XML to Java objects.
Now, to prove to you that view recycling minimizes memory usage.
I've prepared two versions of the app.
One in the current state of the app,
that's using a vertical linear layout with one text view per list item.
And a second version of the app using the ListView and ArrayAdapter.
So the memory monitor tool is included in Android studio
to help you look at the amount of resources that your app is using.
So professional developers use this tool to help them identify
ways that they can minimize resource usage on their app.
The difference in memory usage between using a linear layout and
ListView ArrayAdopter becomes more noticeable
when the number of list items increases.
So I'll be demo these two examples so
that each one displays a list of 1,000 words.
Now here I am in Android Studio looking at the linearly layout
version of the app.
And notice the NumbersActivity is displaying a list of 1,000 words and
here's what the app will look like when we run our device.
So tapping into the NumbersActivity displays our long list of words.
Now with our device connected to the computer,
we can run the memory monitor to check the memory usage of this app.
But before I can run I need to go up here to tools > Android and
enable ADB integration.
So ADB here stands for Android debug bridge.
So the connection between our device and Android Studio.
So once that's enabled, go down here to Android monitor in the runtime window.
Click it open and I'm going to pull it up and
then click here on the memory tab.
So, this screen will allow me to view the memory usage of my app over time.
So, I'm going to go ahead and run the app on our device.
All right, so with their device connected,
we see the memory monitor running.
And we can see the amount of allocated memory used to just displayed the list
of categories.
So, I put up what the screen looks like on our device now and
let's see what happens to the emulator.
When I tap into the NumbersActivity.
[BLANK_AUDIO]
Wow, notice that jump right there.
So it went from around two megabytes to about 14 megabytes.
So is this increase, this jump in allocated memory considered good or
is it not so good?
Well the best way to check is to run a different version of the code
on the same device.
So we're going to be running it on the same device because it has
a controlled set of resources.
Now to do that I'm going to hop on over to a new version of the code that's
using the ListView and ArrayAdapter to display the same list of 1,000 words.
Now here I'm looking at the ListView and ArrayAdapter version of the code and
it's also displaying that same list of 1,000 words.
And now let's take a look at what it looks like when we run this code on
our device.
So the category screen looks the same.
But when we tap into the numbers activity
notice that the list of 1,000 words appears but with dividers.
And so we'll talk more about how and why this works later on.
Now back in Android Studio.
I'm going to launch the memory monitor the same way I did earlier.
So down to Android Monitor here, pull it up, and then go to the Memory tab.
So notice that the memory monitor ran after my device was connected because
ADB was enabled.
And the amount of allocated memory stays constant
when I'm on the category screen.
Now notice what happens when I tap into the numbers activity.
[BLANK_AUDIO]
So notice that slight jump there.
So the number went from about 12.71 to about 12.84.
So that's barely 0.1 megabyte increase in memory going from
the category screen into the numbers activity.
So this time the amount of memory used to display the list of numbers
was a lot smaller than when we use the linear layout version of the code.
And since we're using a small amount of memory, this version of the code
using the ListView and ArrayAdapter will run more smoothly on lower end devices.
Now let's take a look closer into the memory monitor here.
So it's actually showing us two things.
So the dark blue section is the allocated memory
which is the memory our device is using at any given time.
And the light blue section here is the free memory.
So, that's the memory available on the device.
While it may be tempting to use up all the free memory on your device
to make your app look a little cooler,
remember that your app isn't the only one installed on the user's device.
And it's important to leave as much resources for other apps as you can.
Now here are two screenshots from the memory monitor for
our two versions of the app.
So this left side here is the version one of the app using the linear layout.
And the right hand side here is version two of the app using ListView and
ArrayAdapter.
Remember that the allocated memory is shown in the dark blue section and
the free memory is shown in the light blue section.
Notice that in both cases the jump to an allocated memory occurs when the user
clicks on the numbers category to navigate to that list of numbers words.
So let's zoom in to the linear layout version of the app, and
we see the jump occurs from about 11 megabytes to about 14 megabytes.
So this is about three megabyte usage of memory used to display that list of
1,000 words.
Now compare this to when we use the ListView and ArrayAdapter.
And it's barely using 0.5 megabytes to display an entire list of 1,000 words.
So after seeing these two examples.
I'd like you to check out the links below to learn more
about using the memory monitor tool.
And other ways that you can improve your app's performance.