Table of contents
introduction:
First of all, make a simple classification and sorting out of Android performance optimization. Due to many factors affecting performance, for example, the apps, memory, network, and freezes classified in this article all affect each other. Caton should be the most intuitive and visible performance problem for users.
APP optimization focuses on the three aspects of startup, UI drawing and resource optimization
Memory optimization focuses on memory jitter, memory leaks, and memory expansion considerations
Network optimization is considered from the aspects of network request and data compression
The optimization of the above three aspects is conducive to the solution of our system freeze problem. The freeze just mentioned is intuitively perceived by the user. From the perspective of display, the freeze problem is caused by dropped frames. This article will not expand and explain the principle of display refresh for the time being.
APP optimization:
UI optimization (layout optimization, drawing optimization)
layout optimization: RelativeLayout replaces LinearLayout as the default root layout. Reduce nested layouts and improve rendering efficiency
<include> tags
<ViewStub> tags load uncommon layouts, delay loading
<merge> tags reduce layout nesting levels
Installation package optimization (res resource file optimization)
reduce volume
res resource optimization
code Optimization:
lib resource optimization:
assets resource optimization
code obfuscation Use proGuard code
obfuscation tool, including compression, optimization, obfuscation and other
functions
Cold start, hot start, time-consuming threads use sub-threads to execute
Startup logic, loading logic;
non-essential objects can be initialized lazily;
do not create global static objects, but turn to singleton mode, so that the application process only needs to be initialized for the first time.
Consider dependency injection framework
Network Optimization:
Merge network requests, reduce network requests,
avoid DNS analysis,
load a large amount of data, use paged
network data, use GZIP compression
to add network data cache, avoid frequent requests to the network
to upload pictures, and compress pictures when necessary
Memory optimization:
Avoid memory leaks: Long-lived objects refer to short-lived objects. Objects allocated on the heap are no longer used, but the GC collector cannot reclaim them.
Static variable problem: Set the internal class as a static internal class or use context.applicationContext() as a singleton
mode independently: pass parameters to context.getApplicationconText()
property animation: Activity.onDestroy calls Animation.cancel()
Handler: use static internal class +WeakReference weak reference; clear the message queue when the internal class ends its life cycle Thread
problem: Set AsyncTask and Runnable as static internal classes or separate them, use weak references inside the thread to save the Context reference
resource is not closed: even if it is closed when the Activity is destroyed Or logout:
BroadcastReceiver calls unregisterReceiver()
Cursor, Stream, file: call close
Adapter Problem:
Re-instantiate Item every time without using cache and only rely on getView(), which will create pressure on GC Use convertView WebView problem
when constructing Adapter : webView
The same thread collection problem as Activity
: there are static references in map, etc., and no deletion operation is performed
Expand memory: Add largeHeap="true" attribute to Application in the manifest file; the same application starts multiple processes to run, which can increase the total memory space of the application
Caton optimization:
Do not perform network access or large file IO operations on the main thread
Optimize layout
and optimize drawing: avoid unnecessary redrawing and overdrawing
onDraw Do not create new local objects.
Do not do time-consuming operations in the onDraw method