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

5

17

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

Which is the preferred one? Is there any difference between them? Regarding Open JDK 7 someone asked on the mailing list, stating that -Xss is the same for the Hotpot VM as -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:

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.

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 -Xss does not work at all.

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)

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

UPDATE:

I reverified this on my Windows 7 64 bit machine and another 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)

and there -Xss and -XX:ThreadStackSize 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.

Scald answered 27/2, 2015 at 14:53 Comment(2)
Maybe someone can verify on his machine with an other vm? I used groovy 2.3.9 for the script...Scald
Same behaviour on linux: java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode) Seems to me like a linux specific JVM problem...Scald
S
5

-Xss is standard options recognized by the Java HotSpot VM.

-XX:ThreadStackSize as other -XX options are not stable and are subject to change without notice.

See Java HotSpot VM Options

Serranid answered 27/2, 2015 at 15:3 Comment(5)
Than why there behaviour differs?Scald
@Scald Dis you download the java hotspot from Oracle? The link where it's stated that -Xss and -XX:ThreadStackSize are equivalent is about openjdk. Openjdk and Oracle Hotspot VM might differ. See https://mcmap.net/q/54145/-what-is-the-difference-between-jvm-jdk-jre-amp-openjdk. So -XX:ThreadStackSize that might fail because -XX options are not stable.Serranid
Thank you for your suggestion... but why than -XX:ThreadStackSize=2m just works and -Xss does not?Scald
Oracle Java SE 8 docs suggest they are exact synonyms: docs.oracle.com/javase/8/docs/technotes/tools/unix/java.htmlEncamp
Hi @AlanThompson, yes this might be possible, but it can change. If you can use an option not starting with -XX, then do it. -XX options are not stable and are subject to change without notice.Serranid
R
8

-Xss is an alias for -XX:ThreadStackSize both for OpenJDK and Oracle JDK.

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

Retrogressive answered 27/2, 2015 at 17:22 Comment(3)
Well I don't encountered any problems in specifing -XX:ThreadStackSize=2m, altrough you are right, the docs say what you told me ... but this all makes it not clearScald
Oracle Java SE 8 docs suggest they are exact synonyms: docs.oracle.com/javase/8/docs/technotes/tools/unix/java.htmlEncamp
Thanks. When I provided "-Xss3m" as JAVA_OPTS but the GC logs printed "-XX:ThreadStackSize=3072". 3072 does not have any prefix and I was looking for confirmation that size is in kilobytes.Sepulveda
S
5

-Xss is standard options recognized by the Java HotSpot VM.

-XX:ThreadStackSize as other -XX options are not stable and are subject to change without notice.

See Java HotSpot VM Options

Serranid answered 27/2, 2015 at 15:3 Comment(5)
Than why there behaviour differs?Scald
@Scald Dis you download the java hotspot from Oracle? The link where it's stated that -Xss and -XX:ThreadStackSize are equivalent is about openjdk. Openjdk and Oracle Hotspot VM might differ. See https://mcmap.net/q/54145/-what-is-the-difference-between-jvm-jdk-jre-amp-openjdk. So -XX:ThreadStackSize that might fail because -XX options are not stable.Serranid
Thank you for your suggestion... but why than -XX:ThreadStackSize=2m just works and -Xss does not?Scald
Oracle Java SE 8 docs suggest they are exact synonyms: docs.oracle.com/javase/8/docs/technotes/tools/unix/java.htmlEncamp
Hi @AlanThompson, yes this might be possible, but it can change. If you can use an option not starting with -XX, then do it. -XX options are not stable and are subject to change without notice.Serranid
E
4

UPDATED 2019 for Java SE 8

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


For -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 

  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.
Encamp answered 11/1, 2019 at 5:0 Comment(0)
T
2

The Oracle Java SE 8 docs suggest that -Xss and -XX:ThreadStackSize=size are equivalent. However this not correct. Try eg.

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.

This is fixed e.g. in the 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.

and

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

Thwack answered 8/1, 2021 at 10:16 Comment(0)
N
1

-Xss works only on main Java thead, but -XX:ThreadStackSize works on all Java thread.

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

Norland answered 28/2, 2015 at 7:16 Comment(2)
The cited mailing list post seems to apply for JDK 6 and 7. For JDK 8, I will verify that on my local machine again. You hint was really useful, thank you!Scald
Well after studying the mailing list post by Kris Mok in detail, I have to correct you. -Xss will work for the thread stack size as -XX:ThreadStackSize too. The problem arises when you are using both at the same time (in the order -Xss -XX:ThreadStackSize). In this case, the vm will pick up the value specified in -Xss only for the main thread and the value specified in -XXThreadStackSize for all other user threads. Those two options should be equivalent! One has to be careful when using both (which usually does not make any sense)Scald

© 2022 - 2024 — McMap. All rights reserved.