Chapter 10. Monitoring System Resources

It is important to monitor resource usage on mobile devices because memory is limited. In this chapter, I show how to use the profiling tools to help you understand your application’s memory usage.

Memory Usage in Android

Android programmers don’t explicitly allocate free memory, as they do in other languages like C++. It is still possible to create a “memory leak.” This is when code keeps a reference to an object that is no longer used, which can prevent a large set of objects from being garbage-collected. This can be a result of improper scoping of variables, not closing handles to system resources after using them, or long-running processes that may not expire.

The Dalvik runtime is garbage-collected, which means that unused memory is automatically recovered by the system at certain intervals. This might lead you to think that you can ignore memory usage entirely because the system will eventually take care of it. This is not true, as memory issues can manifest in many different ways. Some may be obvious, such as getting an OutOfMemory exception due to not recycling your bitmaps correctly.

There are other issues related to memory usage that are far more difficult to debug and that can impact performance more significantly. These are issues related to inefficient garbage collection as a result of frequent or large collections that manifest themselves in ways that aren’t as obvious. Instead of your app force closing with an OutOfMemory exception, ...

Get Android Developer Tools Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.