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!
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).]
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]
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
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?
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.
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.
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.
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.