Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum stackspace for a function

Tags:

java

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.

like image 799
Christophe De Troyer Avatar asked Dec 11 '25 19:12

Christophe De Troyer


2 Answers

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.

like image 152
Durandal Avatar answered Dec 14 '25 09:12

Durandal


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.

like image 23
M A Avatar answered Dec 14 '25 07:12

M A