Thread Stack Size Observations (Windows)
843811Mar 24 2005 — edited Apr 11 2006I'm using j2sdk 1.4.2_06 on Windows XP Pro SP2. I'm trying to understand what limitations there are to the number of threads I can have running at once. The results will influence the architecture of a client-server application we're developing.
Sun's documentation says that on Windows, the default stack size is "whatever the default stack size is when creating a thread in the OS." [http://java.sun.com/docs/hotspot/PerformanceFAQ.html] On Windows, the default stack size is 1 Mb, UNLESS the application creator linked the .exe in such a way as to change the reserve stack size in the exe header. It appears that Sun has modified this default for java.exe; the stack reserve appears to be 0x40000 (or 256 Kb). Given other reports about the JVM's ability to reference VM on 32-bit Windows, we would expect to be able to create approximately 7000 threads. Empirically, this is in fact the case. So far so good! However, this does not mesh with other observations. I've used the -Xss option as described in http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/java.html to try to maximize the number of threads.
(1) If I specify no additional JVM args, I can create 7000 threads, but not 7500.
(2) If I specify -Xss256k, I can create 1800 threads but not 1900.
(3) If I specify -Xss128k, I can create 7000 threads but not 7500.
This would imply that the default is really -Xss128k. However, watching the java.exe's process size in Windows Task Manager shows that using "-Xss128k" is NOT really the same as leaving off the -Xss argument!
JVM Args NumThreads VM Size
-------------------------------------------
(none) 7000 296 Mb
-Xss128k 7000 1000 Mb
-Xss16k 7000 296 Mb
However, when using -Xss16k, it's clear that MORE than 16k is really reserved, as I can not start 7500 threads with -Xss16k. All of this leaves me wondering:
(a) What IS the default stack size when running java.exe 1.4.2_06 on Windows?
(b) If it's NOT 256k (the value in the exe header), then is the value in the exe header relevant at all?
(c) If it IS 256k, then why is the behavior so different when explicitly specifying -Xss256k?