Android system APP performance optimization in the development of applications should pay attention to what points

Speaking of the Android system phone, most people's impression is that they have become a bit stuck for a while. Some programs crashed inexplicably during the operation. When they opened the system folder, they found a lot of files and then used the phone. Butler app constantly cleans and optimizes before it feels a slight increase in operating speed. Even if the mobile phone scores far ahead of scores in various performance running software, it still feels no matter how much memory space is far from enough. I believe that every user who uses an Android system has had similar experience. Indeed, the Android system is not as fluent as the IOS system. Why, obviously, when looking at the hardware configuration of the mobile phone, Android devices will not lose out on IOS devices, even Stronger than it, the key is software.

The reasons for this phenomenon are many, simple list of points as follows:

In fact, in recent years, as the Android version continues to iterate, the Android system provided by Google has become more and more fluid, and the latest released version is Android 8.0 Oreo. However, Android phones used by most users in China are customized versions by major manufacturers. They are often not the latest native system kernels. Most of them may still remain on Android 5.0 systems, and even Android 6.0 or more are still small. , updates have delays.

Since the source code of the Android system is open, everyone can modify the source code as long as they comply with the corresponding agreement. Then all domestic manufacturers convert their Android-based source code into their own externally released systems, such as the familiar Mi Mi system and Huawei. Mobile phone EMUI system, Oppo mobile phone ColorOS system, etc. Because each manufacturer has modified the Android native system source code, there will be a problem in this area. That is the famous Android fragmentation problem. The essence is that different Android systems have different application compatibility and cannot achieve consistency.

Due to various Android fragmentation and compatibility issues, Android developers need to adapt to different systems when developing applications. At the same time, the development level of each Android developer is uneven, and the written application performance also exists. Different types of problems lead to different user experience in the use of the user, so some problems will be converted to Android system problems, and then affect the evaluation of Android phones.

Performance optimization

The point that we want to talk about today is the optimization of Android APP performance. That is, what should we pay attention to when developing an application? How can we better improve the user experience? A good application, in addition to having attractive features and interactions, should also have high requirements on performance. Instant applications are very distinctive. In the early stages of the product, some users may be attracted, but if the user experience is not good, Will give the product a bad reputation.

How should a good application be defined? There are three main areas:

Business functions

Logical interaction

Excellent performance

As we all know, the Android system as a mobile device-based operating system, hardware configuration is a certain limit, although the configuration is now more and more advanced, but still can not be compared with the PC, the use of the CPU and memory is unreasonable or resource-consuming For a long time, you will encounter stability problems caused by insufficient memory and stalled problems caused by too much CPU consumption.

When faced with the problem, everyone thinks about contacting the user and checking the log. However, they do not know the feedback on the performance issues. The reason is very hard to find. The logs are mostly useless. Why? Because performance issues are mostly non-essential issues, it is difficult to reposition the problem, and there is no critical log, of course, the reason cannot be found. These problems have a great impact on user experience and function usage, so it is important to understand some of the performance optimization solutions and optimize our applications in actual projects to improve the user experience.

Four aspects

The performance issues of the user experience can be summarized in four categories:

Fluency

stable

Power saving, provincial traffic

Small installation package

What are the main reasons for the performance problems? The reasons are the same and different, but in the final analysis, they are nothing more than memory usage, code efficiency, appropriate policy logic, code quality, and the size of the installation package. :

Android system APP performance optimization in the development of applications should pay attention to what points

As can be seen from the figure, creating a high-quality application should be targeted in four directions: fast, steady, provincial, and small.

Fast: Avoids the use of Caton when it is used, has a fast response, reduces the user's waiting time, and meets user expectations.

Stability: Reduce the crash rate and ANR rate, and do not crash and become unresponsive during user use.

Province: saves flow and power consumption, reduces user cost, and avoids hot phones.

Small: Small installation packages can reduce users' installation costs.

To achieve these four goals, the specific implementation is in the right-hand side of the problem: Caton, unreasonable memory usage, poor code quality, code mess, and excessive installation packages. These problems are also the most encountered in the development process. Problems, while realizing business needs, also need to take this into account, spend more time thinking about, how to avoid optimization after the completion of the function, otherwise the cost of maintenance after the realization of such functions will increase.

One, Caton optimization

Android applications are slow to start up, and they often get stuck during use. This affects the user experience and should be avoided as much as possible. There are many scenes of Caton, which can be divided into four categories according to the scene: UI drawing, application start, page jump, event response, as shown in the figure:

Android system APP performance optimization in the development of applications should pay attention to what points

The root causes of these four kinds of tricks can be divided into two categories:

• Interface drawing. The main reason is that the level of drawing is deep, the page is complex, and the refresh is unreasonable. Due to these reasons, the scenes of Caton appear more in the UI and the initial interface after startup and jump to the page drawing.

• data processing. The reason for this kind of stalling is that the amount of data processing is too large and it is generally divided into three situations:

The data is processing the UI thread,

Data processing occupies a high CPU, causing the main thread to miss the time slice.

Increased memory leads to frequent GCs, which cause stalls.

There are many reasons for Caton, but in any case, the cause and the scene are ultimately displayed on the screen of the device to reach the user. In the final analysis, there is a problem with the display. Therefore, to solve Caton, you must first understand the display of the Android system. principle.

Android system display principle

Android display process can be simply summarized as: Android application to measure, layout, after painting the surface of the cached data, through the SurfaceFlinger to render the data to the display screen, refresh the data through Android's refresh mechanism. In other words, the application layer is responsible for drawing, the system layer is responsible for rendering, and the data that the application layer needs to draw is passed to the system layer service through the inter-process communication. The system layer service updates the data to the screen through the refresh mechanism.

We all know that there are three core steps in every View drawing for Android: Measure, Layout, and Draw. The concrete implementation is performed from the performTraversals() method of the ViewRootImp class. Both Measure and Layout recursively obtain the size and position of the View, and take the depth as the priority. It can be seen that the deeper the level, the more elements, and the time-consuming. The longer it will be.

The actual rendering of the data to be displayed on the screen is achieved through the SurfaceFlinger service in the system level process. What does the SurfaceFlinger service mainly do? as follows:

In response to a client event, create a Layer to establish a connection with the client's Surface.

Receive client data and attributes, modify Layer properties such as size, color, transparency, etc.

Refresh the created Layer content to the screen.

Maintain the sequence of Layers and make clipping calculations for the final output of Layer.

Since it is two different processes, then it is definitely a cross-process communication mechanism to implement data transfer. In the Android display system, Android's anonymous shared memory is used: SharedClient. A SharedClient is created between each application and the SurfaceFlinger. , and then in each SharedClient, up to 31 SharedBufferStacks can be created. Each Surface corresponds to a SharedBufferStack, which is a Window.

One SharedClient corresponds to one Android application, while one Android application may contain multiple windows, Surface. That is to say SharedClient contains a collection of SharedBufferStack, which uses double-buffering and triple-buffering techniques in the display refresh mechanism. Finally summarized to show the overall process is divided into three modules: the application layer is drawn to the buffer, SurfaceFlinger renders the buffer data to the screen, because it is a different process, so use Android's anonymous shared memory SharedClient cache to display the data to achieve the purpose .

In addition, we also need a noun: FPS. FPS indicates the number of frames delivered per second. Ideally, 60 FPS will not feel the card, which means that each drawing time should be within 16 ms. However, Android systems may not be able to complete complex page rendering operations in time. The Android system sends a VSYNC signal every 16ms to trigger the rendering of the UI. If the rendering is successful each time, it can achieve the 60FPS required for smooth images. If an operation takes 24ms, the system cannot perform normal rendering when receiving the VSYNC signal. This causes frame dropping. Then the user will see the same frame within 32ms. This phenomenon is more common when performing animations or sliding lists. It may also be that your layout is too complex, stacking too many rendering units, and you cannot complete the rendering in 16ms. Eventually causing refresh is not timely.

Catton Root Cause

According to the principle of Android system display, you can see that the root cause of the impact of the drawing are the following two aspects:

Drawing tasks are too heavy, drawing a frame takes too long.

The main thread is too busy. The data is not ready to drop frames when the VSYNC signal passed from the system.

Drawing takes too long and there are some tools that can help us locate the problem. If the main thread is too busy, you need to pay attention. The main responsibility of the main thread is to handle user interactions, draw pixels on the screen, and load and display related data. Therefore, you need to avoid any main thread, so that the application can maintain user operations. The immediate response.

To sum up, the main thread mainly does the following work:

UI life cycle control

System event processing

Message processing

Interface layout

Interface drawing

Interface refresh

In addition, it should try to avoid placing other processing in the main thread, especially complex data calculations and network requests.

Performance Analysis Tool

Performance problems are not easy to reproduce, nor are they well-positioned. However, if you really run into problems, you still need to solve them. Then analyze the problem and confirm whether the problem is solved, you need to use the appropriate debugging tools, such as viewing the Hierarchy View of the Layout hierarchy. , The GPU profile tool and the static code checking tool Lint on the Android system, these tools play a very important role in the performance optimization, so be familiar with, know what tools are used to analyze the scene.

1. Profile GPU Rendering

In mobile phone developer mode, there is a Caton detection tool called Profile GPU Rendering, as shown in the figure:

Android system APP performance optimization in the development of applications should pay attention to what points

Its features are as follows:

A graphical monitoring tool that can reflect the current drawing time in real time

The horizontal axis represents time, and the vertical axis represents the time required for each frame.

Over time, refreshed rendering from left to right

Provide a standard time-consuming, if it is higher than the standard time, it means that the current frame is lost

2. TraceView

TraceView is a tool that comes with the Android SDK. It is used to analyze the function call process and analyze the performance of the Android application and the code of the Framework layer. It is a graphical tool. It will eventually produce a chart for explaining the performance analysis. It can analyze the execution time of each method. It can count the number of times the method is called, the number of recursions, the actual duration, and other parameters. The use of very intuitive, very convenient analysis of performance.

3. Systrace UI performance analysis

Systrace is a performance data sampling and analysis tool provided by Android 4.1 and above. It returns some information from the perspective of the system. It can help developers collect key information subsystems such as surfaceflinger, WindowManagerService, and other key components of the Framework, services, and View systems, to help developers more intuitively analyze system bottlenecks and improve performance. Systrace's features include tracking system I/O operations, kernel work queues, CPU load, and so on, providing good data on UI display performance analysis, especially on animations that do not play smoothly, render cards, and more.

Optimization proposal

1. Layout optimization

Whether or not the layout is reasonable affects the amount of page measurement time. We know that the display measurement and drawing process of a page is accomplished by recursion. The time of multi-tree traversal is related to the height h of the tree, and its time complexity is O ( h) If the hierarchy is too deep, adding more layers will add more page display time, so the rationality of the layout is very important.

What are the methods of layout optimization, mainly by reducing the level, reducing the measurement and drawing time, and improving the reusability of three aspects.

Summarized as follows:

Reduce hierarchy. Reasonably use RelativeLayout and LinerLayout to make reasonable use of Merge.

Increase display speed. With ViewStub, it is an invisible view object that takes up no layout and consumes very little resources.

Layout reuse. You can improve reuse by tags.

Use wrap_content as little as possible. Wrap_content increases the cost of calculating the layout measure. When the width and height are known to be fixed, wrap_content is not used.

Delete useless properties in the control.

2. Avoid Overdraw

Over drawing means that a pixel on the screen is drawn multiple times within the same frame time. In a multi-level overlapping UI structure, if the invisible UI is also doing drawing operations, it will cause some pixel areas to be drawn multiple times, thus wasting extra CPU and GPU resources.

How to avoid over drawing is as follows:

Layout optimization. Remove unnecessary background in XML, remove Window's default background, display on-demand background image

Custom View optimization. Use canvas.clipRect() to help the system identify those areas that are visible, only to be drawn in this area.

3. Start optimization

Through the monitoring of the startup speed, the problems affecting the startup speed are found, the startup logic is optimized, and the application startup speed is improved. The startup mainly completes three things: UI layout, drawing, and data preparation.

Therefore, the startup speed optimization is to optimize these three processes:

UI layout. The application generally has a splash screen to optimize the UI layout of the splash screen, and can detect dropped frames through the Profile GPU Rendering.

Start load logic optimization. You can use distributed loading, asynchronous loading, deferred loading strategies to increase application startup speed.

data preparation. Data initialization analysis, loading data can consider strategies such as thread initialization.

4. Reasonable refresh mechanism

In the application development process, due to the change of data, the page needs to be refreshed to display new data, but frequent refreshes increase the resource overhead and may cause churn to occur. Therefore, a reasonable refresh mechanism is needed to improve the overall UI fluency. .

Reasonable refresh need to pay attention to the following points:

Minimize the number of refreshes.

Try to avoid having high CPU threads running in the background.

Reduce the refresh area.

5. Others

When implementing animation effects, you need to select an appropriate animation frame according to different scenes to achieve. In some cases, you can use hardware acceleration to provide fluency.

CAT5E Patch Panel

We are a professional manufacturer in the cabling solutions supplies in Ningbo, we could offer the Patch Panel in 8-48 ports, cat5e, cat6 cat6a specification; Metal or plastic Cable Management with brush; the Keystone Jack in UTP and STP style; surface wall mount box in blank, cat5e cat6 or other mount box; the RJ45 Modular Plug in 8P8C, cat5e, cat6, cat7 basing on UTP and STP Style; 86 type, UK type, France, type, German type, USA type Face Plate in 1 port to 8 ports; Stripper and Crimping tool and tool kits, cable tester for RJ11 RJ12 RJ45, HDMI,USB connector; cabling solution accessories like as cable tie, Cabinet screw, LSA module frame, indoor and outdoor distribution box, fiber optical distribution box and patch panel.

We have more 6 staffs in QC team, and 4 staffs in technical division to keep the high quality of products and service to our customers.

CAT5E Patch Panel,STP Patch Panel,24 Ports Patch Panel,keystone patch panel

NINGBO UONICORE ELECTRONICS CO., LTD , https://www.uonicore.com