Share 11 practical tips for Java performance tuning
Most developers think that performance optimization is a complex problem, which requires a lot of experience and knowledge. Yes, it's not wrong. Admittedly, it's not easy to optimize your application for the best performance, but that doesn't mean you can't do anything without this experience and knowledge. Here are a few easy to follow suggestions and best practices that can help you create a good performance application.
Most of these suggestions are based on Java, but not necessarily. Some can be applied to all applications and programming languages. Before we share Java based performance tuning techniques, let's discuss these general performance tuning techniques.
1. Do not optimize before necessary
This is probably one of the most important performance tuning techniques. You should follow common best practices and try to implement your use cases effectively. But that doesn't mean replacing any standard libraries or building complex optimizations before it proves necessary.
In most cases, premature optimization takes a lot of time, making the code difficult to read and maintain. Worse, these optimizations usually don't bring any benefits because you spend a lot of time optimizing non critical parts of your application.
So how do you prove that you need to optimize something?
First, you need to determine the speed of the application code, for example, specify a maximum response time for all API calls, or specify the number of records imported within a specific time range. Once you're done, you can measure which parts of the application are too slow to improve. Once you've done this, move on to the second tuning tip.
2. Use the analyzer to find the real bottleneck
After you follow the first suggestion and determine that some parts of your application really need to be improved, ask yourself where to start?
You can solve this problem in two ways:
As for why the second method should always be followed.
The answer should be obvious. An analyzer based approach allows you to better understand the performance implications of your code and allows you to focus on the most critical parts. If you've ever used an analyzer, you'll be surprised at what parts of the code cause performance problems. However, many times, your first guess will lead you in the wrong direction.
3 . Create a performance test suite for the entire application
This is another general technique to help you avoid many unexpected problems that usually occur after performance improvements are deployed to the production environment. You should often define a performance test suite to test the entire application and run it before and after you complete performance improvement.
These additional test runs will help you identify the functional and performance impact of changes and ensure that you do not release an update that does more harm than good. This is especially important if your task runs on multiple different parts of the application, such as databases or caches.
4. First solve the biggest bottleneck problem
After creating the test suite and using the analyzer to analyze the application, you have a list of questions that need to improve performance, which is good, but it still doesn't answer the question where you should start. You can start with those that can be solved quickly, or start with the most important problems.
Of course, the former is attractive, because it will soon come to an end. Sometimes, you may need to convince other team members or your management that performance analysis is worth it.
But in general, I suggest starting with the most important performance issues. This will provide you with the greatest performance improvement, and you may only need to fix a few of these problems to solve your performance requirements.
After understanding the general performance tuning techniques, let's take a closer look at some Java specific tuning techniques.
5. Use StringBuilder to connect strings programmatically
There are many different options for connecting strings in Java. For example, you can use a simple + or + =, old StringBuffer, or StringBuilder.
Since we have discussed data types, let's look at BigInteger and BigDecimal. Especially the latter is popular because of its high precision. But there is a price. BigInteger and BigDecimal require more memory than simple long or double, and greatly reduce all computing speed. Therefore, if you need extra precision, or your number exceeds a long range, it's best to think twice. This may be the only thing you need to change in improving performance, especially when you are implementing a mathematical algorithm.
One candidate is Apache commons Lang's stringutils Replace method. As Lukas Eder described in his recent blog post, it greatly exceeds Java 8's string Replace method. It only needs a small change. You just need to add a maven dependency for the Apache's commons Lang project to your application POM XML and use stringutils The replace method replaces all strings Call of replace method.
Original link: 11 simple java performance tuning tips (editor in charge / Wei Wei)