3

I am trying to determine when my fork/join implementation will stack overflow.

I know that the compiler determines the maximum stack space that a function needs at compile time. So this information should be available in the .class files of my java code. However, I can't seem to figure out how to get to this value.

Can I print it out at run-time, or could anyone point me to where I might find it in the class file? It's all gibberish in gedit so I can't seem to find it.

Christophe De Troyer
  • 2,852
  • 3
  • 30
  • 47
  • 2
    `.class` files aren't mean to be read by human; it's machine code. Don't bother looking in there. Also, here's some information that might help you out: http://stackoverflow.com/a/20030999/2398375 – Vince Jun 19 '14 at 14:22
  • If you really want to look at .class files use [javap](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html) to give you the disassembly – vandale Jun 19 '14 at 14:25
  • Another resource that supports your issue (couldnt edit previous comment): http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#threads_oom – Vince Jun 19 '14 at 14:29

2 Answers2

3

There is no simple way to tell if a method call will result in a stack overflow.

While you can determine the number of slots a method requires on the stack, this doesn't tell you anything about the number of bytes actually needed.

Java byte code works with an abstraction of the actual stack, where the stack is treated as if were composed of abstract "slots". The this-reference, method parameters and local variables all occupy either one or two slots on the stack (long and double need two slots, everthing else counts as one slot). Thats the information you can obtain from a class file.

What's still missing is how slots actually are translated to stack memory, and also, what the JIT actually creates from the byte code. The JIT might as well decide to add temporary variables when it performs optimizations (such as code invariant movement).

Also when you call a native method, there is no way of telling how much stack memory the call will take.

You see, much depends on the current JRE, VM and platform. Combine this with the lack of a simple build-in method to actually determine how large the actual stack is, this makes it impractical to determine how much stack in needed and how much is available.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • I should agree with you. I have tried the JBE tool but it doesn't give me hard numbers. I think I will formulate my answer in a more general way. Thank you for the information. Could perhaps provide a link with more detailed information? I would like to read up! :) – Christophe De Troyer Jun 19 '14 at 14:43
  • 1
    @ChristopheDeTroyer The dirty details are ultimately all described in the http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html Java Virtual Machine specs. Of paricular intrest are probably chapter 4 (class file format, particularly 4.7.3) and maybe the byte code instruction set (chapter 6). There are probably easier (distilled) articles on specific topics by third parties on the net to answer specific questions. – Durandal Jun 19 '14 at 14:55
2

JBE is a nice standalone tool that lets you browse (and edit) class files. One info it shows is the maximum stack depth for a certain method. If JBE can access this information, then it would probably be possible to do it if you intend to use a bytecode parsing library.

M A
  • 71,713
  • 13
  • 134
  • 174