本文地址:http://blog.csdn.net/iamws/article/details/51636175
Article Two: Theory
Through the tools previously introduced, we know how to get the data to be analyzed, but only how to get the data, but not in-depth data analysis, this article is mainly about UI refresh this part of the Android theory knowledge, with this knowledge, for the above data how to analyze, You've got your answers.
PS: This article is only a personal understanding of the summary, and will not go deep into the source layer analysis, if there are errors, but also please help me correct ~
The theoretical questions to be solved in this article are as follows:
1. What is memory leak, memory overflow, what impact this block has on performance and the type of Android GC
2, the online said Android UI Refresh is 60fps/16ms what's the matter
3, the overall drawing principle of Android
4. View animation and property animation differences
5. What problems can be caused by over drawing
6. Do you use the Lockcanvas () method for the canvas when the hardware acceleration system is used?
7, which operations are CPU-intensive
8, other performance optimization points of attention (thought after adding)
If the above knowledge has been understood, then the trouble to skip this article, look forward to the next practical example (┬_┬)
Okay, here we go.
First: What are memory leaks, memory overflows, what are the effects of this block on performance, and what kind of Android GC?
(This piece needs to have some knowledge of the GC and understand this piece of Java GC.)
Because Android is a mobile device on the operating system, without the hardware support on the PC, so for the system CPU, memory resources need to pay extra attention, this is why pay attention to performance issues;
Memory leak: Simply say that the object was created before, but also allocated memory to it, but later this object is useless, it should be to the system to recycle, but the object has been a GC root on the existence of a reference chain can not be recycled, and then this part of the memory has been occupied, If this object is generated for each operation, it will gradually devour the rest of the app's memory, resulting in the memory being eaten and the overflow exception thrown.
Memory overflow: Often called oom,out of memory, generally play the picture loading is not noticed when it is very easy to encounter this anomaly, this is actually an app in any Android phone running, will be Linux to fork out the virtual machine (for example: Dalvik) process Run, And each virtual machine created by the current system will give him the available memory size, if your app caused the memory to use more than the allocated value, and the remaining memory after the GC can not give you enough space to allocate, then this exception is out
Android GC Type: We enter the GC keyword in Android Logcat, and we can see that the GC basically has the following categories:
Gc_for_malloc: GC that indicates that there is insufficient memory to trigger when allocating memory on the heap
Gc_concurrent: Represents a GC that causes a trigger after the current memory reaches a certain set amount
Gc_before_oom: Indicates that GC is performed before preparing for an OOM exception
Gc_explicit: Represents an active GC that is triggered when an active call to a System.GC, VMRUNTIME.GC interface, or a signal is received in a program
Ps:android GC related knowledge recommendation can see Lao Luo wrote blog, Links: http://blog.csdn.net/luoshengyang/article/details/41822747
You can see that in addition to the active code or signal calls above, the other GC is called during the memory allocation process, and the GC process involves CPU computing, while the GC process is either suspended or parallel with other threads in two modes, At this point, you can see why the GC suddenly appears to be degraded or the application interface is easily unresponsive, which also proves the danger of memory leaks, because memory leaks can lead to reduced memory usage, and as the object to the memory of new applications, it is easy to trigger frequent GC, So resolving memory leaks is an important way to solve performance optimization.
Second: The online said Android UI Refresh is 60fps/16ms what kind of a thing?
We can see that basically the internet has said that the Android UI is 60fps, also converted to 1000/60=16ms refresh once, this is how it came, and this is who defined it?
The answer is, of course, Google, why Google is defined, this is because the Android 2.x series development people know that the fluency of Android was lost to iOS, the fluency is more than the slot, Google was also found that the problem is very serious, So at the 2012 Google I/O Conference, officially launched the 4.1 version, but also launched the butter program, the butter program is to improve the overall smoothness, which involves the so-called single-cache, double-cache and triple-cache channel mechanism differences, and in order to ensure that each refresh of the same number of frames, Developed the vsync signal, this signal is the notification system Surfaceflinger to perform rendering the entire page.
So let's take a quick look at this caching process and some of the techniques:
What is Surfaceflinger: The official explanation is: Every window, that's created on the Android platform are backed by a surface. All of the visible surfaces rendered is composited onto the display by Surfaceflinger.
That means that every window is surface, and all the visible surface is finally made up and displayed, by Surfaceflinger, and Surfaceflinger is a system-level service, Depends on the binder communication mechanism, the client to request to build surface and finally complete the rendering communication process
So the rendering logic of Android is: VSync signal is issued by the system every 16ms, after the issue is to notify the Surfaceflinger need to render, the rendering of the data where? is the cache queue that is stored in the above mentioned (single cache, double cache, and triple cache technology channel), and how the data in the cache queue is calculated by CPU and GPU;
Simply say these caching mechanisms, single cache: Cpu/gpu The data that is drawn is in the same cache queue as the data read on the screen, but because the frequency of computation and the frequency of display are generally different, the most likely cause of this cache mode is the tearing of the picture, That is to say that the interface is easy to appear on the previous frame and the current frame in the same picture to form a picture, and this is not the smoothness we want;
Double cache: gives 2 buffer queue, one to Cpu/gpu after the data, one to the screen rendering time to read the data, while the 2 buffer between the data exchange rules, and finally solve the picture tearing problem; But the CPU and the GPU share the same buffer, And we know, CPU and GPU if in hibernation, only receive vsync signal will start processing the next data, if one time when VSync signal is issued, and just another time consuming operation, CPU or GPU is still processing the calculation of the previous frame, There will be CPU or GPU is occupying the buffer, then that does not occupy the buffer of the CPU/GPU is not able to deal with, so we have to continue to sleep a frame, so the triple cache came out
Triple cache: One more cache than the above dual cache, which means that the hardware you are each in a cache to do things, so maximize the use of each hardware computing frequency advantage, but also standardize the exchange of data between the rules, so that the Android UI fluency came up
By the way, take a look at the map in the Google IO 2012 promotional materials, visually show the above three differences and influence it:
Figure 2-1: The single-cache interface shows the cause of tearing, because the CPU/GPU frequency is high, the display frequency is low, when the interface begins to read the next data, will be on the way 0.015 of the graph read out, torn
Figure 2-1 Single-cache implementation diagram
Figure 2-3 Two-cache implementation diagram
Figure 2-4 Three-cache implementation diagram
So from the above analysis we learned that if you want your application to smooth display interface, a need to ensure that your every rendering submission of the calculation process can be completed in 16ms, two to reduce unnecessary high frame rate calculation, so that your rendering frame rate with the system refresh frequency, so that the CPU can be released to a large extent, At the same time, ensure the smoothness, that is, a method of performance optimization.
Here you can refer to the article:
Official rendering: http://source.android.com/devices/graphics/index.html
Butter Plan: http://blog.csdn.net/innost/article/details/8272867
Video Search: Google published Android 21 performance optimization Model teaching
Third: The overall drawing principle of Android
This piece is purely to look at the Android view to draw the source code, online should also be quite a lot of explanation, specific is how the system layer of WindowManager and viewroot associated with, and then how from the top Decorview to the internal view do measure, Layout,draw and so on, the main point here is that the sub-layout of the measurement process is required to rely on the parent layout of the measurement results and sub-layout properties together to determine the final sub-view measurement results
Why do you want to list this, mainly to consider if your sub-layout has been changed, will bring all the upper parent layout of all the measure process, and measure is CPU consumption, the following is my demo example, testlinearlayout for the parent layout container, Nested two layers, which are then dynamically inserted into the sub-layout container testtextview, the layout tree and the log are as follows:
After looking at the picture, you understand why the UI nesting is not too deep, the main solution is the UI layout is nested too deep, if the sub-layout changes too fast resulting in CPU loss, is an important performance optimization point.
You can refer to the book: "In-depth understanding of Android Volume II" "Android Development Art exploration" in the framework layer and the Java Layer Two pieces of detailed introduction of the entire process, there is not much to say (mainly too lazy (*^__^*), too much information)
Fourth: The difference between view animations and property animations
This piece presumably everybody generally knows these animations in the application layer the biggest difference, the view animation is the false movement, the view actually still is in the original position, the Click What event also listens in the original position, but the property animation is true movement, by changes the View Translation property value, This can also trigger events in a new location
But here to say is their performance differences, first of all, the priority to use property animation to do, property animation for performance optimization to improve the maximum
Why
TODO Insert Animation Compare figures 1, 2, 3
After a few days to make up, do a good figure disappeared--!!!
Fifth: What problems can be caused by over-drawing
What is over-drawing? We know that screen rendering is ultimately dependent on the phone hardware of each small pixel color changes to achieve, and from the above, Android is currently using a 16ms refresh mechanism, that is, you see the image of each frame between the 16ms interval, but in this interval period, Your calculations may specify a number of pixels to do several times, and when the system is finally rendered, it will also draw multiple times in a single frame of your request, resulting in some cases actually doing a lot of useless work, such as specifying a viewgroup background color is white, Then inside a sub-view, but you make this sub-view background color or white, when the system starts to render this frame will be as you mean, a pixel to draw a white, and then draw a white again, so it is not felt behind the drawing is actually superfluous and unnecessary, So over-drawn cuts are important for improved UI fluency.
So the problem caused by over-drawing is that Cpu/gpu did a lot of work, the interface rendering process also wasted a lot of drawing resources ;
The solution is also very simple, 1, reduce background settings overlap, 2, reduce unnecessary view overlap, 3, reduce the complexity of layout implementation (to and product tear forcing a little, hehe), etc.
The following randomly found 2 online app rendering display, are landing function, you can take a look: (only do technical knowledge sharing display, corresponding app development gg don't hit me)
Sixth: Do you use the Lockcanvas () method for the canvas when you want to use hardware acceleration?
At present, the mainstream mobile phone basically has hardware acceleration function, and hardware acceleration on the rendering of our CPU to help a lot, so we all want to use it, we often use Surfaceview, Surfacetextture to do some complex custom view drawing when also particularly hope to use it to help speed up, but contrary to the original official document written a sentence, resulting in our thinking is the use of hardware acceleration help, the result is not, this sentence is the official document focused attention:
If you get the canvas using the Lockcanvas () method, the canvas will not be drawn as hardware acceleration.
This optimization point is first to thank colleagues in the work, he found in the optimization process and put forward, while proving that I read the document is not too careful, as if this sentence I was ignored, ashamed ~
So this piece of people want to let the hardware speed up to help, pay special attention to the next, also counted as an optimization attention point
Seventh: Which operations are CPU-intensive
This is just a little bit of what you've always met.
1. Layout calculation
2. Animation playback
3. Image loading
4. Local IO
5. Reflection mechanism
6. Multiple internal loops
7. Threads
8. Lock
9, some of the API inside some drawing calculation
10, view of the various deformation operation
11, and so on (think of re-fill)
Does it feel like Java, ah, yes, basically the same, Java consumption of the place, Android is the same, this piece is not stressed, we pay attention to the implementation of some logic when more than consider whether there is a better implementation of the program or better API call OK ;
Eighth one: other analysis points
Thought of adding, but are concentrated in the previous article said these questions inside: memory leaks, layout hierarchy too deep, measure calculation, animation drawing problem, Io, custom control draw inside object creation, thread abuse, handler inside lost an external object does not clear, Internal classes and anonymous class issues, etc.;
The next article will begin to combine instance data code analysis, but this is to write a lot of comparison demo, after all, the company project code can not be analyzed, but the demo is based on the project performance problems of real adaptation, this piece is expected to spend more spare time, may be divided into a number of different focus on the launch, Please look forward to ha ~
PS: This article spent a night to tidy up the thoughts after writing, originally left a lot of complementary places, but to start the next stage of work, and this series of articles is needed to combine data to analyze the actual optimization performance, the focus is on the instance, so the first release this version of it, and then consider adding a supplement pull ~
author:[email protected]
转载必须注明出处:本文地址:http://blog.csdn.net/iamws/article/details/51636175
Android performance Optimization Combat theory Chapter