The impact of JAVA exceptions on performance

Source: Internet
Author: User


Taubingzhe-may 12, 2015
When it comes to technical support for ONEAPM's customers, we often see anomalies that many customers simply do not realize. After these exceptions are eliminated, the code runs much faster than before. This allows us to speculate that using exceptions in code can have significant performance overhead. Because exceptions are an important part of error handling, rejection is unlikely, so we need to measure the performance impact of exception handling, and we can look at the performance impact of exception handling through an experiment.

Experiment

My experiment was based on a simple code that randomly throws an exception. From a scientific point of view, this is not completely accurate measurement, at the same time I do not know hotspot   What the compiler does with the code in action. However, this code should allow us to understand some of the basics.  
650) this.width=650; "src=" Http://code.oneapm.com/images/Javaexception.png "title=" "style=" Height:auto; vertical-align:middle;border:0px; "/>

The result is interesting: the cost of throwing and catching exceptions appears to be extremely low. In my case, it's about 0.02 milliseconds for each exception. This is largely negligible unless you really throw too many exceptions (we mean 100,000 or more). Although these results show that exception handling itself does not affect code performance, it does not address the following issue: Who is responsible for the huge impact of exceptions on performance?

I obviously missed out on something important.

After a second thought, I realized that I had missed an important part of exception handling. I didn't think about what you did when the anomaly happened. In most cases you are likely to be more than just catching exceptions! And here's the problem: In general, you'll try to supplement the problem and let the app continue to function with the end user. So what I'm missing is this: "The supplemental code that executes to handle the exception". Performance loss can become quite noticeable, depending on the supplemental code. In some cases this may mean trying to connect to the server, while in other cases it might mean using the default rollback scenario, and the solution offered by this scenario would certainly have very poor performance. This seems to be a good explanation for the behavior we see in many cases.

But I don't think the analysis is all right here, but I feel that there is something else missing.

Stack Trace

I'm still curious about this, and I've monitored how the performance of the Strack trace was changed when I collected it.

What happens most often is this: write down the exception and its stack track, and try to find out where the problem is.

so I modified the code to collect an extra strack trace of the exception. This makes the situation change dramatically. The performance impact of the collection of abnormal Strack trace is 10 times times higher than that of simply capturing and throwing an exception. So while Strack Trace helps to understand where the problem is, it may also help to understand why the problem occurs, but there is a performance penalty. Since we are not talking about a strack trace, the impact here is often very large. In most cases, we are going to throw and catch exceptions at multiple levels. Let's look at a simple example: a WEB service client connects to a server. First, there is a connection failure exception at the Java library level. Thereafter there will be a client failure exception at the framework level, and then there may be a failure exception for the business logic call at the application level. So far, a total of three strack traces have been collected. In most cases, you can see these  strack Trace , while writing these longer strack Trace   often also has a performance impact.

Conclusion

The first reason for the performance impact of the exception is not to abandon the problem. Exceptions help provide a consistent way to troubleshoot run-time problems and help to write clean code. However, we should keep track of the number of exceptions thrown in the code, which can cause significant performance impact. So OneAPM default to keep track of thrown exceptions-in many cases people are amazed at the exceptions that occur in the code and the performance loss when they are resolved. Second, despite the benefits of using exceptions, you should avoid capturing too many strack trace traces. Exceptions should be designed for exceptional situations and should be kept in mind when used. Of course, in case you don't want to follow good programming habits, the Java language will let you know that doing so will allow your program to run faster, encouraging you to do that.


The impact of JAVA exceptions on performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.