Some things that Android Performance Optimizer

Source: Internet
Author: User

When it comes to Android performance optimization, it always feels like a blur, because app performance is always good for the performance of the phone itself, and maybe some of the things that consume content can run on some mobile devices, but there's a memory overflow problem on top of the other, but anyway, But when the problem arises, we should have a solution, at least there is a solution to the idea, the following is a chat about Android performance problems.

The first is three names: a memory leak, a memory overflow (OOM), and an application unresponsive (ANR).

We know that running apps on Android is actually a lot of time (just a lot of times), each app is running in a process that is assigned by the device to the application, but you can also understand that each application runs on one device for our assigned virtual machine (DALIVK virtual machine) , in fact, each program can give us the memory we use is the device assigned to us. Each process can and only one thread, which is the main thread (also called the UI thread). If we do too much time-consuming operation in this main thread, we will cause the problem of the operation lag. If severe, it causes the application to become unresponsive (ANR).

A memory leak is when our program requests memory (such as instantiating objects), and after the end of use, the memory is not released in time. General memory leaks are not too problematic for a single time, but too many memory leaks can cause too much useless memory to occupy, which can cause memory overflow (OOM).

Memory overflow in two cases, the first, we do not allocate enough space for an object to use this object, such as instantiating an integer object, but put a larger object. The second is that the memory of the virtual machine is fully occupied.

So how to solve these problems, here are just a few ideas:

1. Use a simple layout when using layouts. There are two cases, the first, the strength does not nest too much useless layout, so do not do too much useless calculation in the CPU operation, the second, when using the layout, the strength of the use of simple VIEWGROUNP, Here the complexity of the sort: Relativelayout is greater than LinearLayout and framelayout.

2, in the use of layout, more use of <include>, <merge> and Viewstub. General <include> and <merge> work together so that you don't have to reuse a piece of code too much, so that the CPU does not run a layout again. Viewstub is when there is a need to load the layout, such as an interface where a layout is not displayed by default, but when we need time to display, this time if we do not use the time to load this code is not appropriate. It's good to use viewstub at this time.

3, using static variables when the strength of the final, or the use of static variables, we know that the static variable will not disappear with the program that called it, which will lead to a certain degree of memory leaks.

4, use the property animation when the activity ends, to use Animator.cancel () to stop the animation, because some animation is infinite loop, when the call his program is finished, he will always loop, this time will also cause memory leaks.

5, in the custom view or VIEWGROUNP, do not do too much time-consuming operation in OnDraw, because this will lead to the view of the drawing process is not smooth, resulting in a card feeling.

6. Do not define local variables in the OnDraw method, because the OnDraw method is called multiple times in the project of view drawing, which creates local variables more than once, creating unnecessary waste of memory.

7, the optimization of the ListView, in the custom Adpter, the use of holder, so that you can repeatedly invoke the object has been instantiated, but not to re-instantiate the object. At the same time do not do time-consuming operation in the GetView, this will make the ListView and other overall load smooth.

8, the use of low-strength enumeration, enumeration occupies more space than plastic.

9, less to create objects, because the creation of the object will be the corresponding creation of heap memory and stack memory, basically we do not call do not assign value, will also create heap memory.

10, more energy use memory cache and disk cache, such as the use of LRUCache, and so on, so that some resources can be repeated calls.

11, appropriate use of soft applications and weak applications.

12. When using threads, use the thread pool, which avoids a lot of creating threads and destroying threads.

In the event of ANR, we can use the log traces.txt generated by the system in/DATA/ANR to analyze it. Using the command: ADB pull/data/anr/traces.txt, you can open the view log.

Memory leak problem, we can use the Mat tool for analysis.

There are a number of places to note about performance optimization. After all, there is never an optimal as long as better. You are also welcome to add.

Some things that Android Performance Optimizer

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.