Here is a SizedStack type that extends Stack:
import java.util.Stack;
public class SizedStack<T> extends Stack<T> {
private int maxSize;
public SizedStack(int size) {
super();
this.maxSize = size;
}
@Override
public T push(T object) {
//If the stack is too big, remove elements until it's the right size.
while (this.size() >= maxSize) {
this.remove(0);
}
return super.push(object);
}
}
Use it like this: Stack<Double> mySizedStack = new SizedStack<Double>(10);. Other than the size, it operates like any other Stack.
java - Creating a fixed-size Stack - Stack Overflow
How to increase the Java stack size? - Stack Overflow
java - What is the default stack size, can it grow, how does it work with garbage collection? - Stack Overflow
StackOverflowError null, how can I increase the stacksize in the jvm
Here is a SizedStack type that extends Stack:
import java.util.Stack;
public class SizedStack<T> extends Stack<T> {
private int maxSize;
public SizedStack(int size) {
super();
this.maxSize = size;
}
@Override
public T push(T object) {
//If the stack is too big, remove elements until it's the right size.
while (this.size() >= maxSize) {
this.remove(0);
}
return super.push(object);
}
}
Use it like this: Stack<Double> mySizedStack = new SizedStack<Double>(10);. Other than the size, it operates like any other Stack.
You can create a very simple stack like this:
public class FixedStack<T>
{
private T[] stack;
private int size;
private int top;
public FixedStack<T>(int size)
{
this.stack = (T[]) new Object[size];
this.top = -1;
this.size = size;
}
public void push(T obj)
{
if (top >= size)
throw new IndexOutOfBoundsException("Stack size = " + size);
stack[++top] = obj;
}
public T pop()
{
if (top < 0) throw new IndexOutOfBoundsException();
T obj = stack[top--];
stack[top + 1] = null;
return obj;
}
public int size()
{
return size;
}
public int elements()
{
return top + 1;
}
}
Hmm... it works for me and with far less than 999MB of stack:
> java -Xss4m Test
0
(Windows JDK 7, build 17.0-b05 client VM, and Linux JDK 6 - same version information as you posted)
I assume you calculated the "depth of 1024" by the recurring lines in the stack trace?
Obviously, the stack trace array length in Throwable seems to be limited to 1024. Try the following program:
public class Test {
public static void main(String[] args) {
try {
System.out.println(fact(1 << 15));
}
catch (StackOverflowError e) {
System.err.println("true recursion level was " + level);
System.err.println("reported recursion level was " +
e.getStackTrace().length);
}
}
private static int level = 0;
public static long fact(int n) {
level++;
return n < 2 ? n : n * fact(n - 1);
}
}
How much a stack can grow?
You can use a VM option named ss to adjust the maximum stack size. A VM option is usually passed using -X{option}. So you can use java -Xss1M to set the maximum of stack size to 1M.
Each thread has at least one stack. Some Java Virtual Machines (JVM) put Java stack (Java method calls) and native stack (Native method calls in VM) into one stack, and perform stack unwinding using a "Managed to Native Frame", known as M2nFrame. Some JVMs keep two stacks separately. The Xss set the size of the Java Stack in most cases.
For many JVMs, they put different default values for stack size on different platforms.
Can we limit this growth?
When a method call occurs, a new stack frame will be created on the stack of that thread. The stack will contain local variables, parameters, return address, etc. In Java, you can never put an object on stack, only object reference can be stored on stack. Since array is also an object in Java, arrays are also not stored on stack. So, if you reduce the amount of your local primitive variables, parameters by grouping them into objects, you can reduce the space on stack. Actually, the fact that we cannot explicitly put objects on Java stack affects the performance some time (cache miss).
Does stack has some default minimum value or default maximum value?
As I said before, different VMs are different, and may change over versions. See here.
How does garbage collection work on stack?
Garbage collections in Java is a hot topic. Garbage collection aims to collect unreachable objects in the heap. So that needs a definition of 'reachable.' Everything on the stack constitutes part of the root set references in GC. Everything that is reachable from every stack of every thread should be considered as live. There are some other root set references, like Thread objects and some class objects.
This is only a very vague use of stack on GC. Currently most JVMs are using a generational GC. This article gives brief introduction about Java GC. And recently I read a very good article talking about the GC on .NET platform. The GC on Oracle JVM is quite similar so I think that might also help you.
As you say, local variables and references are stored on the stack. When a method returns, the stack pointer is simply moved back to where it was before the method started, that is, all local data is "removed from the stack". Therefore, there is no garbage collection needed on the stack, that only happens in the heap.
To answer your specific questions:
- See this question on how to increase the stack size.
- You can limit the stack growth by:
- grouping many local variables in an object: that object will be stored in the heap and only the reference is stored on the stack
- limit the number of nested function calls (typically by not using recursion)
- For windows, the default stack size is 320k for 32bit and 1024k for 64bit, see this link.
Sorry, this might be a dumb question but I couldnt find anything about it in the internet. So I'm having the program run a thread, which turns out to be this error. I researched quite a bit and realized I need to change the StackSize, but I have no idea how to actually use the commands/arguments I found.
I'm using BlueJ and sadly I can't change to another software, so I'm stuck with what I think is the only option for bluej which is the bluej.defs file
Am I wrong here, can I use something else than the defs file? Idk thx for the answers in advance