Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Performance issue with printStackTrace()

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes I would like to write printStackTrace() in the exception catch block. My tech lead argues that this method has got significant performance issue. So he prevents me from not to use it.

Is he right?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't this code only get invoked if there were an exceptional circumstance, out of line with normal execution? Even if it were a performance drag, the cost wouldn't occur most of the time. And isn't it more important to know the circumstances of an exception taking place rather than blowing past it really fast?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joes answer is perfectly fine ..
The only problem i can see using printStackTrace is that it list out all those method who are in calling chain for the code with exception, sometimes the chain is too long , that the JVM shows something like this
....
and 40 mores ..
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the name implies, exceptions normally happen only in exceptional circumstances. In those exceptional circumstances, one is normally more interested in doing the right thing than doing it fast. Where that's the case, printStackTrace() might provide useful diagnostic information and could well be the right thing to do.

However, sometimes exceptions are much more frequent. This typically indicates that a method is being used in a way that its original designers didn't intend, but that doesn't always make it wrong. If exceptions are happening frequently, and you are thinking of doing printStackTrace(), then there could be a real performance issue.

So the question is: do exceptions happen frequently in the code you're working on?
 
Antony Raj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exceptions doesn't happen frequently. Like when the remote machine is down, it is obviously going to throw the exception. Anyways it is going to happen rarely.

Here my question is, Is calling printStackTrace() costs some performance? I guess it is like calling other methods. Isn't it?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
printStackTrace() has to traverse the data in the Exception, which can be quite a lot, and print it to a stream. Such I/O is much slower than other code. That said, it's still only going to take milliseconds, unless you're printing to a 1974 Teletype or something mad like that.

But, as I said before, if this is a rare occurrence, then the speed is irrelevant. So, in summary, I think you're right and your "lead developer" is wrong.
 
Antony Raj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys for your information.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to pinpoint that the method is threadsafe meaning 'synchronized' is put, which is why it's costly.
Same applies to System.out.print()-kind methods, and even using Vector for single threaded.

Hope this helps your understanding.

Brendan
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antony Raj wrote:
Here my question is, Is calling printStackTrace() costs some performance?



Of course it does. And it's probably much more expensive than many other methods. But the point people are trying to make is: So what? If your foo() method takes 1 ms normally and 1000 ms when you printStackTrace() inside it, so what? It doesn't matter because, a) That 1000ms cost is going to come very, very rarely relative to the 1ms times, and if it doesn't, you're throwing exceptions as part of your normal execution, which is a much bigger problem, and, b) When something does go wrong, it's important that you see as much information as possible about what the system was doing at the time, even if it costs performance.

Now, you can still use you judgment about which stack traces to print. You don't usually need to print them when catching InterruptedException, for instance, or when catching a NumberFormatException as a result of a user typing "abc" when asked for his age. But for those that actually indicate a problem, the performance cost of printing them is small in the big picture, and well worth it if it helps you fix a bug.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brendan Kim wrote:I'd like to pinpoint that the method is threadsafe meaning 'synchronized' is put, which is why it's costly.



No, uncontested synchronization is very cheap.
 
Marshal
Posts: 74757
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Brendan Kim
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pshaw! In most cases, the cost of throwing an exception is going to be much higher than printing the exception! Correct me if I'm wrong, when an exception is thrown, the VM starts travelling up the stack, and has to search for the catch clause at every step in the stack. Finding the catch clause is not a trivial one, and it gets worse and worse the deeper your exception goes.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Pshaw! In most cases, the cost of throwing an exception is going to be much higher than printing the exception!



I kind of doubt that. True, there is a lot of work to do when an exception is thrown, and it's much more expensive than normal "happy path" execution, but I would still expect it to be a smaller cost than the I/O of printing the stack trace.

This is just a gut feel, and I could be completely off base. But either way, the repeatedly-stated point stands: It will be infrequent enough not to be noticeable, and you need that information, even if it is expensive.
 
reply
    Bookmark Topic Watch Topic
  • New Topic