Java: catching stack overflow / sigbus error

Discussion in 'Mac Programming' started by MacNeXT, Dec 3, 2004.

  1. macrumors 6502

    Joined:
    Jun 21, 2004
    #1
    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.
     
  2. macrumors 65816

    broken_keyboard

    Joined:
    Apr 19, 2004
    Location:
    Secret Moon base
    #2
    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...
     
  3. thread starter macrumors 6502

    Joined:
    Jun 21, 2004
    #3
    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.
     
  4. macrumors 65816

    broken_keyboard

    Joined:
    Apr 19, 2004
    Location:
    Secret Moon base
    #4
    You may want to check out the Java Bug Parade at Sun's website and post your bug.
     
  5. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #5
    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
     
  6. thread starter macrumors 6502

    Joined:
    Jun 21, 2004
    #6
    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.
     
  7. macrumors member

    Joined:
    Dec 12, 2003
    Location:
    Planet 10
    #7
  8. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #8
    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.
     
  9. macrumors member

    Joined:
    Dec 12, 2003
    Location:
    Planet 10
    #9
    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.
     

Share This Page