Four directions are set:
-Response times (Response time)
-Interface Lag (ANR)
-Memory consumption
-Memory leaks (out of memories)
Response time
This refers to the client interaction with the server, to get the data, parsing, and then display to the interface the entire process of time spent.
This section deals with client-side optimizations, as well as server optimizations, where only the client is discussed.
HTTP request mode
Our app is generally inseparable from the network, the request interface is the most common operation, how to request, what we will be in the initial development of the set, the service to me the interface provided, can be roughly through get, POST, HEAD, PUT, delete these kinds of request way, Different request methods have different scenarios, such as a GET request, which should be used to request a return result, which is part of the URL, a POST request that changes the service-side data or state, and a head request is just like get, except that the server cannot return the message body in the response; Put request, Used to place the page correctly; Delete requests are used to remove the server-specified document.
The use of excellent open source HTTP framework is our better choice, its advantage is the market verification, a lot of pits have been filled, the disadvantage is that we need to delve into it to expand it, encountered pits may not be able to fill.
If you build your own wheels, we also need to take the time to verify and adapt to our business needs, but the advantage is that we can expand the controls on our own, but this is very much about the quality of the developers.
Data parsing
In actual development, the return data format of the service side is nothing more than two kinds:
-JSON
-XML
These two format data format each has the merits and demerits, from the readability point of view, the XML is slightly better, but the JSON also has the canonical label, from the analytic difficulty and the speed view, everybody prefers to use the JSON, at present the JSON is also the mainstream data format.
In Android, we can use excellent analytic library to speed up our parsing speed, there are Dom4j,json in XML, Jackson, Gson, we through these libraries to achieve faster data analysis, improve our development efficiency.
Data storage
The previous section is about data parsing, and the data that we've parsed may need to store the data somewhere, five ways to store Android:
-Content Provider (used primarily to share data with other applications)
-SQLite (store data in the database)
-File (save local files)
-Sharedpreference (mainly used to save simple configuration information)
-Networked storage (WebService data returned or parsing HTTP protocol for network data interaction)
In order to improve the response time of the application, the data cache is a better way, we can preprocess the data returned by the server, and cache the data for refresh.
Optimization points:
-Asynchronous request for network data
-Preprocessing server return data
-Asynchronous data store operations
-Data Cache Refresh
-Timeout retry
-Manipulate the UI in the main thread
Interface Lag
ANR means "application is unresponsive" and this is what we need to avoid, the reason for this exception:
-The main thread ("Event processing Thread"/"UI thread") did not respond to input events within 5 seconds
-Broadcastreceiver not executed within 10 seconds
There are a number of reasons for ANR, and the general scenario is that the UI thread takes time-consuming actions, such as "network requests" and database operations.
So how to avoid it?
-The UI thread only refreshes the interface, does not take any time-consuming action, and the time-consuming operation is placed on the child thread to do
-Thread+handle or asynctask can be used for logical processing
Memory consumption
The memory of each cell phone is limited, we refer to this memory refers to the cell phone's RAM, it is the abbreviation of Ramdom Access memory, our application needs random read and write data is in RAM, the reason why Android phone consumes memory, This is related to the processing of the Android backend, we know that the Android application is developed using Java, running Java need to have a virtual machine, it is necessary to open an application will create a virtual machine, which is required memory, so we open more applications, the more background process, memory is allocated, is causing serious memory consumption.
In fact, this problem we are not broken, as long as the memory is not enough, our application will be card. The application we develop relies on the heap memory allocated to us by the system, the general upper limit in 16m~48m, but we can request more heap memory by setting the Application property largeheap= "true" in Androidmanifest.
Get the available heap memory limit by using the following code:
Mactivitymanager = (Activitymanager) this.getsystemservice (Context.activity_service);
Mmaxmemory = Mactivitymanager.getmemoryclass (); 1212
Memory leaks
Memory leaks This problem has been said to be rotten, we all know that there is a memory leak this problem exists, but why the memory leaks occur?
The memory leak here is not the true meaning of the leak, but because the memory is not able to perform GC operations, resulting in excessive memory consumption, throw out of a memory anomaly, and killed by the system.
JVM recovery mechanism
It's time to talk about the recovery mechanism of the JVM, see:
The JVM manages three generations of Java objects, namely, the younger generation, the old generation, and the permanent generation.
Young Generation: The vast majority of Java objects are allocated in younger generations and are recycled in younger generations.
Old Generation: In the young generation, long-term existence of non-recycled Java objects is transferred to older generations, and this heap space is usually larger than the heap space of the younger generation.
Permanent generation: Stores metadata for VMS and Java classes, as well as static variables for interned strings and classes.
This involves the knowledge of the JVM, which is not further explored here.
But we should be able to understand the role of the garbage collector:
-Allocating memory
-Ensure that all objects being referenced still exist in memory
-Reclaim memory for objects that are no longer referenced by the execution code
Object reference
The reference types of Java can be divided into the following categories:
-strong reference (strong Ref): Strong, remove the strong can be, will be recycled.
-Soft Reference (Soft Ref): Memory is sufficient, it is kept, memory is tight, then recycled, mainly used for caching.
-Weak references (Weak ref): Weaker than soft ref, and can be recycled even if memory is not tight.
-Virtual Reference (Phantom Ref): Does not persist any objects in memory.
I am responsible for programming, so that the program can run normally.
Mobile App Project optimization (RPM)