Simple recursion / stack question  
Author Message
.





PostPosted: 2005-5-15 11:17:00 Top

java-programmer, Simple recursion / stack question
Hi All,

I suspect this is a bit of a 'how long is a piece of string' type question
(the answer to which is of course precisely 2 x half its length) but I'm
going to ask it anyway.....

How deep is the java process stack? If I write some complicated piece of
code that is recursive, how can I know if I will run out of stack doing the
recursion or not?

Michael


 
John B. Matthews





PostPosted: 2005-5-15 12:08:00 Top

java-programmer >> Simple recursion / stack question In article <4286bf1c$0$20504$email***@***.com>,
"." <.@.com> wrote:

> Hi All,
> [...]
> How deep is the java process stack? If I write some complicated piece of
> code that is recursive, how can I know if I will run out of stack doing the
> recursion or not?
>
> Michael

It depends on the initial and maximum heap sizes allocated by the JVM. Use
java -X to see the options.

In general, a complicated recursive routine should be run in a small heap.
That way you won't have to wait so long for it to run out on memory:-)

--
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
 
none





PostPosted: 2005-5-15 14:49:00 Top

java-programmer >> Simple recursion / stack question John B. Matthews wrote:
> It depends on the initial and maximum heap sizes allocated by the JVM. Use
> java -X to see the options.

Wouldn't procedure calls take up memory on the OS stack, not the heap?

Thanks,
-Mike
 
 
John B. Matthews





PostPosted: 2005-5-16 0:10:00 Top

java-programmer >> Simple recursion / stack question In article <yhChe.1749$_f7.200@trndny01>, none <email***@***.com> wrote:

> John B. Matthews wrote:
> > It depends on the initial and maximum heap sizes allocated by the JVM. Use
> > java -X to see the options.
>
> Wouldn't procedure calls take up memory on the OS stack, not the heap?

Naturally, the architecture of a particular JVM might permit this, but such a
JVM would would be unacceptably fragile. In practice, the heap fills up with
whatever the JVM puts in its stack frame(s). Remember, it's the JVM
manipulating a stack to execute your (recursive) code, not the JVM calling
itself recursively. Conversely, nothing you write in java can manipulate the
OS stack directly. Try it on your JVM:

public static int Ack(int m, int n) {
return (m == 0) ? (n + 1) :
((n == 0) ? Ack(m - 1, 1) :
Ack(m - 1, Ack(m, n - 1)));
}


--
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
 
 
darrell





PostPosted: 2005-5-16 23:16:00 Top

java-programmer >> Simple recursion / stack question On Sun, 15 May 2005, it was written:

>
> Hi All,
>
> I suspect this is a bit of a 'how long is a piece of string' type question
> (the answer to which is of course precisely 2 x half its length) but I'm
> going to ask it anyway.....
>
> How deep is the java process stack? If I write some complicated piece of
> code that is recursive, how can I know if I will run out of stack doing the
> recursion or not?

Thje answer to the first question is, the stack is configurable. There is
no one set answer. The second question is the important question. The
answer is to use a tool that can monitor stack usage. If there are outside
variables (user input, variable data, etc.) that can affect the order of
execution then you will have to do an analysis and determine worst case
scenarios. Then you can run the program through a worst case scenario and
let the tools tell you how much stack was used.

Google on profiler or memory debugger and you should be able to find some
tools out there to help you out.

--
Send e-mail to: darrell dot grainger at utoronto dot ca