给 jvm 的线程堆栈大小选项(-Xss)是什么?为什么它在 Windows pc 中至少有 68k 的限制?堆栈、它在、线程、选项

2023-09-07 03:32:13 作者:放白鹿于林间

我见过 JVM 选项 -Xss - 它究竟做了什么? 这个链接,但我的问题是这个选项有什么用处.

I have seen JVM option -Xss - What does it do exactly? this link, but my question is how is this option useful.

因为,如果我们为 -Xss 值设置一个非常小的限制,则线程可能无法正常工作,因为它可能会在大多数情况下抛出 stackOverflow 错误.

Because, if we set a very minimum limit to the -Xss value, maybe the threads are not going to work properly as it may throw stackOverflow error most of the times.

为什么这个值至少有 64k 个限制?我如何得到这个 64k 限制是当我试图在 IntelliJ iDE 上配置运行时 vm 选项时,我试图给出 10k 之类的东西,它弹出了这个错误,指出它需要至少 64k 的线程堆栈大小.

Why is there a 64k limit at least for this value? How i got this 64k limit is when i was trying to configure the runtime vm options on the IntelliJ iDE, I tried to give some thing like 10k and it popped up this error stating it needs at least 64k for thread stack size.

另一个问题是,如何从 java 程序中找到在我的嵌入式设备中运行的 jvm 的默认线程堆栈大小?

Another question is that, how to find the default thread stack size of my jvm running in my embedded device from a java program?

谢谢,森

推荐答案

-Xss 允许根据应用需要配置Java线程栈大小:

-Xss allows to configure Java thread stack size according to application needs:

较大的堆栈大小适用于使用递归算法或其他深度方法调用的应用程序;较小堆栈大小适用于运行数千个线程的应用程序 - 您可能希望节省线程堆栈占用的内存. larger stack size is for an application that uses recursive algorithms or otherwise deep method calls; smaller stack size is for an application that runs thousands of threads - you may want to save memory occupied by thread stacks.

请记住,HotSpot JVM 还为本地方法和 JVM 运行时调用(例如类加载)使用相同的 Java 线程堆栈.这意味着 Java 线程堆栈不仅用于 Java 方法,而且 JVM 也应该为自己的操作保留一些堆栈页面.

Bear in mind that HotSpot JVM also utilizes the same Java thread stack for the native methods and JVM runtime calls (e.g. class loading). This means Java thread stack is used not only for Java methods, but JVM should reserve some stack pages for its own operation as well.

所需的最小堆栈大小由以下公式计算:

The minimum required stack size is calculated by the formula:

(StackYellowPages + StackRedPages + StackShadowPages + 2*BytesPerWord + 1) * 4096

在哪里

StackYellowPagesStackRedPages 是检测和处理 StackOverflowError 所必需的;StackShadowPages 为本地方法保留;2*4(32 位 JVM)或 2*8(64 位 JVM)用于 VM 运行时函数;extra 1 用于主线程中的 JIT 编译器递归;4096 是默认页面大小. StackYellowPages and StackRedPages are required to detect and handle StackOverflowError; StackShadowPages are reserved for native methods; 2*4 (32-bit JVM) or 2*8 (64-bit JVM) is for VM runtime functions; extra 1 is for JIT compiler recursion in main thread; 4096 is the default page size.

例如对于 32 位 Windows JVM 最小堆栈大小 = (3 + 1 + 4 + 2*4 + 1) * 4K = 68K

E.g. for 32-bit Windows JVM minimum stack size = (3 + 1 + 4 + 2*4 + 1) * 4K = 68K

顺便说一句,您可以使用这些 JVM 选项减少所需的最小堆栈大小:(不推荐!)

BTW, you may reduce the minumum required stack size using these JVM options: (not recommended!)

-XX:StackYellowPages=1 -XX:StackRedPages=1 -XX:StackShadowPages=1