title: What is the difference between -Xss and -XX:ThreadStackSize?
body: <p>I just want to control the stack size for all of my threads in a Java (groovy) application. For the Hotspot Oracle VM, I know that there are two parameters doing that  (<a href="http://www.onkarjoshi.com/blog/209/using-xss-to-adjust-java-default-thread-stack-size-to-save-memory-and-prevent-stackoverflowerror/" rel="nofollow noreferrer"><code>-Xss</code></a> and <a href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html" rel="nofollow noreferrer"><code>XX:ThreadStackSize</code></a>).</p>
  
  <p>Which is the preferred one? Is there any difference between them? Regarding Open JDK 7 <a href="http://mail.openjdk.java.net/pipermail/hotspot-dev/2011-June/004272.html" rel="nofollow noreferrer">someone asked on the mailing list</a>, stating that <code>-Xss</code> is the same for the Hotpot VM as <code>-XX:ThreadStackSize</code>. </p>
  
  <p>The point is, that I am measuring how many threads can be started on my system.
  My groovy script which does this looks like:</p>
  
  <pre><code>int count = 0
  
  def printCountThreads = {
       println("XXX There were started $count threads.")
  }
  
  try {
      while(true){
              new Thread({Thread.sleep(Integer.MAX_VALUE)}).start()
              count++
              if(count % 1000 == 0){
                      printCountThreads()
              }
      }
  } catch (Throwable e){
      printCountThreads()
      throw e
  }
  </code></pre>
  
  <p>Interestingly enough I just get a reduced number of of threads using -<code>XX:ThreadStackSize</code>. I am starting the groovy application with and with different content in the environment variable JAVA_OPTS. </p>
  
  <pre><code>groovy countmax-threads.groovy
  </code></pre>
  
  <p>When I set JAVA_OPTS to <code>-XX:ThreadStackSize=2m</code>, I get about 1000 started threads until the memory is consumed. But, when I use <code>JAVA_OPTS='-Xss2m'</code>, I get about 32000 threads until the expected error arises. So it seems that <code>-Xss</code> does not work at all. </p>
  
  <p>I am using </p>
  
  <blockquote>
    <p>java version "1.8.0_05"<br>
    Java(TM) SE Runtime Environment (build 1.8.0_05-b13)<br>
    Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)  </p>
  </blockquote>
  
  <p>on a Ubuntu 14.04 64 bit machine with four hardware threads and about 8 GB of RAM.</p>
  
  <p>UPDATE:</p>
  
  <p>I reverified this on my Windows 7 64 bit machine and another JDK:</p>
  
  <blockquote>
    <p>java version "1.8.0_20"
    Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
    Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)</p>
  </blockquote>
  
  <p>and there <code>-Xss</code> and <code>-XX:ThreadStackSize</code> work as expected (as some answers pointed out). So I suppose it is a Linux specific problem or even a bug in the JDK version 1.8.05.</p>