How well do you master APP performance optimization? Just chat?

Product performance is something that every technical team is more concerned about. Whether it is before the product is launched or after it is launched, the performance of the product needs to be monitored and optimized. If there is a problem with the product during operation, it will greatly affect the user experience. .

Therefore, in some large factory technical teams, the ability to optimize personal performance is very important, and even the recruitment requirements are clearly stated:

And some Android developers read in their resumes that they will be: familiar with performance optimization, proficient in performance optimization, etc., but in fact they will simply use third-party tools to operate. If the optimization requirements involve the bottom layer, people will be confused, because there are many people doing business-level development in the field of Android development, and few people have a deep understanding of the underlying logic of the Framework. In fact, I can also understand, after all, I am also a person who has been there.

The following is a selection of some interview questions related to Android performance optimization, which are often encountered in interviews with major manufacturers. You can read all the questions first. If you were an interviewer, how would you answer them?

1. What performance analysis tools have you used? What are their advantages and disadvantages?

Answer: Common Android performance analysis tools include TraceView, Systrace, Profiling, Hierarchy Viewer and MAT.

  • TraceView can display the method and call time of each thread in the application, helping developers find performance bottlenecks. However, markers need to be added manually.
  • Systrace is a system-level capability analysis tool provided by Android Studio, which can truly detect the working status of the system at the API level, thereby helping us locate program performance problems.
  • Profiling tracks an application's resource usage, including CPU usage, function calls, and thread activity. But you also need to add markers manually.
  • Hierarchy Viewer shows the hierarchy of Android UI components and can check whether the layout is effectively handling its hierarchy. It can also use rendering and image analysis tools to check the view's performance.
  • MAT (Memory Analyzer Tool) can help confirm application memory usage and detect memory leaks. MAT is powerful, but not easy to use.

2. How to avoid memory leaks? Please provide a few examples.

Answer: Common methods to avoid memory leaks are:

  • Stop references to objects or variables that are no longer used.
  • Avoid overusing the singleton pattern.
  • Make sure to cancel all callbacks of the object when needed.
  • Release unused resources such as database connections and file handles.
  • Use weak and soft references to keep track of objects and allow the garbage collector to reclaim them if there are no other references to them.

Example 1 : Ensure object references are released from memory when the Activity is destroyed. For example: cancel the unclosed database connection, unload the unreleased Bitmap, etc.
Example 2 : When the Activity is closed using the finish() method, unregister all broadcast listeners and GoogleApiClient.
Example 3 : Avoid keeping references to Context objects in static methods or global variables (can be initialized using Application Context or onCreate() method).

3. How do you optimize your app's startup time?

A: Common ways to optimize app startup time are:

  • Reduce redundant custom View layouts, lighten the burden on the layout hierarchy tree, and use relative layout and linear layout to minimize nesting levels.
  • Avoid using too many custom Views, use pictures or gradient backgrounds instead of obviously different Views, etc.
  • Minimize APK size as much as possible, remove unused code or resources.
  • Use a lightweight user data store, such as SharedPreferences, and avoid large databases.
  • For network services, use threads instead of processes whenever possible.
  • An Activity or Service that is delayed.
  • For activities that start slowly, try using loading animations or placeholders.

4. What methods would you use to optimize the layout rendering performance of your app?

Answer: To optimize the layout performance of your application, you can take the following actions:

  • Avoid laying out too many controls on the screen. Combining it into one control can greatly improve performance.
  • Reduce unnecessary ViewGroup and excessively nested hierarchical relationships.
  • Use ViewStub to delay the loading of layouts that are not needed until they are needed.
  • Avoid using too many includes in layout files. Failure to do so may result in poor screen rendering performance for applications with repeated layouts.
  • In ListView and RecyclerView, reuse rows as much as possible, and use sliding lazy loading to load the interface as much as possible.
  • Using relative layouts can ease nesting hierarchies and keep layouts scalable.

5. How do you locate the bottleneck of the application and optimize it accordingly?

Answer: To locate the bottleneck of the application and optimize accordingly, the following steps need to be followed:

  • Use performance analysis tools, such as TraceView and Systrace, to monitor application behavior and detect bottlenecks such as computationally intensive and network calls.
  • Do an analysis of bottlenecks and try to fix things like using asynchronous tasks, using caches, delaying reading from the network, shrinking images or text, etc.
  • Repeat this process and optimize, gradually reducing the bottleneck until the performance of the application reaches an acceptable level.

6. Talk about your understanding of multithreaded programming, and how to use multithreading to improve performance in Android?

Answer: Multithreaded programming is a technique that takes full advantage of the multi-core processors of modern computers and allows programs to perform tasks in the background while the user interface is processed on the front end.

In Android, we can use tools such as AsyncTask, Handler and thread pool (ThreadPoolExecutor and ScheduledThreadPoolExecutor) to handle multi-threaded programming.

Using multi-threaded programming, you can improve the responsiveness of your application and reduce UI thread blocking to properly use threads to optimize your application's performance. But when using multithreaded programming, care needs to be taken to avoid deadlocks and thread safety issues.

7. Talk about your understanding of the memory management mechanism, and how to manage memory in Android?

Answer: Android's memory management mechanism uses the garbage collection mechanism of the Java virtual machine. Applications can allocate large amounts of memory and release it to the system, and the system will promptly clean up unused memory.

In Android, applications can use various storage technologies to store data, such as SharedPreferences, SQLiteDatabase, Content Providers, etc. At the same time, you can use the Android memory analysis tool to check for memory leaks, and use the weak and soft references of the Java virtual machine to limit memory allocation.

8. What do you consider when writing code that can improve the performance of the application?

Answer: When writing code, we can take the following actions to improve the performance of the application :

  • Don't optimize the code too much, you should let the code run first, and then optimize it.
  • Shrink APK size, remove unused code and resources.
  • Use caching and lightweight data storage techniques such as SharedPreferences.
  • Use asynchronous tasks or threads to perform time-consuming tasks to avoid blocking the UI thread.
  • Avoid excessive nesting of custom Views, ViewGroups, or layouts.
  • Get some challenging performance tests.

9. How to judge whether the performance of the application meets expectations, and provide an effective optimization solution?

Answer: In order to evaluate the performance of the application, the following key indicators can be used :

  • Responsiveness: Responsiveness has always been the most important performance metric for client-side applications, web pages, and other products that provide user experience.
  • Resource usage: including CPU, memory, battery, and network bandwidth. Applications need to minimize their use of these resources so that they are available to other applications while avoiding user complaints.
  • Stability: The application needs to run stably without crashing or generating exceptions.

If you find that the performance of your application is not as expected, you can try some optimization strategies :

  • Reduce unnecessary computation or I/O operations.
  • Store large amounts of data locally.
  • Minify downloaded resource files or data, and optimize API calls to improve application performance on slow networks or poor communication quality.

10. Do you know about pre-compilation technology (AOT) and JIT (just-in-time compilation) technology, and under what circumstances can using them improve the performance of the application?

A: Ahead of time compilation technology (AOT) and just-in-time compilation technology (JIT) are compilation methods to improve application performance.

In AOT, the application is fully compiled to machine code before being run. All code is compiled once, which may require some additional time overhead, but can greatly improve the performance of the application.

In JIT, an application is partially compiled into machine code at runtime. JIT compiles methods in an application incrementally, which can speed up performance improvements but may make the application more prone to crashes or memory issues.

In general, the compilation time of AOT will be longer than that of JIT, but the performance will be better. For applications that require higher performance, AOT compilation may be considered. For applications that require more flexibility, JIT compilation can be used.

at last

In fact, it can be seen from the above questions that performance optimization involves a relatively wide range of knowledge, so if you want to fully learn all the knowledge points in performance optimization, it will not happen overnight. We need to understand the underlying principles of Framework and some performance Optimizing the use of the monitoring framework is conducive to performance optimization.

Although some people will say: building rockets for interviews and screwing screws for work , there are not many people who actually apply them to work.

It’s true to say this, but if you happen to encounter something you don’t know at work, and the top is pressing you to hand in the project by a certain time, it is definitely too late to learn at this time. Some people will say that it is enough to find a related solution classmate on the Internet, but have you ever thought about what problems will arise in the future? So only you will be the best.

In order to help you better understand performance optimization in a comprehensive and clear way, we have prepared relevant learning routes and core notes (returning the underlying logic): https://qr18.cn/FVlo89You can learn for reference:

Performance optimization core notes:https://qr18.cn/FVlo89

  • start optimization
  • memory optimization
  • UI optimization
  • Network Optimization
  • Bitmap optimization and image compression optimization
  • Multi-thread concurrency optimization and data transmission efficiency optimization
  • Volume Pack Optimization
    insert image description here

"Android Performance Monitoring Framework":https://qr18.cn/FVlo89

"Android Framework Learning Manual":https://qr18.cn/AQpN4J

  1. Boot Init process
  2. Start the Zygote process at boot
  3. Start the SystemServer process at boot
  4. Binder driver
  5. AMS startup process
  6. The startup process of the PMS
  7. Launcher's startup process
  8. The four major components of Android
  9. Android system service - distribution process of Input event
  10. Android underlying rendering-screen refresh mechanism source code analysis
  11. Android source code analysis in practice

Guess you like

Origin blog.csdn.net/maniuT/article/details/130610107