[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Level of stack allowed by java

Posted on 2007-07-19
11
Medium Priority
?
222 Views
Last Modified: 2010-03-30
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;
    }


   
0
Comment
Question by:william007
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 20

Expert Comment

by:Venabili
ID: 19525429
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
 
LVL 9

Author Comment

by:william007
ID: 19525449
Exactly Venabili, thanks for catching that:)
0
 
LVL 20

Accepted Solution

by:
Venabili earned 1000 total points
ID: 19525460
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Venabili
ID: 19525472
And of course the last one had to read "the stack size" and not the "stack trace"...
:) Sorry about this
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 600 total points
ID: 19525544
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
 
LVL 20

Assisted Solution

by:Venabili
Venabili earned 1000 total points
ID: 19525549
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 200 total points
ID: 19529006
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
 
LVL 3

Assisted Solution

by:asood314
asood314 earned 200 total points
ID: 19533942
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
 
LVL 14

Expert Comment

by:CPColin
ID: 19534000
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
 
LVL 3

Expert Comment

by:asood314
ID: 19535556
I stand corrected.
0
 
LVL 9

Author Comment

by:william007
ID: 19595393
Thanks:)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question