PDA

View Full Version : Java: catching stack overflow / sigbus error




MacNeXT
Dec 3, 2004, 05:53 PM
Hi

I'm writing a piece of software in which it would be convenient to catch a stack overflow error. I know a stack overflow can be considered to be a very bad thing, especially in Java, but it's a natural phenomenon in this case. If you want to know why, let me know, but I'll stick to the subject in case no one cares.

The problem is that I sometimes get a stack overflow, which is the expected behavior, but in many other cases I get:

"Executable “java” has exited due to signal 10 (SIGBUS)."

This means that the operating system shut down the JVM because it did something illegal. Obviously, this is not acceptable, you can't catch this within Java. Is this a bug in Apple's JVM port? What does "signal 10 (SIGBUS)" mean?

Has anyone seen this before? Unfortunately, I haven't got access to a Windows or Linux system right now, so I can't find out how their JVM ports react in this case.

Any insight is appreciated.



broken_keyboard
Dec 3, 2004, 07:21 PM
It seems like the JVM itself is crashing. If that's the case you can't catch the error in Java since how can your Java program exist without the JVM? (in a runtime sense)

You can increase the stack of the JVM process itself like so:
ulimit -s 16384

Or you can increase the stack of the Java program with the -Xss command line option to java I think. I had to use this once when I had a recursive function that went a bit too deep...

MacNeXT
Dec 3, 2004, 07:37 PM
It seems like the JVM itself is crashing. If that's the case you can't catch the error in Java since how can your Java program exist without the JVM? (in a runtime sense)

You can increase the stack of the JVM process itself like so:
ulimit -s 16384

Or you can increase the stack of the Java program with the -Xss command line option to java I think. I had to use this once when I had a recursive function that went a bit too deep...

Yes, as I mentioned too, the OS exits the JVM, and ofcourse there is nothing in Java to catch when the Java Virtual Machine doesn't exist anymore. I will look into your suggestions on how to increase the stack, thanks, but I don't consider that to be a solution to the problem. The problem is: Why the hell does the JVM crash in such a horrendous way? Looks like a bug to me.

broken_keyboard
Dec 3, 2004, 10:04 PM
You may want to check out the Java Bug Parade at Sun's website and post your bug.

jeremy.king
Dec 6, 2004, 11:05 AM
Yes, as I mentioned too, the OS exits the JVM, and ofcourse there is nothing in Java to catch when the Java Virtual Machine doesn't exist anymore. I will look into your suggestions on how to increase the stack, thanks, but I don't consider that to be a solution to the problem. The problem is: Why the hell does the JVM crash in such a horrendous way? Looks like a bug to me.

Depends on your code. Anyone can write code to crash a JVM with stack overflows. Have you tried profiling and debugging using Eclipse or something similar? Are you using any recursion or have an absurd amount of method nesting? Are you using JNI to call other non-java libraries?

Another way to increase the JVM stack (can't say I have used broken's method) is to use the -Xss flag on your java command.

For example: -Xss10m or -Xss512k

MacNeXT
Dec 7, 2004, 08:06 AM
Depends on your code. Anyone can write code to crash a JVM with stack overflows. Have you tried profiling and debugging using Eclipse or something similar? Are you using any recursion or have an absurd amount of method nesting? Are you using JNI to call other non-java libraries?

Another way to increase the JVM stack (can't say I have used broken's method) is to use the -Xss flag on your java command.

For example: -Xss10m or -Xss512k

It's not direct recursion. It's more like A calls B calls C calls A calls B ... (oversimplified). It's also a mixture of static methods and instance methods. But what do you mean with "crash a JVM with stack overflows"? I can't find evidence that a stack overflow is something that cannot be caught in some cases, it should be possible to handle this. It should not crash the JVM. Unlike for example an Out of Memory error, which cannot be caught.

It still looks like a bug to me.

FritzTheWonderM
Dec 7, 2004, 09:18 AM
It still looks like a bug to me.
Does this help?:

http://lists.apple.com/archives/java-dev/2002/Aug/msg00129.html

jeremy.king
Dec 7, 2004, 10:27 AM
But what do you mean with "crash a JVM with stack overflows"?

Sounds more like a code issue to me. You are so quick to blame the JVM. Have you tried Profiling your application?

See http://www.geocities.com/herong_yang/jvm/stack.html

ALthough JVMs have improved since 2002, I would venture to guess these simple programs are still effective at crashing a JVM.

Aside - Worth a shot, but wrap your code with a try/catch and instead of catching Exception, catch Throwable.

FritzTheWonderM
Dec 7, 2004, 10:48 AM
Aside - Worth a shot, but wrap your code with a try/catch and instead of catching Exception, catch Throwable.

Good point. Also, try running the same code on different JVMs if possible. If it doesn't explode on the Windows or Linux 1.4.2_xx JVM or maybe a 1.3 JVM it's probably Apple's problem.