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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28767905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:45:24  来源:igfitidea点击:

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

javamultithreadingoraclejvmjvm-hotspot

提问by user2078148

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 (-Xssand XX:ThreadStackSize).

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

Which is the preferred one? Is there any difference between them? Regarding Open JDK 7 someone asked on the mailing list, stating that -Xssis the same for the Hotpot VM as -XX:ThreadStackSize.

哪个是首选?它们之间有什么区别吗?关于 Open JDK 7,有人在邮件列表中询问,指出-XssHotpot VM 与-XX:ThreadStackSize.

The point is, that I am measuring how many threads can be started on my system. My groovy script which does this looks like:

关键是,我正在测量可以在我的系统上启动多少线程。我执行此操作的 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
}

Interestingly enough I just get a reduced number of of threads using -XX:ThreadStackSize. I am starting the groovy application with and with different content in the environment variable JAVA_OPTS.

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

groovy countmax-threads.groovy

When I set JAVA_OPTS to -XX:ThreadStackSize=2m, I get about 1000 started threads until the memory is consumed. But, when I use JAVA_OPTS='-Xss2m', I get about 32000 threads until the expected error arises. So it seems that -Xssdoes not work at all.

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

I am using

我在用

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)

java 版本“1.8.0_05”
Java(TM) SE 运行时环境(构建 1.8.0_05-b13)
Java HotSpot(TM) 64 位服务器 VM(构建 25.5-b02,混合模式)

on a Ubuntu 14.04 64 bit machine with four hardware threads and about 8 GB of RAM.

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

UPDATE:

更新:

I reverified this on my Windows 7 64 bit machine and another JDK:

我在我的 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)

java 版本“1.8.0_20”Java(TM) SE 运行时环境(构建 1.8.0_20-b26)Java HotSpot(TM) 64 位服务器 VM(构建 25.20-b23,混合模式)

and there -Xssand -XX:ThreadStackSizework 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.

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

采纳答案by alain.janinm

-Xssis standard options recognized by the Java HotSpot VM.

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

-XX:ThreadStackSizeas other -XXoptions are not stable and are subject to change without notice.

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

See Java HotSpot VM Options

请参阅Java HotSpot VM 选项

回答by apangin

-Xssis an alias for -XX:ThreadStackSizeboth for OpenJDK and Oracle JDK.

-Xss-XX:ThreadStackSizeOpenJDK 和 Oracle JDK的别名。

Though they parse arguments differently:
-Xssmay accept a number with K, M or G suffix;
-XX:ThreadStackSize=expects an integer (without suffix) - the stack size in kilobytes.

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

回答by Alan Thompson

UPDATED 2019 for Java SE 8

Java SE 8 的 2019 年更新

Current Oracle Java SE 8 docs suggest that -Xssand -XX:ThreadStackSize=sizeare equivalent. See
https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

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



For -Xss:

对于-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.


For -XX:ThreadStackSize=size

为了 -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.

回答by lichengwu

-Xssworks only on mainJava thead, but -XX:ThreadStackSizeworks on all Java thread.

-Xss仅适用于 Java 线程main,但-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.

如果 -Xss(或 -ss)在命令行上被传递,它会被启动器直接选取并稍后用于创建“主”Java 线程,而不需要向 VM 询问首选线程堆栈大小。不一致的原因是:如果在 -XX:ThreadStackSize 之后给出 -Xss,那么事情仍然很好;否则,“主”Java 线程的堆栈大小将由 -Xss 指定,而其他 Java 线程的堆栈大小仍将遵循 ThreadStackSize 的堆栈大小。

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

java启动器中 -Xss 和 -XX:ThreadStackSize 之间的不一致