Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the effects of exceptions on performance in Java?

Question: Is exception handling in Java actually slow?

Conventional wisdom, as well as a lot of Google results, says that exceptional logic shouldn't be used for normal program flow in Java. Two reasons are usually given,

  1. it is really slow - even an order of magnitude slower than regular code (the reasons given vary),

and

  1. it is messy because people expect only errors to be handled in exceptional code.

This question is about #1.

As an example, this page describes Java exception handling as "very slow" and relates the slowness to the creation of the exception message string - "this string is then used in creating the exception object that is thrown. This is not fast." The article Effective Exception Handling in Java says that "the reason for this is due to the object creation aspect of exception handling, which thereby makes throwing exceptions inherently slow". Another reason out there is that the stack trace generation is what slows it down.

My testing (using Java 1.6.0_07, Java HotSpot 10.0, on 32 bit Linux), indicates that exception handling is no slower than regular code. I tried running a method in a loop that executes some code. At the end of the method, I use a boolean to indicate whether to return or throw. This way the actual processing is the same. I tried running the methods in different orders and averaging my test times, thinking it may have been the JVM warming up. In all my tests, the throw was at least as fast as the return, if not faster (up to 3.1% faster). I am completely open to the possibility that my tests were wrong, but I haven't seen anything out there in the way of the code sample, test comparisons, or results in the last year or two that show exception handling in Java to actually be slow.

What leads me down this path was an API I needed to use that threw exceptions as part of normal control logic. I wanted to correct them in their usage, but now I may not be able to. Will I instead have to praise them on their forward thinking?

In the paper Efficient Java exception handling in just-in-time compilation, the authors suggest that the presence of exception handlers alone, even if no exceptions are thrown, is enough to prevent the JIT compiler from optimizing the code properly, thus slowing it down. I haven't tested this theory yet.

like image 474
John Ellinwood Avatar asked Nov 18 '08 15:11

John Ellinwood


People also ask

What is an exception in Java and how it affects your work?

Java Exception is a simple mechanism for handling runtime errors. If we try to understand "exception" in general, it relates to errors that take place while executing a program. Exception handling is also used in C# programming language.

What happens when an exception happens in Java?

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

What are the disadvantages of exception handling in Java?

Disadvantages. Using exceptions for error handling has two disadvantages. First, exceptions can trap only runtime errors. Therefore, a PL/SQL program cannot trap and recover from compile-time (syntax and semantic) errors such as table or view does not exist.

Why are exceptions slow?

What does it mean that exceptions are “slow”? Mainly it means that a throw can take a Very Long Time™ compared to e.g. an int assignment, due to the search for handler.


2 Answers

FYI, I extended the experiment that Mecki did:

method1 took 1733 ms, result was 2 method2 took 1248 ms, result was 2 method3 took 83997 ms, result was 2 method4 took 1692 ms, result was 2 method5 took 60946 ms, result was 2 method6 took 25746 ms, result was 2 

The first 3 are the same as Mecki's (my laptop is obviously slower).

method4 is identical to method3 except that it creates a new Integer(1) rather than doing throw new Exception().

method5 is like method3 except that it creates the new Exception() without throwing it.

method6 is like method3 except that it throws a pre-created exception (an instance variable) rather than creating a new one.

In Java much of the expense of throwing an exception is the time spent gathering the stack trace, which occurs when the exception object is created. The actual cost of throwing the exception, while large, is considerably less than the cost of creating the exception.

like image 30
Hot Licks Avatar answered Nov 09 '22 06:11

Hot Licks


It depends how exceptions are implemented. The simplest way is using setjmp and longjmp. That means all registers of the CPU are written to the stack (which already takes some time) and possibly some other data needs to be created... all this already happens in the try statement. The throw statement needs to unwind the stack and restore the values of all registers (and possible other values in the VM). So try and throw are equally slow, and that is pretty slow, however if no exception is thrown, exiting the try block takes no time whatsoever in most cases (as everything is put on the stack which cleans up automatically if the method exists).

Sun and others recognized, that this is possibly suboptimal and of course VMs get faster and faster over the time. There is another way to implement exceptions, which makes try itself lightning fast (actually nothing happens for try at all in general - everything that needs to happen is already done when the class is loaded by the VM) and it makes throw not quite as slow. I don't know which JVM uses this new, better technique...

...but are you writing in Java so your code later on only runs on one JVM on one specific system? Since if it may ever run on any other platform or any other JVM version (possibly of any other vendor), who says they also use the fast implementation? The fast one is more complicated than the slow one and not easily possible on all systems. You want to stay portable? Then don't rely on exceptions being fast.

It also makes a big difference what you do within a try block. If you open a try block and never call any method from within this try block, the try block will be ultra fast, as the JIT can then actually treat a throw like a simple goto. It neither needs to save stack-state nor does it need to unwind the stack if an exception is thrown (it only needs to jump to the catch handlers). However, this is not what you usually do. Usually you open a try block and then call a method that might throw an exception, right? And even if you just use the try block within your method, what kind of method will this be, that does not call any other method? Will it just calculate a number? Then what for do you need exceptions? There are much more elegant ways to regulate program flow. For pretty much anything else but simple math, you will have to call an external method and this already destroys the advantage of a local try block.

See the following test code:

public class Test {     int value;       public int getValue() {         return value;     }      public void reset() {         value = 0;     }      // Calculates without exception     public void method1(int i) {         value = ((value + i) / i) << 1;         // Will never be true         if ((i & 0xFFFFFFF) == 1000000000) {             System.out.println("You'll never see this!");         }     }      // Could in theory throw one, but never will     public void method2(int i) throws Exception {         value = ((value + i) / i) << 1;         // Will never be true         if ((i & 0xFFFFFFF) == 1000000000) {             throw new Exception();         }     }      // This one will regularly throw one     public void method3(int i) throws Exception {         value = ((value + i) / i) << 1;         // i & 1 is equally fast to calculate as i & 0xFFFFFFF; it is both         // an AND operation between two integers. The size of the number plays         // no role. AND on 32 BIT always ANDs all 32 bits         if ((i & 0x1) == 1) {             throw new Exception();         }     }      public static void main(String[] args) {         int i;         long l;         Test t = new Test();          l = System.currentTimeMillis();         t.reset();         for (i = 1; i < 100000000; i++) {             t.method1(i);         }         l = System.currentTimeMillis() - l;         System.out.println(             "method1 took " + l + " ms, result was " + t.getValue()         );          l = System.currentTimeMillis();         t.reset();         for (i = 1; i < 100000000; i++) {             try {                 t.method2(i);             } catch (Exception e) {                 System.out.println("You'll never see this!");             }         }         l = System.currentTimeMillis() - l;         System.out.println(             "method2 took " + l + " ms, result was " + t.getValue()         );          l = System.currentTimeMillis();         t.reset();         for (i = 1; i < 100000000; i++) {             try {                 t.method3(i);             } catch (Exception e) {                 // Do nothing here, as we will get here             }         }         l = System.currentTimeMillis() - l;         System.out.println(             "method3 took " + l + " ms, result was " + t.getValue()         );     } } 

Result:

method1 took 972 ms, result was 2 method2 took 1003 ms, result was 2 method3 took 66716 ms, result was 2 

The slowdown from the try block is too small to rule out confounding factors such as background processes. But the catch block killed everything and made it 66 times slower!

As I said, the result will not be that bad if you put try/catch and throw all within the same method (method3), but this is a special JIT optimization I would not rely upon. And even when using this optimization, the throw is still pretty slow. So I don't know what you are trying to do here, but there is definitely a better way of doing it than using try/catch/throw.

like image 60
Mecki Avatar answered Nov 09 '22 06:11

Mecki