Configuring Stack Sizes in the JVM – 在JVM中配置堆栈大小

最后修改: 2020年 7月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’re going to see how to configure the thread stack sizes in the HotSpot JVM.

在这个快速教程中,我们将看到如何在HotSpot JVM中配置线程栈大小。

2. Default Stack Size

2.默认堆栈大小

Each JVM thread has a private native stack to store call stack information, local variables, and partial results. Therefore, the stack plays a crucial part in method invocations. This is part of the JVM specification, and consequently, every JVM implementation out there is using stacks.

每个JVM线程都有一个私有的本地堆栈,用于存储调用堆栈信息、本地变量和部分结果。因此,堆栈在方法调用中起着至关重要的作用。这是JVM规范的一部分,因此,外面的每一个JVM实现都在使用堆栈。

However, other implementation details, such as stack size, are implementation-specific. From now on, we’ll focus on the HotSpot JVM and will use the terms JVM and HotSpot JVM interchangeably.

然而,其他实现细节,如堆栈大小,是特定的实现。从现在开始,我们将专注于HotSpot JVM,并将交替使用JVM和HotSpot JVM这两个术语。

Anyway, the JVM will create stacks at the same time it creates the owning thread.

无论如何,JVM将在创建拥有线程的同时创建堆栈。

If we don’t specify a size for the stacks, the JVM will create one with a default size. Usually, this default size depends on the operating system and computer architecture. For instance, these are some of the default sizes as of Java 14:

如果我们没有为堆栈指定一个尺寸,JVM 将以默认尺寸创建一个堆栈。通常,这个默认尺寸取决于操作系统和计算机架构。例如,这些是截至 Java 14 的一些默认大小。

Basically, we can expect around 1 MB for each stack in most modern operating systems and architectures.

基本上,在大多数现代操作系统和架构中,我们可以期待每个堆栈有1MB左右。

3. Customizing the Stack Size

3.自定义堆栈大小

To change the stack size, we can use the -Xss tuning flag. For example, the -Xss1048576 sets the stack size equal to 1 MB:

为了改变堆栈大小,我们可以使用-Xsstuningflag。例如,-Xss1048576设置堆栈大小等于1MB。

java -Xss1048576 // omitted

If we don’t want to calculate the size in bytes, we can use some handy shortcuts to specify different units — the letter k or K to indicate KB, m or M to indicate MB, and g or G to indicate GB. For example, let’s see a couple of different ways to set the stack size to 1 MB:

如果我们不想以字节为单位计算大小,我们可以使用一些方便的快捷方式来指定不同的单位–字母kK表示KB,mM表示MB,而gG则表示GB。例如,让我们看看将堆栈大小设置为1MB的几种不同方法。

-Xss1m 
-Xss1024k

Similar to -Xss, we can also use the -XX:ThreadStackSize tuning flag to configure the stack size. The syntax for -XX:ThreadStackSize, however, is a bit different. We should separate the size and the flag name with an equal sign:

-Xss类似,我们也可以使用-XX:ThreadStackSize调整标志来配置堆栈大小。然而,-XX:ThreadStackSize的语法有点不同。我们应该用一个等号来分隔大小和标志名称。

java -XX:ThreadStackSize=1024 // omitted

The HotSpot JVM won’t allow us to use a size less than a minimum value:

HotSpot JVM 不允许我们使用一个小于最小值的尺寸

$ java -Xss1K -version
The Java thread stack size specified is too small. Specify at least 144k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Also, it won’t allow us to use a size more than the max value (usually 1 GB):

另外,它不允许我们使用超过最大值的尺寸(通常是1GB)。

$ java -Xss2g -version
Invalid thread stack size: -Xss2g
The specified size exceeds the maximum representable size.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

4. Conclusion

4.总结

In this quick tutorial, we saw how to configure the thread stack sizes in the HotSpot JVM.

在这个快速教程中,我们看到了如何在HotSpot JVM中配置线程栈大小。