Skip to main content

Reduce the number of thrown (and catched) Exceptions to improve performance

3 replies [Last post]
dietmarhechensteiner
Offline
Joined: 2006-09-07

When launching the swingset2 application, 752 exceptions are thrown (and catched) during the startup phase. 526 of them are ClassNotFoundExceptions. In real world applications ten thousands of exceptions are thrown during the startup phase. Throwing exceptions are expensive operations but most of them can be easily avoided, with a small and compatible redesign of the runtime classes. Please add for example methods like loadClassNoExc or findClassNoExc to the class ClassLoader. They could return null instead of throwing an exception and use them in the phase of classloading. Same for method getObject in class ResourceBundle.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dietmarhechensteiner
Offline
Joined: 2006-09-07

According to the changes histories this issue seems to be fixed in JDK 6u18 build b03 and JDK 7 build b73. See Bug ID 4917309

sjasja
Offline
Joined: 2004-08-15

> Throwing exceptions are expensive operations

How do you measure that? When I time my laptop it can throw and catch more than 1,000,000 exceptions per second. 752 exceptions would take 0.0007 seconds. YMMV. Sure it could be improved, though I wonder if anyone would ever notice the improvement.

dietmarhechensteiner
Offline
Joined: 2006-09-07

I must admit, i did not make any measurements, but i argue your measurement did only measure the time for object creation, not the time for garbage collect them.
One of the goals for jdk 7 is performance tuning (See http://java.sun.com/features/jdk/7/ ).
One of the principles of java performance tuning is to avoid object creation (See: http://www.sun.com/books/documents/effective_java_ch02.pdf Page 20) or see for example the implementation of the method Integer.valueOf(int i).
Maybe the time gain is relativly small, but there are 2 other facts too.
1) If you would examine the thrown and catched exceptions in your program (very interesting!), this is difficult, if there are lots of exceptions thrown from runtime classes.
2) In my opinion in the mentioned cases Exceptions are misused as return values because they are not thrown on exceptional cases but on a regular basis. And code from runtime is taken from programmers as sample for good programming style.