Oracle says that on Windows
-Xsssize默认值取决于虚拟内存
如何在给定的Oracle JVM 8中找出java在Windows上分配的线程堆栈大小的值?
我尝试过where to find default XSS value for Sun/Oracle JVM?
的解决方案但它只是打印0。
java -XX:+PrintFlagsFinal -version
java -XX:+PrintFlagsFinal
应该打印实际的线程堆栈大小,而不是0.看起来像JVM错误。
我想调整JVM性能,并想知道为线程堆栈分配了多少内存。它精确地指定为unix platforms。而且我无法获得Windows的这个价值,这很奇怪。
答案 0 :(得分:5)
这不是问题,它是一种特定于平台的行为,略微具有向后兼容性。 HotSpot的源代码中有两个有趣的文件:
在globals_windows_x86中,HotSpot将ThreadStackSize初始化为0以使用系统默认值:
// Default stack size on Windows is determined by the executable (java.exe
// has a default value of 320K/1MB [32bit/64bit]). Depending on Windows version, changing
// ThreadStackSize to non-zero may have significant impact on memory usage.
// See comments in os_windows.cpp.
define_pd_global(intx, ThreadStackSize, 0); // 0 => use system default
define_pd_global(intx, VMThreadStackSize, 0); // 0 => use system default
在os_windows_x86中,有一个解释为什么在Windows平台上堆栈大小为0:
// Create the Win32 thread
//
// Contrary to what MSDN document says, "stack_size" in _beginthreadex()
// does not specify stack size. Instead, it specifies the size of
// initially committed space. The stack size is determined by
// PE header in the executable. If the committed "stack_size" is larger
// than default value in the PE header, the stack is rounded up to the
// nearest multiple of 1MB. For example if the launcher has default
// stack size of 320k, specifying any size less than 320k does not
// affect the actual stack size at all, it only affects the initial
// commitment. On the other hand, specifying 'stack_size' larger than
// default value may cause significant increase in memory usage, because
// not only the stack space will be rounded up to MB, but also the
// entire space is committed upfront.
//
// Finally Windows XP added a new flag 'STACK_SIZE_PARAM_IS_A_RESERVATION'
// for CreateThread() that can treat 'stack_size' as stack size. However we
// are not supposed to call CreateThread() directly according to MSDN
// document because JVM uses C runtime library. The good news is that the
// flag appears to work with _beginthredex() as well.
您也可以阅读MSDN document。
为什么Windows平台上的大小为零?这是将默认值传递给WinAPI的最简单方法,Java主线程http://bugs.java.com/view_bug.do?bug_id=4689767中存在一个问题,分辨率为:
Windows:从二进制文件中读取默认的线程堆栈大小 (java.exe的);主线程堆栈是用这个大小创建的。
隐藏主要差异的替代解决方案 线程和其他线程是为了避免运行任何java字节码 由于JNI,主线程认为一般不可能。
在我们停止支持支持之前,它不会固定在窗户上 WIN95 / WIN98 / WinME的
让我总结一下 - ThreadStackSize
是一个内部属性,可能有任何默认值,例如Windows上为0以支持传统平台(ME / 98)。 PrintFlagsFinal
也提供调试信息而没有任何保证,因此在没有一定知识的情况下引用此信息是不正确的。从1.7.0_45开始,Hotpot有一个很好的内部虚拟机功能,称为"Native Memory Tracking" (NMT)
java -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics -version
...
- Thread (reserved=14453KB, committed=14453KB)
(thread #14)
(stack: reserved=14392KB, committed=14392KB)
(malloc=44KB #76)
(arena=16KB #28)
您可以尝试将堆栈大小从默认值(在此示例中显示为1M,保留空间为14453 KB的14个线程)减少到-Xss256k
以下的值:
- Thread (reserved=10613KB, committed=10613KB)
(thread #14)
(stack: reserved=10552KB, committed=10552KB)
(malloc=44KB #76)
(arena=16KB #28)
答案 1 :(得分:2)
我终于从JDK源代码中找到了答案。
获取源代码:
hg clone http://hg.openjdk.java.net/jdk8/jdk8/hotspot/
根据JDK文档, Xss 值可以更改 Java线程堆栈大小。但是,论点如何起作用?这是代码:
HANDLE thread_handle =
(HANDLE)_beginthreadex(NULL,
(unsigned)stack_size,
(unsigned (__stdcall *)(void*)) java_start,
thread,
CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
&thread_id);
Xss 被分配给 stack_size ,该内存用于为线程堆栈分配内存。
但是如果您什么都没设置怎么办?在 os_windows.cpp 中,请参见以下段落:
// Create the Win32 thread
//
// Contrary to what MSDN document says, "stack_size" in _beginthreadex()
// does not specify stack size. Instead, it specifies the size of
// initially committed space. The stack size is determined by
// PE header in the executable. If the committed "stack_size" is larger
// than default value in the PE header, the stack is rounded up to the
// nearest multiple of 1MB. For example if the launcher has default
// stack size of 320k, specifying any size less than 320k does not
// affect the actual stack size at all, it only affects the initial
// commitment. On the other hand, specifying 'stack_size' larger than
// default value may cause significant increase in memory usage, because
// not only the stack space will be rounded up to MB, but also the
// entire space is committed upfront.
如果未设置 Xss 值,则默认堆栈大小取决于PE文件(java.exe)。如果您运行32位Java应用程序,则默认堆栈大小为 320K 。如果您运行64位Java应用,则默认堆栈大小为 1024K 。
我们可以使用以下源代码来验证堆栈大小:
#include <windows.h>
typedef u_char* address;
address os::current_stack_base() {
MEMORY_BASIC_INFORMATION minfo;
address stack_bottom;
size_t stack_size;
VirtualQuery(&minfo, &minfo, sizeof(minfo));
stack_bottom = (address)minfo.AllocationBase;
stack_size = minfo.RegionSize;
// Add up the sizes of all the regions with the same
// AllocationBase.
while( 1 )
{
VirtualQuery(stack_bottom+stack_size, &minfo, sizeof(minfo));
if ( stack_bottom == (address)minfo.AllocationBase )
stack_size += minfo.RegionSize;
else
break;
}
#ifdef _M_IA64
// IA64 has memory and register stacks
//
// This is the stack layout you get on NT/IA64 if you specify 1MB stack limit
// at thread creation (1MB backing store growing upwards, 1MB memory stack
// growing downwards, 2MB summed up)
//
// ...
// ------- top of stack (high address) -----
// |
// | 1MB
// | Backing Store (Register Stack)
// |
// | / \
// | |
// | |
// | |
// ------------------------ stack base -----
// | 1MB
// | Memory Stack
// |
// | |
// | |
// | |
// | \ /
// |
// ----- bottom of stack (low address) -----
// ...
stack_size = stack_size / 2;
#endif
return stack_bottom + stack_size;
}