Level of stack allowed by java

Does java specify how many level of stack is allowed? or it is machine dependent?

I try this function, it only allows me up to n=6955, n=6966 will cause stack overflow.
    public static double fact(double n){
        if(n==0) return 1;
    }


   
LVL 9
william007Asked:
Who is Participating?
 
VenabiliConnect With a Mentor Commented:
The stack trace is OS and Java settings  dependant
See this:
http://forum.java.sun.com/thread.jspa?threadID=187774&messageID=615478

It is a bit old but is still valid (and I actually need to tweak my Solaris machine sometimes this way so I have the discussion bookmarked)
0
 
VenabiliCommented:
Is it the factorial?
    public static double fact(double n){
        if(n==0) return 1;
       return fact(n-1)*n;
    }

or you mean another function. Because what you posted won't even compile :)
0
 
william007Author Commented:
Exactly Venabili, thanks for catching that:)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
VenabiliCommented:
And of course the last one had to read "the stack size" and not the "stack trace"...
:) Sorry about this
0
 
CPColinConnect With a Mentor Senior Java ArchitectCommented:
It'll also depend on how much data needs to be put onto the stack for each method call (called the size of the "stack frame"). If you're passing more parameters to the method, the stack frame will be larger, and you'll get fewer levels of recursion before the stack fills up and dies. The same happens when the method has more local variables, which also increases the size of the stack frame.

It's all fun stuff that you don't have to worry about very often unless you're programming in assembly.
0
 
VenabiliConnect With a Mentor Commented:
Something else for reading:
The VM options: http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp - check the ones for the Stack size

This is why I say Java settings dependant :)
0
 
ksivananthConnect With a Mentor Commented:
stack is always limited and error will be thrown when its exceeded...

the solution is to use the for/while loops instead of recursive function call!
0
 
asood314Connect With a Mentor Commented:
Loops are better than recursive function calls if you're concerned about stack size because you're not creating so many new variables.  However, if you do want to change implementations, try using Runtime.getRuntime().gc().  This function run Java's garbage collector and free up any memory to which there are no pointers.  It will slow down your program, but it should allow you to prevent stack overflow or at least get more recursions.
0
 
CPColinSenior Java ArchitectCommented:
The stack and the heap are separate, and there's nothing an explicit call to run the garbage collector will do that the VM wouldn't have done on its own when space got tight.
0
 
asood314Commented:
I stand corrected.
0
 
william007Author Commented:
Thanks:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.