Performance optimization points

https://testerhome.com/topics/4304

First, select the appropriate base classes and controls

a, select the appropriate base class

When we inherit a base class, the base class may do a lot of things but the subclass and will not be used, but the impact on performance

Examples 1: Setting the sub-pages in order to use a certain one of the two methods inherited SettingsPreferenceFragment SettingsPreferenceFragment, while most other variables and does not use the methods, such as: inflate in onCreateView SettingsPreferenceFragment layout but in this subclass and it will not be used, and findViewById instantiated variables do not have access.

Optimization Method: Common inherited Fragment, the use of 1,2 SettingsPreferenceFragment method used to extract.

Examples 2: Setting main interface LeUIMainSettings parent is SettingsActivity, () function call setContentView in SettingsActivity the onCreate (), and in onCreate method subclass again calls setContentView () set a different view, resulting in causing invalid inflate layout

Optimization: in this case due to the subclass many places have a dependency on the parent class must inherit the parent class, but we can do: If you need to set up sub-class layout layout differs from the parent class: Add a flag in the parent class calls setContentView judged () before, to avoid invalid setting.

b, select the appropriate controls

In order to use a simple function controls the introduction of tripartite control, but the control may be a number of complex operations and will not spend.

Examples: provided setting the system page listview using PinnedHeaderListView control, the tracking code is found not taking advantage of the characteristics PinnedHeaderListView, but rather PinnedHeaderListView do a lot of decision logic and measure operation onScroll process, resulting in an initial load and rolling listview when time-consuming and dropped frames.

Optimization Methods: After determining the necessity of not using PinnedHeaderListView listView use native controls.

Select the proper use of preference, preference of usage scenarios generally: data entry is more fixed number of entries, display relatively simple, not always refresh the place, and when preference is a custom style preference, PreferenceGroupAdapter and will not be reused, resulting in Each

Will re-inflate when updating the entry.

Example: Setting several times in an asynchronous refresh application settings page is used in preference (custom preference), and the page data will exist, resulting in multiple inflate all of the entries, dropped frames.

Optimization: switch listView, prepared viewHolder, and when the data changes using NotifyDataSetChange

Second, lazy loading strategy:

In the case of minimizing the impact on the content of the show, lazy loading strategy can effectively improve the speed of the page to enter

1. Delay ViewPager cached pages: When using ViewPager, viewPager will automatically help us load the page cache, so when we show page1 page2 also immediately created.

Example: Setting the main layout of the main interface is a viewPager, the left is the system settings, application settings right is the first thing you see is a system user settings page, so we can let the application settings page load latency to accelerate the speed into the main interface.

Optimization: The handler sends the message in a delay 200ms application settings page and then refresh the data acquisition and the list, then it will speed up the startup speed, and when the user clicks on the application settings and also feel less than 200ms of delay.

2. When a page needs to show more of the following may be necessary and common user content module to show the first time, it was not slow to load content to show urgency lazy loading.

Examples: Setting of the battery sub-page, a list of the power consumption of each load App is more time-consuming, and this is part of the module than the page bottom position.

Optimization: The battery page: Overview of the battery and information about the temperature in the main thread and the like loaded synchronously displayed, the power consumption of each App then loaded into the interface asynchronously update the UI after the sub-thread computing.

Three, listView optimization

App performance optimization, optimization is a large part of the show and of the ListView load and sliding Caton optimization, optimization for ListView In addition to optimizing the use of viewHolder basis, the following also exemplified 4:00 optimization direction

1. Performance analysis tools found: listView takes longer when the inflate layout obtainview

Examples: ListView System Settings page is used to set inflate carried out in view of the implementation of its Adapter to only getView, thus taking up the main thread of a long time period.

Optimization: child thread inflate listview item view in advance and stored in the memory variables in real time to the listview getview can be used as a memory variable has been instantiated good View object.

2. getView the time-consuming operation into the sub-thread loaded

Example: Each item listView system settings page will show the appropriate settings in the icon, then the icon would be more time-consuming to load each time getView.

Optimization: child thread icon will be built in the first cache, so that when getView icon is displayed, you can directly use it, and have the cache after each getView not when they are reloaded.

3. The simplified layout of the ListView item

Examples: item listView system settings page provided in many cumbersome nested layouts

Optimization: reduce the burden layout level, reduce the work of inflate.

4.ListView in the idle state longer be updated, reducing dropped frames during the slide

Optimization: listView by onScroll listening, we can know the current state of listView rolling, and we'll make some time-consuming operation when it is judged to be in idle state in listView in getview

Example: See: Sixth, avoid frequent handler message is sent to the main thread

Fourth, pay attention to memory usage, reduce gc

1. Select the variable instantiation place, reducing the number of GC

Example: Setting an example of some members of the variables put in onResume, resulting in an increase in the number of application memory, increase the number of gc.

Optimization: The instantiation of member variables into onCreate in.

2. Avoid the list each time to reconstruct data listview

Example: In the System Settings page settings, each time with a new listView, are re-construct a new list data, and then a more appropriate logic remove out some of the entries.

Optimization: loading copy of the original data, a reference to the data in the original copy updates, then the entry is selected in the brush List data copy.

Fifth, the rational use of synchronization lock

1. For the different critical areas or critical variables to select the appropriate lock

Examples: LeuiApplicationsState class in all critical areas and places all critical variables using the same lock, resulting in a lot of situations such as lock time-consuming when called.

Optimization Method: Analysis premise for the code and code logic variables do not belong to the same region of the critical problems of concurrent access is not present in the use of different synchronization lock.

2. Avoid the main thread calls a long time waiting for the child thread lock resource appears.

Example 1: When the main thread to call certain methods in LeuiApplicationsState will lock resources for a long time to wait LeuiApplicationsState neutron threads hold appears.

Optimization: It is best to avoid competing for the same lock resource, if you must use the same lock, you can adjust the sequence of calls to reduce competition in the same situation to lock the same time.

Example 2: In the optimization process listView of (child thread advance inflate view), the child thread used getSystemService the layoutInflate for the inflate, the main thread and the like lock occurs (because: getSystemService acquired layoutInflate child thread through the same object, in the inflate () function call to start synchronized (mConstructorArgs))

Optimization: a new child thread by new PhoneLayoutInflate objects, to avoid competition with the same lock the main thread appears.

Sixth, avoid frequent handler message is sent to the main thread

Avoid frequent handler message is sent to the main thread to block the main thread looper 's message queue caused click, sliding and other events can not be obtained in time, Caton dropped frames.

Example: Setting application used in the management of the space occupied by icons and computational tools LeuiApplicationsState background thread to load each application, each load handler sends a message to the main thread to go, caused when a start rolling listView Caton dropped frames

Optimization: callback message notification, or to send messages to the child thread looper thread in the background, icons, and when a number of loading space, and determines the idle state after listview thrown main thread.

Seven other

1. carding without removing the business logic code

In the process of optimization settings found: some of the calling code and time-consuming but did not play any role, after confirming found that the part of the code is left before the function.

2.overDraw optimization: Overdraw Summary

Reduce unnecessary background levels, and reduce the nested hierarchy layout layout

3. Optimization of memory leaks : memory leak detection

Reproduced in: https: //www.jianshu.com/p/3484e2b009a2

Guess you like

Origin blog.csdn.net/weixin_34329187/article/details/91290639