Android Performance Tips and Tricks

Updated 5 June 2019

Save

In this blog, We will learn some android performance tips that how to manage memory, where to use the static and some good loop practices.

Avoid creating unnecessary objects

Object creation is never free. A generational garbage collector with per-thread allocation pools for temporary objects can make allocation cheaper, but allocating memory is always more expensive than not allocating memory.

As you allocate more objects in your app, you will force a periodic garbage collection, creating little “hiccups” in the user experience. The concurrent garbage collector introduced in Android 2.3 helps, but unnecessary work should always be avoided.

Thus, you should avoid creating object instances you don’t need to. Some examples of things that can help:

A somewhat more radical idea is to slice up multidimensional arrays into parallel single one-dimension arrays:

Generally speaking, avoid creating short-term temporary objects if you can. Fewer objects created mean less-frequent garbage collection, which has a direct impact on user experience.

Prefer static over virtual

If you don’t need to access an object’s fields, make your method static. Invocations will be about 15%-20% faster. It’s also good practice, because you can tell from the method signature that calling the method can’t alter the object’s state.

Use static final for constants

Consider the following declaration at the top of a class,

The compiler generates a class initializer method, called, that is executed when the class is first used. The method stores the value 42 into intVal and extracts a reference from the class file string constant table for strVal. When these values are referenced later on, they are accessed with field lookups.

We can improve matters with the “final” keyword:

The class no longer requires a <clinit> the method, because the constants go into static field initializers in the dex file. Code that refers to intVal will use the integer value 42 directly, and accesses to strVal will use a relatively inexpensive “string constant” instruction instead of a field lookup.

 

Use enhanced for loop syntax

The enhanced for loop (also sometimes known as “for-each” loop) can be used for collections that implement the Iterable interface and for arrays. With collections, an iterator is allocated to make interface calls to hasNext() and next(). With an ArrayList, a hand-written counted loop is about 3x faster (with or without JIT), but for other collections the enhanced for loop syntax will be exactly equivalent to explicit iterator usage.

There are several alternatives for iterating through an array:

zero() is slowest, because the JIT can’t yet optimize away the cost of getting the array length once for every iteration through the loop.

one() is faster. It pulls everything out into local variables, avoiding the lookups. Only the array length offers a performance benefit.

two() is fastest for devices without a JIT, and indistinguishable from one() for devices with a JIT. It uses the enhanced for loop syntax introduced in version 1.5 of the Java programming language.

I hope this blog is useful to you. Thanks for reading.

Stay Cool And Stay Updated!!!

 

Reference: https://developer.android.com/

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home