A Word On Memory

No matter how much an application is given, it could always ask for more.

There are two big differences between an Android device, like a phone or tablet, and a traditional computer:

  • The amount of physical memory
  • The ability to do virtual memory swapping

Typically, today's computers come installed with several gigabytes of RAM (few come installed with only 1GB or less), however Android devices often have a maximum of 512MB of RAM. To add insult to injury, computers use swapping, which Android devices don't have, to give the system and applications the illusion of more memory. For example, an application could still address up to 4GB of memory even with a system that has only 256MB of RAM. Your Android application simply does not have this luxury, and therefore you have to be more careful about how much memory your application uses.

NOTE: You can find applications on Android Market that enable swapping, but they require root access and a different Linux kernel. Assume the Android devices your applications will be running on do not have swapping capabilities.

The Java language defines the size of most primitive types, as shown in Table 4-1, together with their matching native types. If you are primarily familiar with C/C++ code, then you need to pay extra attention to two things:

  • Java's char is 16-bit (UTF-16).
  • Java's long is 64–bit (while C long is usually 32-bit and C long long is usually 64–bit).

Image

A good rule of thumb is to use as little memory as possible, which is just common sense as your Android device and applications have a limited amount of memory. But in addition to reducing the risk of an OutOfMemoryError exception, using less memory can also increase performance.

Performance depends mostly on three things:

  • How the CPU can manipulate a certain data type
  • How much space is needed to store data and instructions
  • How data is laid out in memory

We will cover each one of these items, looking at both native code and Java code, measuring execution times, and comparing several implementations of simple algorithms to improve performance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.8.216