-Xss 和 -XX:ThreadStackSize 有什么区别?

What is the difference between -Xss and -XX:ThreadStackSize?

我只想控制 Java (groovy) 应用程序中所有线程的堆栈大小。对于 Hotspot Oracle VM,我知道有两个参数可以做到这一点 (-Xss and XX:ThreadStackSize)。

哪个是首选?它们之间有什么区别吗?关于 Open JDK 7 someone asked on the mailing list,声明 -Xss 对于 Hotpot VM 与 -XX:ThreadStackSize 相同。

重点是,我正在测量可以在我的系统上启动多少线程。 我的 groovy 执行此操作的脚本如下所示:

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
}

有趣的是,我只是使用 -XX:ThreadStackSize 减少了线程数。我正在启动 groovy 应用程序,在环境变量 JAVA_OPTS 中有不同的内容。

groovy countmax-threads.groovy

当我将 JAVA_OPTS 设置为 -XX:ThreadStackSize=2m 时,我会启动大约 1000 个线程,直到内存耗尽。但是,当我使用 JAVA_OPTS='-Xss2m' 时,我会得到大约 32000 个线程,直到出现预期的错误。所以看起来 -Xss 根本不起作用。

我正在使用

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

在 Ubuntu 14.04 64 位机器上,有四个硬件线程和大约 8 GB 的 RAM。

更新:

我在我的 Windows 7 64 位机器和另一台 JDK:

上重新验证了这一点

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)

并且 -Xss-XX:ThreadStackSize 按预期工作(正如一些答案指出的那样)。所以我想这是一个 Linux 特定问题,甚至是 JDK 版本 1.8.05.

中的错误

-Xss 是 Java HotSpot VM 识别的标准选项。

-XX:ThreadStackSize 因为其他 -XX 选项不稳定,如有更改,恕不另行通知。

Java HotSpot VM Options

-Xss-XX:ThreadStackSize 的别名,对于 OpenJDK 和 Oracle JDK.

尽管他们解析参数的方式不同:
-Xss 可以接受后缀为 K、M 或 G 的数字;
-XX:ThreadStackSize= 需要一个整数(无后缀)- 以千字节为单位的堆栈大小。

-Xss 仅适用于 main Java thead,但 -XX:ThreadStackSize 适用于所有 Java 线程。

If -Xss (or -ss) were passed on the command line, it gets picked up directly by the launcher and is used later to create the "main" Java thread, without asking the VM for the preferred thread stack size. That where inconsistency comes from: if -Xss is given after -XX:ThreadStackSize, then things are still good; otherwise, the "main" Java thread would have a stack size specified by -Xss where as other Java threads' stack size would still follow that of ThreadStackSize.

Inconsistency between -Xss and -XX:ThreadStackSize in the java launcher

2019 年更新 Java SE 8

Current Oracle Java SE 8 文档表明 -Xss-XX:ThreadStackSize=size 是等效的。参见
https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html


对于-Xss

-Xsssize  

   Sets the thread stack size (in bytes). Append the 
   letter k or K to indicate KB, m or M to indicate MB, g or G to 
   indicate GB. The default value depends on the platform:

Linux/ARM (32-bit): 320 KB

Linux/i386 (32-bit): 320 KB

Linux/x64 (64-bit): 1024 KB

OS X (64-bit): 1024 KB

Oracle Solaris/i386 (32-bit): 320 KB

Oracle Solaris/x64 (64-bit): 1024 KB

The following examples set the thread stack size to 1024 KB in different units:

-Xss1m
-Xss1024k
-Xss1048576 

This option is equivalent to -XX:ThreadStackSize.

对于-XX:ThreadStackSize=size

-XX:ThreadStackSize=size 

  Sets the thread stack size (in bytes). Append the 
  letter k or K to indicate kilobytes, m or M to indicate 
  megabytes, g or G to indicate gigabytes. The default 
  value depends on the platform:

Linux/ARM (32-bit): 320 KB

Linux/i386 (32-bit): 320 KB

Linux/x64 (64-bit): 1024 KB

OS X (64-bit): 1024 KB

Oracle Solaris/i386 (32-bit): 320 KB

Oracle Solaris/x64 (64-bit): 1024 KB

The following examples show how to set the thread stack size to 1024 KB in different units:

-XX:ThreadStackSize=1m
-XX:ThreadStackSize=1024k
-XX:ThreadStackSize=1048576 

This option is equivalent to -Xss.

Oracle Java SE 8 文档建议 -Xss 和 -XX:ThreadStackSize=size 是等效的。 然而,这是不正确的。尝试例如

java -XX:ThreadStackSize=1024 -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

java -Xss1024 -version
The stack size specified is too small, Specify at least 160k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

这是固定的,例如在 Java 14 documentation:

-XX:ThreadStackSize=size Sets the Java thread stack size (in kilobytes). Use of a scaling suffix, such as k, results in the scaling of the kilobytes value so that -XX:ThreadStackSize=1k sets the Java thread stack size to 1024*1024 bytes or 1 megabyte.

-Xss size Sets the thread stack size (in bytes).