Book Image

Asynchronous Android

By : Steve Liles
Book Image

Asynchronous Android

By: Steve Liles

Overview of this book

With more than a million apps available from Google Play, it is more important than ever to build apps that stand out from the crowd. To be successful, apps must react quickly to user input, deliver results in a flash, and sync data in the background. The key to this is understanding the right way to implement asynchronous operations that work with the platform, instead of against it. Asynchronous Android is a practical book that guides you through the concurrency constructs provided by the Android platform, illustrating the applications, benefits, and pitfalls of each.Learn to use AsyncTask correctly to perform operations in the background, keeping user-interfaces running smoothly while avoiding treacherous memory leaks. Discover Handler, HandlerThread and Looper, the related and fundamental building blocks of asynchronous programming in Android. Escape from the constraints of the Activity lifecycle to load and cache data efficiently across your entire application with the Loader framework. Keep your data fresh with scheduled tasks, and understand how Services let your application continue to run in the background, even when the user is busy with something else.Asynchronous Android will help you to build well-behaved apps with smooth, responsive user-interfaces that delight users with speedy results and data that's always fresh, and keep the system happy and the battery charged by playing by the rules.
Table of Contents (14 chapters)
Asynchronous Android
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Introducing the Dalvik Virtual Machine


Android applications are typically programmed using the Java language, but the virtual machines in the Android stack are not instances of the Java Virtual Machine (JVM). Instead, the Java source is compiled to Java byte-code and translated into a Dalvik executable file (DEX) for execution on a Dalvik Virtual Machine (DVM).

It is no accident that Google chose Java as the primary language, allowing a vast pool of developer talent to quickly get to work on building apps, but why not simply run Android applications directly on a JVM?

Dalvik was created specifically for Android, and as such, was designed to operate in environments where memory, processor, and electrical power are limited, for example, mobile devices. Satisfying these design constraints resulted in a very different virtual machine than the typical JVM's that we know from desktop and server environments.

Dalvik goes to great lengths to improve the efficiency of the JVM, involving a range of optimizations to simplify and speed up interpretation and reduce the memory footprint of a running program. The most fundamental difference between the two VM architectures is that the JVM is a stack-based machine, whereas the DVM is register-based.

A stack-based virtual machine must transfer data from registers to the operand stack before manipulating them. In contrast, a register-based VM operates by directly using virtual registers. This increases the relative size of instructions because they must specify which registers to use, but reduces the number of instructions that must be executed to achieve the same result.

Dalvik's creators claim that the net result is in Dalvik's favour and that the DVM is on average around 30 percent more efficient than the JVM. Clearly, Google has gone to great lengths to squeeze every last drop of performance out of each mobile device to help developers build responsive applications!

Memory sharing and the Zygote

Another huge efficiency of the platform is brought about by the way in which a new DVM instance is created and managed.

A special process called the Zygote is launched when Android initially boots. The Zygote starts up a virtual machine, preloads the core libraries, and initializes various shared structures. It then waits for instructions by listening on a socket.

When a new Android application is launched, the Zygote receives a command to create a virtual machine to run the application on. It does this by forking its prewarmed VM process and creating a new child process that shares its memory with the parent, using a technique called Copy-On-Write. This has some fantastic benefits:

  • First, the virtual machine and core libraries are already loaded into the memory. Not having to read this significant chunk of data to initialize the virtual machine drastically reduces the startup overhead.

  • Second, the memory in which these core libraries and common structures reside is shared by the Zygote with all other applications, resulting in saving a lot of memory when the user is running multiple apps.