-
Title:
Memory Leaks
-
Description:
-
One of the best things about Android's
Java language is that it's a managed
-
memory environment that is, you don't
have to be super careful about handling
-
when objects are created or destroyed.
-
While this is generally great,
-
there's some hidden performance problems
lurking under the surface here.
-
Now remember,
the memory heaps in Android's runtimes
-
are segmented into spaces,
based on the type of allocation and
-
how best the system can organize
allocations for future GC events.
-
And each space has its
own reserved memory size.
-
When the combined size of an object in
a space begins to approach its upper
-
limit, a garbage collection event
is kicked off to free up space and
-
remove unneeded objects.
-
These GC events aren't generally a
noticeable problem to your performance.
-
However, a lot of them recurring
over and over and over and
-
over again can quickly
eat up your frame time.
-
The more time you're spending doing GCs,
-
the less time you have to do other stuff
like rendering or streaming audio.
-
One common situation that developers
can fall into that cause a lot of
-
these GCs to occur is
known as memory leaks.
-
Memory leaks are objects which
the application is no longer using, but
-
the garbage collector fails
to recognize them as unused.
-
The result is that they
stay resident in your heap,
-
taking up valuable space that's
never freed up for other objects.
-
As you continue to leak memory,
-
the available space in your heap's
generation continues to get smaller and
-
smaller and smaller, which means that
more GCs will be executed more often
-
to try to free up space for
normal program execution.
-
Finding and
fixing leaks is tricky business.
-
Some leaks are really easy to create,
-
like making circular references to
objects which the program isn't using.
-
While other are not so simple,
-
like holding on handles to class loader
objects as they're being loaded.
-
In either case, a smooth running,
fast application needs to be aware and
-
sensitive to memory
leaks that may exist.
-
I mean, your code's going to be
running on a federation of devices and
-
different types, and
-
not all of them are going to have
the same memory footprints and sizes.
-
Thankfully, there's a simple tool that's
available to help us see where these
-
leaks might exist
inside the Android SDK.
-
Let's take a look.