catalog

1. Project background.............................................. .................................................. .................................................. .................................................. .........3

2. Challenges encountered ... .................................................. .................................................. .................................................. .....3

3. Taking OOM as an example, explain the steps to solve this type of problem......................... .................................................. .................................................. .................................................. .............3

1. What is OOM............................................. .................................................. .................................................. .................................................. ...3

2. How to check the available memory of the JVM virtual machine... .................................................. .................................................. .................................................. ........4

3. How to monitor and optimize the memory situation of the application... .................................................. .................................................. .................................................. ........5

4. Comparison between normal and abnormal memory... .................................................. .................................................. .................................................. ...5

5. Application memory optimization............................ .................................................. .................................................. .................................................. ....7

4. Monitoring of IOS and React Native applications... .................................................. .................................................. .................................................. ........9

V. Summary............................................... .................................................. .................................................. .................................................. .10

Appendix: "Introduction"............................................ .................................................. .................................................. .................................................. ....11

1. Project background

In a certain energy APP developed by our company, it serves the employees of many household energy enterprises. In the past few years of development, I have actually encountered many feedbacks reported by users, but the root cause of some problems is unknown. Sometimes it is even necessary to refactor a module as a whole to solve the problem. After several years of iteration , Share some practical experience of online applications here.

In the process of developing App, you often encounter OOM (out of memory) memory overflow. This article will combine my years of development experience to discuss solutions to performance problems.

2. Challenges encountered

Many concurrent operations will be encountered in the apps and mini programs launched by our company, especially on the App side. In some control operations, they are often affected by network fluctuations and response speeds, and their operations often fail or become abnormal. Quitting, even crashing, and other problems. When encountering such problems, it is often a headache for programmers, because the problem is not easy to locate, and it is not easy to find the location of the problem. It also brings a bad experience to online users.

Although it is a 2B business, it will not cause any loss to the number of users in the short term, but if such problems cannot be solved in the long term, the frequency of use of many users will gradually decrease. There are also scenarios that are similar to the remote intelligent control of the machine on the mobile phone. If an error occurs, it may also cause unnecessary economic losses.

3. Taking OOM as an example, explain the steps to solve such problems

1. What is OOM

Among the error messages collected, let’s take a look at what the OOM looks like when it reports an error:

09-08 21:15:38.771: E/dalvikvm-heap(123452): Out of memory on a 20455736-byte allocation. 09-08 21:15:38.779: E/AndroidRuntime(23231): java.lang.OutOfMemoryError

This is because the program applied for "20455736" byte memory, but the Java virtual machine could not provide such a large memory, so the program was forced to die.

2. How to check the available memory of the JVM virtual machine

Our system's memory will not be used by the JVM virtual machine, so we need to know how much memory is actually available in order to know the direction we should optimize.

During development, you can test out the available memory of the current system by using java commands

 java -XmxXXXXM -version

For example: Execute the command java -Xmx2000M -version, if the version information of java is displayed, it means that 2000M memory is available.

In this way, you can always modify the memory size to test until it shows:

The above figure appears, indicating that the memory has exceeded the available memory of the JVM, and this memory is the maximum memory of the current JVM.

We can calculate the amount of memory that can be used by the application based on the memory size set by the simulator. Different models have different memory sizes. Make sure that the mainstream models have lower configurations and can run programs normally and smoothly.

3. How to monitor and optimize the memory of the application

Regardless of whether it is an Android APP or a Java program, we can judge whether there is an abnormality by monitoring the operation of the APP in the JVM.

We can use the JDK monitoring and management console (jconsole.exe) in the installation directory: jdk/bin.

Through this platform, you can monitor the running status of the application, the memory usage, and whether there are deadlocked threads, as shown in the following figure:

4. Comparison of normal and abnormal memory

For applications with normal memory usage, the memory monitoring should look like this:

For applications with memory problems, the memory curve is like this (continuously increasing, not decreasing):

The above figure shows that the application memory has been increasing since it was started, and the memory is still in short supply after garbage collection. Then the end of the curve is the value near the limit where OOM occurs in the application.

5. Application memory optimization

The purpose of memory optimization is to allow us to adjust the JVM parameters so that the memory used by the application can be recycled well, and the memory will not only increase and decrease in long-term operation.

①. Optimize the memory ratio of the old generation

If the memory occupied by Old GC keeps increasing and accounts for a relatively large amount, we can optimize the JVM parameters and set the -XX:NewRatio value to 4, which means: the new generation accounts for 1, the old generation accounts for 4, and the young generation accounts for the entire heap. 1/5. This optimization is to allocate more memory in the old generation of GC.

SurviorRatio is also set to 4, that is, set the proportion of Eden area, S0/S1 is the same.

②. Increase the target utilization rate of the survivor area

Set the target usage rate of the survivor area, and readjust the TenuringThreshold value when the usage rate is reached, so that the object can go to the old area as soon as possible.

-XX:TargetSurvivorRatio: A parameter for calculating the desired Survivor size (Desired survivor size). The default value is 50, which is 50%

-XX:TargetSurvivorRatio=50 (the default is 50, that is, reach)

After the object enters the Survivor area, because the default adaptive strategy is used, when the age is 1, the Minor GC is 1 time, and it is moved to the old age. As a result, the memory consumption of the old generation is too fast. In a concurrent scenario, an FGC occurs once in a while.

We can increase this value,

-XX:TargetSurvivorRatio=90

③. Increase the maximum memory during program operation

If after optimization, the program still needs a lot of memory at some resource-consuming time points, then we can only increase the maximum memory of the application running in the JVM.

-Xmx1024M

④. Choose the best garbage collection algorithm

Test data of different garbage collection methods. In step ① and step ②, we actually do a little bit of testing, and finally determine which scheme to use. Records of multiple tests:

4. Monitoring IOS and React Native applications

The above mentioned dynamic monitoring tools and optimization strategies are all based on the Java platform, so how to monitor the applications developed by IOS and React Native? How to understand what errors and exceptions occurred when these applications were running? How to collect these errors and transfer them to the developer's bug fix plan?

Recommend an application performance monitoring platform U-APM here:

On this platform, we can monitor the abnormalities of the application in real time, and have complete statistics on the abnormalities and the number of abnormalities, model, version number, application download source market, operating system, and user region. There is also the application's memory usage of each model. No matter how many simulators and real machines we test, it can't match the big data of real users.

U-Meng U-APM mobile application performance monitoring platform provides analysis data such as crash, freeze, startup, memory, network, etc., which can cover all indicators of developers to restore bugs, and help developers quickly locate and fix bugs.

5. Summary

1. To be targeted for performance tuning, you need to make targeted adjustments, comparisons, and observations based on the characteristics of the actual business system and the JVM log records for a certain period of time.

2. Performance tuning is an extremely time-consuming and endless process. It is necessary to weigh the tuning cost (including labor cost) and the cost of replacing hardware in exchange for user experience.

3. Performance tuning includes not only the tuning of JVM, but also the tuning and optimization of server hardware configuration, operating system parameters, middleware thread pool, database connection pool, database parameters, and specific database tables, indexes, and partitions.

4. It is a relatively economical and quick tuning method to check and correct the performance problems existing in the code through a specific tool (application performance monitoring platform U-APM). Find the problem late. Therefore, the monitoring platform must be launched, and the abnormalities captured by the platform must be repaired and optimized before users perceive it.

Appendix: "Introduction"

I currently work in a listed energy company and have been engaged in the Internet development industry for 7 years. In the direction of mobile development, I have participated in two Android APPs and RN applets, where I served as the APP architecture design and main developer. He is currently responsible for the digital transformation of the Energy Internet. He has many years of experience in IoT data platforms, monitoring platforms, and energy management platforms. He has deep involvement in data governance, data management, application development, and platform operation and maintenance.

In my spare time, I like to read and study knowledge not limited to the computer field. Often write blogs, share technology and professional knowledge, and discuss technical details with colleagues.


性能优化实践者
11 声望220 粉丝