Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android High Performance Programming

You're reading from  Android High Performance Programming

Product type Book
Published in Aug 2016
Publisher Packt
ISBN-13 9781785288951
Pages 412 pages
Edition 1st Edition
Languages
Authors (3):
Emil Atanasov Emil Atanasov
Profile icon Emil Atanasov
Enrique López Mañas Enrique López Mañas
Profile icon Enrique López Mañas
Diego Grancini Diego Grancini
Profile icon Diego Grancini
View More author details

Table of Contents (17) Chapters

Android High Performance Programming
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
1. Introduction: Why High Performance? 2. Efficient Debugging 3. Building Layouts 4. Memory 5. Multithreading 6. Networking 7. Security 8. Optimizing Battery Consumption 9. Native Coding in Android 10. Performance Tips Index

Memory management


Memory is always, by definition, a scarce resource on any software platform. But when it comes to mobile devices, this is an even more constrained resource. Mobile devices often have less physical memory and processor capacity that their bigger peers, and having an efficient memory management is crucial to improving user experience and software stability.

Dalvik Virtual Machine routinely triggers garbage collection in a similar way to Java, but this does not mean that we can ignore memory management completely. One very common error in junior programmers is to create memory leaks. A memory leak happens when an object is stored in memory, but it cannot be accessed anymore by the running code. The size can vary a lot (from an integer to a big bitmap or structure of several megabytes), but in general they affect software smoothness and integrity. We can use automated tools and frameworks to detect memory leaks and also apply some programming techniques to avoid allocating objects unnecessarily (and equally important, to deallocate them when they are no longer needed).

An Android application has a maximal amount of RAM memory that it can manage. It is different for each device (yes, another problem of the system fragmentation), and can be particularly checked by calling the function getMemoryClass() on the ActivityManager. Early devices had a per-app cap of 16 MB. Later devices increased that to 24 MB or 32 MB, and it will not be surprising to see devices up to 48 or 64 MB. There are several factors contributing to this fact, such as screen size. Larger screens generally mean larger resolutions for bitmaps; thus, as they increase, memory requirements will also grow. Some techniques can also bypass this limitation, such as using the NDK or requesting from the system a larger heap. This last is, however, considered to be poor form for an Android app.

When a process starts, it is forked from an existing or root process called Zygote. Zygote starts every time the system boots and loads the resources common to all the apps. By doing this, Android tries to share all the common resources among the applications, avoiding duplicating memory usage for the same frameworks.

You have been reading a chapter from
Android High Performance Programming
Published in: Aug 2016 Publisher: Packt ISBN-13: 9781785288951
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}