Forums Register Login

Recursion

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
Thanks Carl! How do I go about changing the stack size?
+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
carl, do I type it in the command line?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thank you Carl!!
+Pie Number of slices to send: Send
Hi Carl, I got this message:
Unrecognized option: -Xoss1m
Could not create the java Virtual Machine.
What should I do?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
Hi, I typed
java -oss1m NQ 8
8 is my arg[0]
And I have JDK1.3. I think my JDK has schizophrenia!!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you Carl! Thanks for your help and patience.
+Pie Number of slices to send: Send
NP, what did you find out? Did Sun eliminate the option or has it changed in some way with 1.3?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Why did Sun do that? I changed my recursion to return in some arbitrary intervals, then call it again. Thank you Carl so much!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you....but I still wish JDK would have allowed for users to change stack size.
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1338 times.
Similar Threads
Strange recursive method call! ???
Recursion in java
XSOMParser throwing out of memory error
Recursion vs Nested loops.
Why recursion?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:14:08.