Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursion" Watch "Recursion" New topic
Author

Recursion

ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I am trying to do the N-Queen problem, but I always blew the stack when running it. I use recursion and it always goes to 5000 levels deep at 8x8 when blowing up the stack. Can anyone help?
Thanks!
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
java.exe has two options available that will allow you to increase the stack size for any run.

-ssx
Each Java thread has two stacks: one for Java code and one for C code. The -ss option sets the maximum stack size that can be used by C code in a thread to x. Every thread that is spawned during the execution of the program passed to java has x as its C stack size. The default units for x are bytes. The value of x must be greater than or equal to 1000 bytes.
You can modify the meaning of x by appending either the letter "k" for kilobytes or the letter "m" for megabytes. The default stack size is 128 kilobytes ("-ss128k").

-ossx
Each Java thread has two stacks: one for Java code and one for C code. The -oss option sets the maximum stack size that can be used by Java code in a thread to x. Every thread that is spawned during the execution of the program passed to java has x as its Java stack size. The default units for x are bytes. The value of x must be greater than or equal to 1000 bytes.
You can modify the meaning of x by appending either the letter "k" for kilobytes or the letter "m" for megabytes. The default stack size is 400 kilobytes ("-oss400k").


[This message has been edited by Carl Trusiak (edited August 02, 2000).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thanks Carl! How do I go about changing the stack size?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Since you are using recursion, you need more stack space for your java code so it would be something like.
java -oss1m [any other options that you are using such as -cp] [class file to execute]
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
carl, do I type it in the command line?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Yes.
I have a class name InitTest.class. It doesn't include recursion or any thing else that could blow out the stack but, it ran successfully with
java -oss1m InitTest
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thank you Carl!!
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi Carl, I got this message:
Unrecognized option: -Xoss1m
Could not create the java Virtual Machine.
What should I do?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by ricky gonzalez:
Hi Carl, I got this message:
Unrecognized option: -Xoss1m
Could not create the java Virtual Machine.
What should I do?

What are you entering in the command line? What jdk are you using?
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I typed
java -oss1m NQ 8
8 is my arg[0]
And I have JDK1.3. I think my JDK has schizophrenia!!!
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I'm using jdk 1.2.2_01 and it runs just fine. Check the documentation for java.exe with 1.3, Sun may have eliminated that as an option or, changed the syntax.
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thank you Carl! Thanks for your help and patience.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
NP, what did you find out? Did Sun eliminate the option or has it changed in some way with 1.3?
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, Carl, the syntax for JDK1.3 is
java -Xms1m myFile (for 1 mb)
java -Xmx1m myFile (for maximum 1 mb)
I forgot the link to this, but it should not be hard to find.
The thing is that, even after the stack was increased, the program still blew the stack at exactly 5000 levels deep. So I don't know if this is correct.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The option -Xms changes the size of the program heap and not the stack. Method calls are tracked on the stack that's why recursion can cause this kind of error. With java.exe in jdk1.2.2 the option -oss exsists to modify the size of the stack.
I just visited the Sun site for the tools documentation for 1.3. http://java.sun.com/j2se/1.3/docs/tooldocs/tools.html#basic
They did away with the option to set the stack size AND reduced the size of the default stack! Also read the docs, they call for a minimum for -Xms of 1M + 1024 and for -Xmx of 2M + 1024.
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Why did Sun do that? I changed my recursion to return in some arbitrary intervals, then call it again. Thank you Carl so much!!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
They reduced the size of the default stack so programs could run more threads. Remember, each thread gets a stack. Now the per-thread stack allocation is only 1/4 of what it used to be.
Any recursive algorithm can do this to you, so you should avoid recursion in production code. That applies to all languages that use similar mechanisms for function calls, not just to Java. No matter what you set the stack size to, some loony will find a way to call your recursive method with arguments that'll blow it. In theory (at least) you can always invent an iterative solution that's equivalent to a recursive algorithm, and it'll usually run faster too.
I guess you've heard the programming joke "To iterate is human, to recurse is divine." The Divine Ones evidently have machines with infinite RAM.
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thank you....but I still wish JDK would have allowed for users to change stack size.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursion
 
Similar Threads
Why recursion?
XSOMParser throwing out of memory error
Recursion vs Nested loops.
Recursion in java
Strange recursive method call! ???