On a quest for missing stacktraces

One comment to our recent blog post brought back some memories about a specific experience. The type of experience I wish I had not experienced. Long time before we founded Plumbr I was debugging an application that gave me an exception every once in a blue moon. The level of detail was astounding:
 
 
 
 
 
 
 

java.lang.NullPointerException

That was it – no stacktrace to work with. And the application was guarded by a truly evil sysadmin who would not let me within 100 yards from the deployment. So the stage was set for numerous all-nighters trying to understand what was causing the error. I bet most of our readers know what I am talking about here. The first suspect was obvious. The summer intern who never really got exception handling and who could have easily swallowed the exception instead of rethrowing or proper logging. So I was expecting to find something like this in the code somewhere:

System.out.println(e);

But no matter how thoroughly I grep’ed through my source code I could not find anything resembling such carelessness. I do not recall much from the following day besides answering a phone call every few hours and giving the “progress report”. Unfortunately the progress was nowhere in sight. Unless I would count in the numerous updates done to production in a desperate attempt of getting something a bit more meaningful about the possible causes. But I do remember my faith in Java being tested when I started suspecting something I had always thought being impossible. Namely that Java is capable of literally eating your Exception stacktraces. And indeed the JIT can and will do it. Need proof? Run the following code:

for (int i = 0; i < 100_000; i++) {
	try {
		args[0].toString();
	} catch (Exception e) {
		if (e.getStackTrace().length == 0) {
			System.out.format("Java ate my stacktrace after iteration #%d %n", i);
			break;
		}
	}
}

In my MB Pro equipped with the latest stable JDK 7 hotspot the exception stacktrace gets eaten at the iteration #12,288. But why on earth is this happening and how could one avoid it? The simple answer is that it is one of the JIT optimizations applied during runtime. As one of our recent posts described, creating Exceptions is darn expensive. So, for performance purposes, when the JIT discovers that an exception is being thrown several times, the code may be recompiled. After recompilation, the compiler may choose a faster tactic using preallocated exceptions that do not provide a stack trace. Hopefully the vast majority of you will hopefully never have to deal with a situation like this. After all, when exceptions are created at the pace triggering JIT to switch to preallocated exceptions, you must be doing something else wrong. After all, exceptions should occur only on exceptional situations. But for those who do, the solution is simple. You can disable the use of preallocated exceptions by adding -XX:-OmitStackTraceInFastThrow to your JVM startup parameters. So did I and found the culprit after the next JVM restart.
 

Reference: On a quest for missing stacktraces from our JCG partner Nikita Salnikov Tarnovski at the Plumbr Blog blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

2 Responses to "On a quest for missing stacktraces"

  1. Thanks admin you very much for providing such informational material….
    Please visit also core java concepts

  2. Mike Hayes says:

    The Java Specialist Newsletter has a great article on this (http://www.javaspecialists.eu/archive/Issue187.html).

    Chances are that if you see some gnarly behaviour in Java the The Java Specialist Newsletter will be able to explain it and provide a workaround.

Leave a Reply


3 + = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books