title: Why is Java stack use inconsistent?
body: <p>Question:</p>
  
  <p>I have a recursive program in Java. When I run it by manually setting the stack size with <code>java -Xss1000k</code> it sometimes throws a stackoverflow error and sometimes doesn't. The program and inputs are not changing between runs. I can, of course, increase the stack size so that it runs fine or decrease the size so there's always an error, 1000k just happens to be right on the edge. But why is it able to sometimes work at this value? Shouldn't a program take the same stack space each time it runs?</p>
  
  <p>More context:</p>
  
  <p>The program simply iterates through a linked list and removes a few values. This can and should be implemented iteratively but my prof wanted to teach recursion so here we are. I understand the basics of the Java stack, a new frame is added for each method call and is removed when the call finishes. The only similar situation I could find was <a href="https://stackoverflow.com/q/30114718/9814339">here</a> but the solution there is just use iteration. I'm not so much looking for a solution to the problem as an explanation of why it happens. </p>