| By Alois Reitbauer | Article Rating: |
|
| April 22, 2011 07:30 AM EDT | Reads: |
7,839 |
Recently there was a bigger discussion at dynaTrace around the cost of exceptions. When working with customers we very often find a lot of exceptions they are not aware of. After removing these exceptions, the code runs significantly faster than before. This creates the assumption that using exceptions in your code comes with a significant performance overhead. The implication would be that you better avoid using exceptions. As exceptions are an important construct for handling error situation, avoiding exceptions completely does not seem to be good solution. All in all this was reason enough to have a closer look at the costs of throwing exceptions.
The Experiment
I based my experiment on a simple piece of code that randomly throws an exception. This is not a really scientifically-profound measurement and we also don’t know what the HotSpot compiler does with the code as it runs. Nevertheless it should provide us with some basic insights.
public class ExceptionTest {
public long maxLevel = 20;
public static void main (String ... args){
ExceptionTest test = new ExceptionTest();
long start = System.currentTimeMillis();
int count = 10000;
for (int i= 0; i < count; i++){
try {
test.doTest(2, 0);
}catch (Exception ex){
// ex.getStackTrace();
}
}
long diff = System.currentTimeMillis() - start;
System.out.println(String.format("Average time for invocation: %1$.5f",((double) diff)/count));
}
public void doTest (int i, int level){
if (level < maxLevel){
try {
doTest (i, ++level);
}
catch (Exception ex){
// ex.getStackTrace();
throw new RuntimeException ("UUUPS", ex);
}
}
else {
if (i > 1) {
throw new RuntimeException("Ups".substring(0, 3));
}
}
}
}
The Result
The result was very interesting. The cost of throwing and catching an exception seems to be rather low. In my sample it was about 0.002ms per Exception. This can more or less be neglected unless you really throw too many exceptions – and too many means we are talking about 100.000 or more.
While these results show that exception handling itself is not affecting code performance, it leaves open the question: what is responsible for the huge performance impact of exceptions? So obviously I was missing something – something important.
After thinking about it again, I realized that I was missing an important part of exception handling. I missed out the part on what you do when exceptions occur. In most cases you – hopefully – do not just catch the exception and that’s it. Normally you try to compensate for the problem and keep the application functioning for your end users. So the point I was missing was the compensation code that is executed for handling an exception. Depending on what this code is doing the performance penalty can become quite significant. In some cases this might mean retrying to connect to a server in other cases it might mean using a default fallback solution that is providing a far less-performing solution.
While this seemed to be a good explanation for the behavior we saw in many scenarios, I thought I am not done yet with the analysis. I had the feeling that there is something else that I was missing here.
Stack Traces
Still curious about this problem I looked into how the situation changes when I collect stack traces. This is what very often happens. You log an exception and its stack trace to try to figure out what the problem is.
I therefore modified my code to now get the stack trace of an exception as well. This changed the situation dramatically. Getting the stack trace of an exception had a 10x higher impact on the performance than just catching and throwing them. So while stack traces help to understand where and possibly also why a problem occurred, they come with a performance penalty.
The impact here is often very high as we are not talking about a single stack trace. In most cases exceptions are thrown – and caught – at multiple levels. Let us look at a simple example of a Web Service client connecting to a server. First there is an exception at the Java library level for the failed connection. Then there is a framework exception for the failed client and then there might be an application-level exception that some business logic invocation failed. This now sums up to three stack traces being collected.
In most cases you should see them in your log files or application output. Writing these potentially long stack traces again comes with some performance impact. At least you normally see and you can react to them if you look at your log files regularly – which is something you do, don’t you? ![]()
In some cases I have seen even worse behavior due to some incorrect logging code. Instead of checking whether a certain log level is enabled by calling log.isxxEnabled () first, developers just call logging methods. When this happens, logging code is always executed including getting stack traces of exceptions. As the log level however is set too low they never show up anywhere you might not even be aware of them. Checking for log levels first should be a general rule as it also avoids unnecessary object creation.
Conclusion
Not using exceptions because of their potential performance impact is a bad idea. Exceptions help to provide a uniform way to cope with runtime problems and they help to write clean code. You however need to trace the number of exceptions that are thrown in your code. Although they might be caught they can still have a significant performance impact. In dynaTrace we, by default, track thrown exceptions – and in many cases people are surprised by what is going on in their code and what the performance impact is in resolving them.
While exception usage is good you should avoid capturing too many stack traces. In many cases they are not even necessary to understand the problem – especially if they cover a problem you already expect. The exception message therefore might prove as being enough information. I get enough out of a Connection refused message so I do not need the full stack trace into the internal of the java.net call stack.
Related reading:
- Application Performance Monitoring in production – A Step-by-Step Guide – Part 1 // Setting up Application Performance Monitoring is a big task,...
- The impact of Garbage Collection on Java performance // In my last post I explained what a major...
- Top 10 Performance Problems taken from Zappos, Monster, Thomson and Co For a recent edition of the Swiss Computerworld Magazine we...
- Top 10 Client-Side Performance Problems in Web 2.0 Inspired by the Top 10 Performance Problems post which focuses...
- Real Life Ajax Troubleshooting Guide One of our clients occasionally runs into the following problem...
Published April 22, 2011 Reads 7,839
Copyright © 2011 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
- "Lincoln and Lee at Antietam - The Cost of Freedom" to Air on Television Memorial Day Weekend
- "Lincoln and Lee at Antietam - The Cost of Freedom" DVD Wins Top Award at WorldFest - Houston Film Festival
- "Lincoln and Lee at Antietam - The Cost Of Freedom" Available at Borders Stores Nationwide
- Have a Instant Look at Various Industrial Aids
- Victim-nomics: Estimating the “Costs” of Compromise
More Stories By Alois Reitbauer
Alois Reitbauer works as a Technology Strategist for dynaTrace Software where he is leading the Methods and Technology team. As part of the R&D team he influences the dynaTrace product strategy and works closely with key customers in implementing performance management solution for the entire lifecylce. Alois has 10 years experience as architect and developer in the Java and .NET space. He is a frequent speaker at technology conferences on performance and architecture related topics and regularly publishes articles blogs on blog.dynatrace.com
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York Speaker Profile: Dave Linthicum – Cloud Technology Partners
- Windows Azure IaaS Reaches General Availability
- Enterasys Spotlights SDN's Impact on Traditional Networking in Upcoming Webinar
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- NASA's Twitter Account Wins Back-To-Back Shorty Awards
- Big Data Isn’t About the Database, It’s About the Application
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Upcoming Bloomberg BNA Webinar Focuses on COPPA Compliance
- Cloud Expo New York | Danger Ahead: Why File Sync Is NOT Endpoint Backup
- Cloud Expo New York: Basics of SSD Technology and Its Use in Cloud
- AWS Going into a New Line of Work
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York Speaker Profile: Dave Linthicum – Cloud Technology Partners
- Cloud Expo New York: How to Use Google Apps Script
- Windows Azure IaaS Reaches General Availability
- Enterasys Spotlights SDN's Impact on Traditional Networking in Upcoming Webinar
- Upcoming Domino's Pizza Investor Events
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Scripps Networks Interactive’s Popular Lifestyle Shows from HGTV, DIY Network, Food Network, Cooking Channel and Travel Channel Coming to Prime Instant Video and Amazon Instant Video
- Rackspace Hosting Named “Platinum Plus Sponsor” of Cloud Expo New York
- RetailMeNot Shoppers Trend Report: While Over 8 in 10 U.S. Residents Cite Affordability as Their Top Vacation Priority, a Majority (58%) Could Waste Hundreds of Dollars by Booking Travel a la Carte
- Small Cancers, Big Data, and a Life Examined
- NASA's Twitter Account Wins Back-To-Back Shorty Awards
- The Top 150 Players in Cloud Computing
- Who Are The All-Time Heroes of i-Technology?
- Where Are RIA Technologies Headed in 2008?
- Success, Arrogance, Rise and Fall
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Personal Branding Checklist
- The Top 250 Players in the Cloud Computing Ecosystem
- i-Technology Viewpoint: Attack of the Blogs
- Exclusive Q&A with Jeff Haynie, Co-Founder & CEO, Appcelerator
- Web 2.0 News and Wrapping Up "Real-World AJAX" Seminar
- Passing Parameters to Flex That Works
- i-Technology Viewpoint: It's Time to Take the Quotation Marks Off "Web 2.0"



























