Android performance Optimization (i)

Source: Internet
Author: User

Android Performance Optimization Model

1, most users perceive the performance problems such as lag is the most important reason for rendering performance.

From the designer's point of view, they want the app to have more animation, pictures and other fashion elements to achieve a smooth user experience.

But the Android system is likely to be unable to complete those complex interface rendering operations in time.

The Android system emits a vsync signal every 16ms, triggering a rendering of the UI, if each render succeeds,

In order to achieve 60fps, it is possible to achieve 60fps of smooth picture,

This means that most operations of the program must be done within 16MS.

If one of your operations takes 24ms of time, the system will not be able to render normally when the vsync signal is obtained.

In this way, a drop-frame phenomenon occurs. Then the user will see the same frame in 32ms.


It is easy for users to perceive that the lag is not smooth when animating the UI or sliding the ListView.

Because the operation here is relatively complex, prone to drop frame phenomenon, so that the feeling of lag.

There are a number of reasons why frames can be dropped, perhaps because your layout is too complex to render within 16ms,

It is possible that there are too many drawing units stacked on your UI, and it is possible that the animations have been executed too many times. These can cause CPU or GPU overloading.

Summary: Try to avoid writing overly complex layouts, layout as simple and clear as possible, in the writing layout must consider performance issues, to avoid nested duplication redundancy.

2. Overdraw (Over-drawn) describes that a pixel on the screen is drawn several times within the same frame.

In a multi-layered UI structure, if the invisible UI is also doing the drawing operation, this causes some pixel areas to be drawn multiple times.

This wastes a lot of CPU and GPU resources.

When designing for a more ornate visual effect, it is easy to get caught up in the vicious circle of using more and more cascading components to achieve this visual effect.

This can easily lead to a lot of performance problems, in order to get the best performance, we have to minimize the occurrence of overdraw situations.


Overdraw sometimes because there are a lot of overlapping parts in your UI layout, or because you don't have to overlap the background.

For example, an activity has a background, and then the layout has its own background, and the sub-view has its own background.

Simply by removing the necessary background image, this can reduce the number of red overdraw areas and increase the percentage of the blue area.

This measure can significantly improve program performance.

Summary: To avoid the layout of duplicate drawing, if not particularly need to avoid multiple view using duplicate or different backgrounds

3. Although Android has a mechanism to manage memory automatically, improper use of memory can still cause serious performance problems.

Creating too many objects in the same frame is something that needs special attention.

The Android system has a generational Heap memory model,

The system performs different GC operations based on the different memory data types in memory.

For example, recently allocated objects are placed in the young generation area,

Objects in this area are often created quickly and destroyed quickly,

At the same time, the GC operation speed of this area is also faster than the GC operation of the old generation region.


In addition to the speed difference, any action on any thread will need to be paused when the GC operation is performed, and other actions will continue to run after the GC operation is complete.

In general, a single GC does not take up too much time, but a large number of non-stop GC operations can significantly occupy the frame interval (16ms).

If there are too many GC operations in the frame interval, then natural other similar calculations, rendering operations such as the availability of time has become less.

There are two reasons why a GC can be performed frequently:

1, memory jitter churn memory jitter, because a large number of objects are created and in a short period of time is released immediately.
2, the instantaneous generation of a large number of objects will seriously occupy young generation memory area, when the threshold, the remaining space is not enough,

The GC is also triggered. Even though each allocated object consumes little memory, stacking them together increases the pressure on the heap,

Thus triggering more other types of GC. This operation has the potential to affect the frame rate and make the user aware of performance issues.


Summary: You need to avoid allocating objects in the For loop to occupy memory, and you need to try to move the creation of objects outside the loop body.

The OnDraw method in custom view also requires attention, and each time the screen is drawn and the animation is executed,

The OnDraw method is called to avoid complex operations within the OnDraw method and to avoid creating objects.

For those who cannot avoid the need to create objects, we can consider the object pool model, through the object pool to solve the problem of frequent creation and destruction,

However, it is important to note that the objects in the object pool need to be freed manually after the end of use.

4, although Java has the mechanism of automatic recovery, but this does not mean that there is no memory leak in Java, and memory leaks can easily lead to serious performance problems.

Memory leaks mean that objects that are no longer used by the program cannot be recognized by the GC, which causes the object to remain in memory.

Takes up valuable memory space. Obviously, this also makes the memory area of each level of generation less space available, and GC is more susceptible to triggering, which can cause performance problems.

5, Power is actually one of the most valuable resources of handheld devices, most of the equipment need to continuously charge to maintain continued use.

Unfortunately, for developers, power optimization is the last thing they'll think about. But you can be sure that you can't make your application a big drain on your battery.

Purdue University studies The power consumption of some of the most popular applications,

On average, only about 30% of the power is used by the program's core methods, such as drawing pictures, placing layouts, etc.

The remaining 70% or so of the power is reported data, check location information, timed retrieval of background advertising information used out.

How to balance the power consumption of both is very important.

There are several measures that can significantly reduce the consumption of electricity:

1, we should try to reduce the number of wake-up screen and duration of time, using Wakelock to deal with the wake-up problem,

Be able to perform the wake operation correctly and close the operation to sleep according to the set time.
2, some do not have to do immediately, such as uploading songs, image processing, etc., can wait until the device is in charge or sufficient power when the time.
3, triggering the operation of the network request, each time will keep the wireless signal for a period of time, we can be fragmented network requests packaged for one operation,

Avoid excessive power consumption caused by wireless signals. About the power consumption of wireless signals caused by network requests







Android performance Optimization (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.