Impact of JAVA exceptions on Performance
When providing technical support to OneAPM customers, we often see exceptions that many customers don't even realize. After these exceptions are eliminated, the code running speed is greatly improved. This leads to a speculation that the use of code may lead to significant performance overhead. Because exceptions are an important part of error handling, it is impossible to discard them. Therefore, we need to measure the impact of exception handling on performance, we can use an experiment to see the impact of abnormal processing on performance.
Lab
My experiment is based on a simple piece of code that throws an exception randomly. From a scientific point of view, this is not completely accurate measurement, and I do not knowHotSpotWhat will the compiler do to the running code. But in any case, this code should let us know some basic information.
The result is interesting: the cost of throwing and capturing exceptions seems very low. In my example, every exception is about 0.02 milliseconds. Unless you throw too many exceptions, we mean 0.1 million times or more), this can be ignored. Although these results show that exception handling does not affect code performance, it does not solve the following problem: who is responsible for the huge impact of exceptions on performance?
I have missed out on important issues.
After thinking about it, I realized that I missed an important part of exception handling. I did not consider what you did when the exception occurred. In most cases, you may not just capture exceptions! This is where the problem is: In general, you will try to supplement the problem and make the application still function in the end user. So what I missed is: "" Supplemental code executed to handle exceptions ". Depending on the supplemental code, the performance loss may become quite significant. In some cases, this may mean retrying to connect to the server. In other cases, it may mean using the default rollback solution, the solution provided by this solution will certainly bring very poor performance. This seems to give a good explanation of the behavior we see in many cases.
However, I don't think everything has been done here, but I feel that nothing else is missing.
Stack trace
I am still curious about this issue, so I monitored the performance changes when collecting strack traces.
This often happens: Write down exceptions and stack tracks, and try to find out where the problem is.
For this reason, I modified the code and collected strack trace exceptions. This significantly changes the situation. For the collection of strack trace exceptions, the performance impact is 10 times higher than that of captured and thrown exceptions. Therefore, although strack trace helps to understand where a problem occurs, it may also help to understand why the problem occurs), there is a performance loss. Since we are not talking about a strack trace, the impact here is often very large. In most cases, an exception is thrown and captured at multiple layers. Let's take a simple example: the Web service client connects to the server. First, there is a connection failure exception at the Java library level. Later, there will be a framework-level client failure exception, and later there may be an application-level business logic call failure exception. Up to now, three strack traces have to be collected. In most cases, you can see these in log files or application output.strack traceAnd write these longstrack traceThis will also affect performance.
Conclusion
First, it is not a good strategy to discard exceptions because of performance impact. Exceptions help to provide a consistent way to solve runtime problems and help to write clean code. However, we should track the number of exceptions thrown in the code, which may cause significant performance impact. Therefore, OneAPM should track the thrown exceptions by default-in many cases, people are surprised by the exceptions in the Code and the performance loss when solving these exceptions. Secondly, despite the benefits of exceptions, you should avoid capturing too manystrackTrace. Exceptions should be designed for exceptions and should be kept in mind when used. Of course, if you don't want to follow good programming habits,JavaThe language will let you know that doing so can make your program run faster and encourage you to do that.