Collection performance optimization face questions Android

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44339238/article/details/99308899

Foreword

Prior to the interview a lot of people, may not work in Internet companies over the years or worked shorter but I do not know what the Internet company technical interview questions will be asked? Coupled with the possibility to prepare their own is not sufficient, the interview did not go a few rounds to hit a few questions by the interviewer Mongolia, and finally ended in defeat.

The following is my interview questions included finishing Android summary, due to space reasons, only part of the performance optimization topics listed, follow-up will face questions remaining update content, we can look at me and know I promptly update the knowledge while finishing this interview highlights I also spent a lot of time, a friend in need can help forward under the share point a praise ~

Performance Optimization

Stable for many reasons (out of memory crashes) and smooth (Caton) loss (power flow) installation package (APK thin) affect stability, such as memory: Android performance optimization, optimization mainly from the following aspects use unreasonable, the code abnormal scenario not considered comprehensive, code logic unreasonable, etc., will affect the stability of the application. Among the most common are two scenarios: Crash and ANR, two errors will make the program unusable. So do Crash global monitoring, treatment flash back while the crash information, the information collected by the abnormal recording it for subsequent analysis; rational use of the main thread processing business, do not do the time-consuming operations in the main thread to prevent the occurrence of ANR program is not responding.

(A) stable - Memory Optimization

(1) Memory Monitor tool:

It is the Android Studio comes with a memory monitoring tool, it can be good to help us memory real-time analysis. Android Studio by clicking on the bottom right of Memory Monitor tab to open the tool can see relatively light blue represents the free memory, while the dark part represents the memory used by conversion chart from memory transformation can be judged on the use of state of the memory, for example, when the memory continues to increase, a memory leak may occur; when the memory is suddenly reduced, the GC and the like may occur, as shown in FIG.

(2) LeakCanary tools:

LeakCanary Square is the company's open source framework based on the development of a monitoring MAT Android memory leaks. Its working principle is: the use of a monitoring mechanism of Java WeakReference and ReferenceQueue, by Activity package to WeakReference, the package had been WeakReference Activity If the object is recovered, the references will be put WeakReference ReferenceQueue by monitoring the inside of ReferenceQueue Activity can check whether the content can be recovered (instructions can be recycled, there is no leak in ReferenceQueue; otherwise, there may be a leak, LeakCanary is executed again GC, if not yet in ReferenceQueue, the leak will be identified as).

Activity was identified as if leaked, to fetch the memory dump file (Debug.dumpHprofData); followed by analysis by memory file analysis HeapAnalyzerService.runAnalysis; memory leak then be analyzed by HeapAnalyzer (checkForLeak-findLeakingReference-findLeakTrace). Finally, the display memory leaks by DisplayLeakService.

(3) Android Lint tool:

Android Lint Tool is integrated Android Sutido kind of a code hints Android tool that can give you the layout, the code provides a very powerful help. You will be prompted to hard-coded warning level, for example: write in the layout file a triple redundant LinearLayout layout, write the text to be displayed directly in the TextView, the font size used instead of sp dp units, will be in the right editor see the prompt.

(B) smooth - Caton Optimization

Caton scenario usually occurs in the most direct terms of user interaction experience. Two factors Caton, are drawing interface and data processing.

Draw interface: The main reason is to draw a deep level, page complexity, refresh unreasonable due to these causes Caton appeared on the scene more drawn after the initial interface and UI start and jump to the page.

Data processing: causes include Caton scene is too large amount of data processing, generally divided into three cases, one is processing data in the UI thread, and second data processing CPU-high, leading to the main thread can not get time slice, Third, the memory increase leading to frequent GC, causing Caton.

(1) Layout Optimization

View to be measured such systems Android, when layout and drawing, operations are performed by traversing the number of View. If the height is too high a number of View will seriously affect the measurement, layout and speed of the draw. Google is also its API documentation View recommended height of not more than 10 layers Oh. Google uses the current version of kinds RelativeLayout alternative LineraLayout as the default root layout, the purpose is to reduce the height of the tree LineraLayout nested layout generated, thereby improving the efficiency of UI rendering.

Multiplexing layout, using the tag reuse layout; speed up the display, using the delay View loading; reduced level, using the tag replacing the parent layout; Note that the use of the wrap_content, increases the computational cost measure; delete unwanted attribute control;

(2) Optimization of drawing

Overdraw refers to a pixel on the screen is drawn several times within the same time frame. In the multi-layered overlapping UI structure, if not visible UI are doing the drawing operation, it will result in some pixel area is drawn several times, thus wasting the extra CPU and GPU resources. How to avoid overdraw?

Optimize the layout. Africa must remove the background XML, remove the default Window background, placeholder display background images on demand

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

(3) Start optimization

Application of generally splash screen page SplashActivity, UI layout optimization splash screen page can be detected by Profile GPU Rendering frame is dropped.

(C) saving - power consumption optimization

In Android5.0 before testing on the application of electricity consumption that is troublesome and inaccurate, but after 5.0 Google introduced a special device to obtain power consumption information API-- Battery Historian. Battery Historian Android system is a power analysis tool provided by Google to visually demonstrate the power on your phone process, the input power by analyzing documents to show consumption.

Finally, to provide some reference power optimization methods:

(1) Calculation Optimization. Algorithm, for loop optimization, Switch ... case alternative if ... else, to avoid the floating-point operations.

Floating-point operations: the computer integer and fractional form is stored in the usual format, for example 1024,3.1415926 and so on, this no features, but the number of such accuracy is not high, express also not comprehensive enough, to be able to have a number of common representation , invented floating-point numbers. Bit floating point representation like scientific notation (. *** × 10), which is a representation of 0. The × 10, as a computer in a form. E ± ), where the asterisk in front of the decimal point, and is pure decimal integer part is 0, the index of the back part is fixed-point integer. With such a form can be any integer and decimal is shown, for example, can be represented as 1024 0.1024 × 10 ^ 4, i.e. .1024e + 004,3.1415926 can be represented as 0.31415926 × 10 ^ 1, i.e. .31415926e + 001 this is a float. Floating-point operation is carried out floating-point operations. Floating-point arithmetic is more complex than conventional operations, so the computer floating-point operations faster than conventional operations much slower.

(2) Wake Lock avoid improper use.

Wake Lock is a lock mechanism, mainly in terms of relative dormancy system ,, as long as someone holding the lock, the system can not go to sleep means that my program to the CPU plus the lock that the system will not sleep , the aim is to cooperate fully with running our program. Some cases if you do not do this there will be some problems, such as micro-letters and other instant messaging heartbeat package will stop the problem in the network access screen put out shortly after. Therefore, there is a large number of micro-channel used to Wake_Lock lock. In order to save system power, CPU busy when there is no task will automatically go to sleep. There are tasks need to wake up when the efficient execution of the CPU, the CPU will add Wake_Lock lock. We often mistake, it's easy to wake up the CPU to work, but it is easy to forget to release Wake_Lock.

(3) Using Job Scheduler manage background tasks.

In the Android 5.0 API 21, google provided JobScheduler API called the assembly to a point in time when the process or to perform a task when a specific condition is satisfied scene, for example, when the user equipment is turned on at night rest or power adapter WiFi connection to download updates to start the task. This can enhance the efficiency of the application while reducing resource consumption.

(Iv) installation packages thin --APK

Composition structure (1) installation packages

assets folder. Store some configuration files, resource files, Assets not automatically generate the corresponding ID, but is obtained through the interface AssetManager class.

res. res is the abbreviation of the resource, the resource directory for files automatically generated and mapped to corresponding ID .R file, direct access to resources ID.

META-INF. Save signature information of the application, the signature information can verify the integrity of APK files.

AndroidManifest.xml. This file is used to describe the configuration of Android applications, some of the components of the registration information, you can use permissions.

classes.dex. Dalvik bytecode program that allows Dalvik virtual machine executable, under normal circumstances, Android applications converts Java bytecode into Dalvik bytecode when packaged by the Android SDK's dx tool.

resources.arsc. Record the mapping relationship between resource files and resource ID, used to find the resources according to the resource ID.

(2) reducing the size of the installation package

Code obfuscation. Use of code obfuscation proGuard IDE own tool, which includes a compression, optimization, etc. confusion. Optimize resources. Such as using Android Lint delete redundant resources, minimizing resource files and so on. Image optimization. PNG optimization tools such as the use of the pictures do the compression process. Recommended most advanced compression tool Googlek open source library zopfli. If the application in 0 version of the above, it is recommended to use WebP image format. Avoid duplication or third-party libraries useless function. For example, Baidu Maps can access the base map, hearing fly voice without having to access offline, photo gallery Glide \ Picasso and so on. Plug-oriented development. For example functional module on a server, downloaded on demand, can reduce the size of the installation package. You can use micro-channel open-source resource file obfuscation tool --AndResGuard. Generally can compress large apk of about 1M.

Cold start and hot start

Cold start when you start the application, the system does not process the application, then the system will create a new process assigned to the application;

Hot start the process when you start the application, the application is already in the system (for example: press the back button, home button, although it will exit the application, but the application process is still retained in the background);

The difference between a cold start: the system does not process the application, you need to create a new process assigned to the application, it will create and initialize the Application class, and then create and initialize MainActivity class (including a series of measurements, layout, drawing), and finally display at the interface. Hot Start: to start from the existing process, does not create and initialize the Application class directly create and initialize MainActivity class (including a series of measurements, layout, drawing), and finally displayed on the screen.

Cold start the process Zygote process fork to create a new process; create and initialize Application class, create MainActivity; inflate layout, when onCreate / onStart / onResume methods are completed; contentView of measure / layout / draw is displayed on the screen.

Cold start optimizing reduce onCreate in the Application and an Activity () method of work; do not let the Application to participate in operating the business; not to carry out time-consuming operation Application; not as static variables are stored in the data in the Application; reduce layout complexity and depth;

Guess you like

Origin blog.csdn.net/weixin_44339238/article/details/99308899